implement host option update channel feed. need to fix sigsegv bug when goroutine reads from closed channel

This commit is contained in:
mykola2312 2025-01-18 10:29:01 +02:00
parent afcba5d91c
commit 9e661baad2
3 changed files with 20 additions and 3 deletions

View file

@ -48,6 +48,10 @@ func (node *LuxNode) GetStateLock() *sync.RWMutex {
return &node.stateLock
}
func (node *LuxNode) GetHostStateChannel() <-chan LuxHostState {
return node.state.GetStateChannel()
}
func (node *LuxNode) handleHeartbeat(packet *net.LuxPacket) {
if packet.ChannelType != net.LuxChannelExterior {
log.Error("heartbeat not on exterior!")

View file

@ -19,17 +19,24 @@ func (state *LuxHostState) String() string {
}
type LuxNodeState struct {
hosts map[proto.LuxID]*LuxHostState
hosts map[proto.LuxID]*LuxHostState
stateChan chan LuxHostState
generation uint64
}
func NewNodeState() LuxNodeState {
return LuxNodeState{
hosts: make(map[proto.LuxID]*LuxHostState),
stateChan: make(chan LuxHostState),
generation: rand.Uint64(),
}
}
func (ns *LuxNodeState) GetStateChannel() <-chan LuxHostState {
return ns.stateChan
}
func (ns *LuxNodeState) RegisterHeartbeat(hostId proto.LuxID, hostState host.LuxState) {
// if we already have host, then update state and increment
if state, ok := ns.hosts[hostId]; ok {
@ -42,7 +49,10 @@ func (ns *LuxNodeState) RegisterHeartbeat(hostId proto.LuxID, hostState host.Lux
Increment: 0,
}
}
// TODO: pass updated options to node controller
// we're making copy here instead of sending pointer,
// so receiver does not mutate it
ns.stateChan <- *ns.hosts[hostId]
}
// Merge current state and state received from node state multicast.
@ -67,6 +77,8 @@ func (ns *LuxNodeState) Merge(new *LuxNodeState) {
// add new host state
ns.hosts[id] = newState
}
// let channel receivers know about new merged states
ns.stateChan <- *ns.hosts[id]
}
}

View file

@ -39,5 +39,6 @@ func TestNodeHeartbeat(t *testing.T) {
t.Fatal(err)
}
node.GetRouter().Recv()
hostState := <-node.GetHostStateChannel()
t.Log(hostState)
}