Skip to content

feat: HA tailnet coordinator #4170

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

Closed
wants to merge 12 commits into from
Prev Previous commit
Next Next commit
Merge branch 'main' into colin/pg-coordinate
  • Loading branch information
coadler committed Oct 7, 2022
commit 02e079dbb656e733c66f15c62a63aa2aa37630cb
1 change: 0 additions & 1 deletion codersdk/workspaceagents.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"tailscale.com/tailcfg"

"cdr.dev/slog"
"github.com/coder/coder/agent"
"github.com/coder/coder/tailnet"
"github.com/coder/retry"
)
Expand Down
30 changes: 11 additions & 19 deletions tailnet/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,32 +195,21 @@ func (c *memoryCoordinator) handleNextClientMessage(id, agent uuid.UUID, decoder
}

c.mutex.Lock()
defer c.mutex.Unlock()

// Update the node of this client in our in-memory map. If an agent
// entirely shuts down and reconnects, it needs to be aware of all clients
// attempting to establish connections.
// Update the node of this client in our in-memory map. If an agent entirely
// shuts down and reconnects, it needs to be aware of all clients attempting
// to establish connections.
c.nodes[id] = &node

// Write the new node from this client to the actively
// connected agent.
err = c.writeNodeToAgent(agent, &node)
if err != nil {
return xerrors.Errorf("write node to agent: %w", err)
}

return nil
}

func (c *memoryCoordinator) writeNodeToAgent(agent uuid.UUID, node *Node) error {
agentSocket, ok := c.agentSockets[agent]
if !ok {
c.mutex.Unlock()
return nil
}
c.mutex.Unlock()

// Write the new node from this client to the actively
// connected agent.
data, err := json.Marshal([]*Node{node})
// Write the new node from this client to the actively connected agent.
data, err := json.Marshal([]*Node{&node})
if err != nil {
return xerrors.Errorf("marshal nodes: %w", err)
}
Expand All @@ -232,6 +221,7 @@ func (c *memoryCoordinator) writeNodeToAgent(agent uuid.UUID, node *Node) error
}
return xerrors.Errorf("write json: %w", err)
}

return nil
}

Expand Down Expand Up @@ -299,16 +289,17 @@ func (c *memoryCoordinator) handleNextAgentMessage(id uuid.UUID, decoder *json.D
}

c.mutex.Lock()
defer c.mutex.Unlock()

c.nodes[id] = &node
connectionSockets, ok := c.agentToConnectionSockets[id]
if !ok {
c.mutex.Unlock()
return nil
}

data, err := json.Marshal([]*Node{&node})
if err != nil {
c.mutex.Unlock()
return xerrors.Errorf("marshal nodes: %w", err)
}

Expand All @@ -324,6 +315,7 @@ func (c *memoryCoordinator) handleNextAgentMessage(id uuid.UUID, decoder *json.D
}()
}

c.mutex.Unlock()
wg.Wait()
return nil
}
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.