Skip to content

fix: pass OnUnsubscribe to HA MultiAgent #9947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2023
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
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