MCPcopy Create free account
hub / github.com/coder/coder / processSnapshotUpdate

Function processSnapshotUpdate

vpn/tunnel.go:673–706  ·  view source on GitHub ↗

processSnapshotUpdate handles the logic when a full state update is received. When the tunnel is live, we only receive diffs, but the first packet on any given reconnect to the tailnet API is a full state. Without this logic we weren't processing deletes for any workspaces or agents deleted while th

(update *tailnet.WorkspaceUpdate, agents map[uuid.UUID]agentWithPing, workspaces map[uuid.UUID]tailnet.Workspace)

Source from the content-addressed store, hash-verified

671// Without this logic we weren't processing deletes for any workspaces or agents deleted
672// while the client was disconnected while the computer was asleep.
673func processSnapshotUpdate(update *tailnet.WorkspaceUpdate, agents map[uuid.UUID]agentWithPing, workspaces map[uuid.UUID]tailnet.Workspace) {
674 // ignoredWorkspaces is initially populated with the workspaces that are
675 // in the current update. Later on we populate it with the deleted workspaces too
676 // so that we don't send duplicate updates. Same applies to ignoredAgents.
677 ignoredWorkspaces := make(map[uuid.UUID]struct{}, len(update.UpsertedWorkspaces))
678 ignoredAgents := make(map[uuid.UUID]struct{}, len(update.UpsertedAgents))
679
680 for _, workspace := range update.UpsertedWorkspaces {
681 ignoredWorkspaces[workspace.ID] = struct{}{}
682 }
683 for _, agent := range update.UpsertedAgents {
684 ignoredAgents[agent.ID] = struct{}{}
685 }
686 for _, agent := range agents {
687 if _, present := ignoredAgents[agent.ID]; !present {
688 // delete any current agents that are not in the new update
689 update.DeletedAgents = append(update.DeletedAgents, &tailnet.Agent{
690 ID: agent.ID,
691 Name: agent.Name,
692 WorkspaceID: agent.WorkspaceID,
693 })
694 }
695 }
696 for _, workspace := range workspaces {
697 if _, present := ignoredWorkspaces[workspace.ID]; !present {
698 update.DeletedWorkspaces = append(update.DeletedWorkspaces, &tailnet.Workspace{
699 ID: workspace.ID,
700 Name: workspace.Name,
701 Status: workspace.Status,
702 })
703 ignoredWorkspaces[workspace.ID] = struct{}{}
704 }
705 }
706}
707
708// hostsToIPStrings returns a slice of all unique IP addresses in the values
709// of the given map.

Callers 2

TestProcessFreshStateFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestProcessFreshStateFunction · 0.68