-
Notifications
You must be signed in to change notification settings - Fork 985
chore: replace wsconncache with a single tailnet #8176
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
Changes from 1 commit
7222135
d4181b0
2a133d1
aa6bcb6
f9040fc
a988fef
2e201dc
5901f68
311ea2b
30aefcb
e55e146
457470d
2c32434
11f2805
a88be66
49a8364
5e4b631
8ef2fb7
9864d89
dd3cc15
8896ae4
be4db71
7bfac9b
10d44fe
ef44092
9a29d85
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,8 +34,6 @@ func init() { | |
} | ||
} | ||
|
||
// TODO(coadler): ServerTailnet does not currently remove stale peers. | ||
|
||
// NewServerTailnet creates a new tailnet intended for use by coderd. It | ||
// automatically falls back to wsconncache if a legacy agent is encountered. | ||
func NewServerTailnet( | ||
|
@@ -102,14 +100,49 @@ func NewServerTailnet( | |
}) | ||
|
||
go tn.watchAgentUpdates() | ||
go tn.expireOldAgents() | ||
return tn, nil | ||
} | ||
|
||
func (s *ServerTailnet) expireOldAgents() { | ||
const ( | ||
tick = 5 * time.Minute | ||
cutoff = 30 * time.Minute | ||
) | ||
|
||
ticker := time.NewTicker(tick) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-s.ctx.Done(): | ||
return | ||
case <-ticker.C: | ||
} | ||
|
||
s.nodesMu.Lock() | ||
agentConn := s.getAgentConn() | ||
for agentID, node := range s.agentNodes { | ||
if time.Since(node.lastConnection) > cutoff { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This measures the last time we started a connection, not the last time the connection was used. If we proxy a long-lived connection like We might need some ref-counting to keep track of the connections to each agent, so that we expire them when they are no longer used. |
||
err := agentConn.UnsubscribeAgent(agentID) | ||
if err != nil { | ||
s.logger.Error(s.ctx, "unsubscribe expired agent", slog.Error(err), slog.F("agent_id", agentID)) | ||
} | ||
delete(s.agentNodes, agentID) | ||
|
||
// TODO(coadler): actually remove from the netmap | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do this before merge |
||
} | ||
} | ||
s.nodesMu.Unlock() | ||
} | ||
} | ||
|
||
func (s *ServerTailnet) watchAgentUpdates() { | ||
for { | ||
nodes := s.getAgentConn().NextUpdate(s.ctx) | ||
if nodes == nil { | ||
if s.getAgentConn().IsClosed() && s.ctx.Err() == nil { | ||
conn := s.getAgentConn() | ||
nodes, ok := conn.NextUpdate(s.ctx) | ||
if !ok { | ||
if conn.IsClosed() && s.ctx.Err() == nil { | ||
s.reinitCoordinator() | ||
continue | ||
} | ||
|
@@ -129,24 +162,22 @@ func (s *ServerTailnet) getAgentConn() tailnet.MultiAgentConn { | |
} | ||
|
||
func (s *ServerTailnet) reinitCoordinator() { | ||
s.nodesMu.Lock() | ||
agentConn := (*s.coord.Load()).ServeMultiAgent(uuid.New()) | ||
s.agentConn.Store(&agentConn) | ||
|
||
s.nodesMu.Lock() | ||
// Resubscribe to all of the agents we're tracking. | ||
for agentID, agentNode := range s.agentNodes { | ||
closer, err := agentConn.SubscribeAgent(agentID) | ||
for agentID := range s.agentNodes { | ||
err := agentConn.SubscribeAgent(agentID) | ||
if err != nil { | ||
s.logger.Warn(s.ctx, "resubscribe to agent", slog.Error(err), slog.F("agent_id", agentID)) | ||
} | ||
agentNode.close = closer | ||
} | ||
s.nodesMu.Unlock() | ||
} | ||
|
||
type tailnetNode struct { | ||
lastConnection time.Time | ||
close func() | ||
} | ||
|
||
type ServerTailnet struct { | ||
|
@@ -210,13 +241,12 @@ func (s *ServerTailnet) ensureAgent(agentID uuid.UUID) error { | |
// If we don't have the node, subscribe. | ||
if !ok { | ||
s.logger.Debug(s.ctx, "subscribing to agent", slog.F("agent_id", agentID)) | ||
closer, err := s.getAgentConn().SubscribeAgent(agentID) | ||
err := s.getAgentConn().SubscribeAgent(agentID) | ||
if err != nil { | ||
return xerrors.Errorf("subscribe agent: %w", err) | ||
} | ||
tnode = &tailnetNode{ | ||
lastConnection: time.Now(), | ||
close: closer, | ||
} | ||
s.agentNodes[agentID] = tnode | ||
} else { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although maybe a bit weird, I'd argue we should put this in
workspaceagents.go
sincewsconncache
will be going away.