Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: pass OnSubscribe to HA MultiAgent
Fixes #9929
  • Loading branch information
coadler committed Sep 29, 2023
commit aad3842c0914c1c46471e13cc0e684fcf9ab5949
41 changes: 28 additions & 13 deletions enterprise/tailnet/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ func (c *haCoordinator) ServeMultiAgent(id uuid.UUID) agpl.MultiAgentConn {
ID: id,
AgentIsLegacyFunc: c.agentIsLegacy,
OnSubscribe: c.clientSubscribeToAgent,
OnUnsubscribe: c.clientUnsubscribeFromAgent,
OnNodeUpdate: c.clientNodeUpdate,
OnRemove: func(enq agpl.Queue) { c.clientDisconnected(enq.UniqueID()) },
OnRemove: c.clientDisconnected,
}).Init()
c.addClient(id, m)
return m
Expand Down Expand Up @@ -101,6 +102,22 @@ func (c *haCoordinator) clientSubscribeToAgent(enq agpl.Queue, agentID uuid.UUID
return nil, nil
}

func (c *haCoordinator) clientUnsubscribeFromAgent(enq agpl.Queue, agentID uuid.UUID) error {
c.mutex.Lock()
defer c.mutex.Unlock()

connectionSockets, ok := c.agentToConnectionSockets[agentID]
if !ok {
return nil
}
delete(connectionSockets, enq.UniqueID())
if len(connectionSockets) == 0 {
delete(c.agentToConnectionSockets, agentID)
}

return nil
}

type haCoordinator struct {
id uuid.UUID
log slog.Logger
Expand Down Expand Up @@ -161,7 +178,7 @@ func (c *haCoordinator) ServeClient(conn net.Conn, id, agentID uuid.UUID) error
defer tc.Close()

c.addClient(id, tc)
defer c.clientDisconnected(id)
defer c.clientDisconnected(tc)

agentNode, err := c.clientSubscribeToAgent(tc, agentID)
if err != nil {
Expand Down Expand Up @@ -200,26 +217,24 @@ func (c *haCoordinator) initOrSetAgentConnectionSocketLocked(agentID uuid.UUID,
c.clientsToAgents[enq.UniqueID()][agentID] = c.agentSockets[agentID]
}

func (c *haCoordinator) clientDisconnected(id uuid.UUID) {
func (c *haCoordinator) clientDisconnected(enq agpl.Queue) {
c.mutex.Lock()
defer c.mutex.Unlock()

for agentID := range c.clientsToAgents[id] {
// Clean all traces of this connection from the map.
delete(c.nodes, id)
for agentID := range c.clientsToAgents[enq.UniqueID()] {
connectionSockets, ok := c.agentToConnectionSockets[agentID]
if !ok {
return
continue
}
delete(connectionSockets, id)
if len(connectionSockets) != 0 {
return
delete(connectionSockets, enq.UniqueID())
if len(connectionSockets) == 0 {
delete(c.agentToConnectionSockets, agentID)
}
delete(c.agentToConnectionSockets, agentID)
}

delete(c.clients, id)
delete(c.clientsToAgents, id)
delete(c.nodes, enq.UniqueID())
delete(c.clients, enq.UniqueID())
delete(c.clientsToAgents, enq.UniqueID())
}

func (c *haCoordinator) handleNextClientMessage(id uuid.UUID, decoder *json.Decoder) error {
Expand Down