Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ca8c17

Browse files
committed
Address PR comments
1 parent 81d9fef commit 5ca8c17

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

coderd/database/databasefake/databasefake.go

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -899,18 +899,8 @@ func (q *fakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
899899

900900
var hasAgentMatched bool
901901
for _, wa := range workspaceAgents {
902-
switch arg.HasAgent {
903-
case "connected":
904-
hasAgentMatched = wa.LastConnectedAt.Valid
905-
case "connecting":
906-
hasAgentMatched = !wa.FirstConnectedAt.Valid
907-
case "disconnected":
908-
hasAgentMatched = (wa.DisconnectedAt.Valid && wa.DisconnectedAt.Time.After(wa.LastConnectedAt.Time)) ||
909-
(wa.LastConnectedAt.Valid && wa.LastConnectedAt.Time.Add(time.Duration(arg.AgentInactiveDisconnectTimeoutSeconds)*time.Second).Before(database.Now()))
910-
case "timeout":
911-
hasAgentMatched = !wa.FirstConnectedAt.Valid &&
912-
wa.CreatedAt.Add(time.Duration(wa.ConnectionTimeoutSeconds)*time.Second).Before(database.Now())
913-
}
902+
mapped := mapAgentStatus(wa, arg.AgentInactiveDisconnectTimeoutSeconds)
903+
hasAgentMatched = mapped == arg.HasAgent
914904
break // only 1 agent is expected
915905
}
916906

@@ -957,6 +947,38 @@ func (q *fakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
957947
return convertToWorkspaceRows(workspaces, int64(beforePageCount)), nil
958948
}
959949

950+
func mapAgentStatus(dbAgent database.WorkspaceAgent, agentInactiveDisconnectTimeoutSeconds int64) string {
951+
var status string
952+
connectionTimeout := time.Duration(dbAgent.ConnectionTimeoutSeconds) * time.Second
953+
switch {
954+
case !dbAgent.FirstConnectedAt.Valid:
955+
switch {
956+
case connectionTimeout > 0 && database.Now().Sub(dbAgent.CreatedAt) > connectionTimeout:
957+
// If the agent took too long to connect the first time,
958+
// mark it as timed out.
959+
status = "timeout"
960+
default:
961+
// If the agent never connected, it's waiting for the compute
962+
// to start up.
963+
status = "connecting"
964+
}
965+
case dbAgent.DisconnectedAt.Time.After(dbAgent.LastConnectedAt.Time):
966+
// If we've disconnected after our last connection, we know the
967+
// agent is no longer connected.
968+
status = "disconnected"
969+
case database.Now().Sub(dbAgent.LastConnectedAt.Time) > time.Duration(agentInactiveDisconnectTimeoutSeconds)*time.Second:
970+
// The connection died without updating the last connected.
971+
status = "disconnected"
972+
case dbAgent.LastConnectedAt.Valid:
973+
// The agent should be assumed connected if it's under inactivity timeouts
974+
// and last connected at has been properly set.
975+
status = "connected"
976+
default:
977+
panic("unknown agent status: " + status)
978+
}
979+
return status
980+
}
981+
960982
func convertToWorkspaceRows(workspaces []database.Workspace, count int64) []database.GetWorkspacesRow {
961983
rows := make([]database.GetWorkspacesRow, len(workspaces))
962984
for i, w := range workspaces {

coderd/database/queries.sql.go

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaces.sql

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ WHERE
187187
WHEN @has_agent = 'timeout' THEN
188188
latest_build.agent_first_connected_at IS NULL AND (latest_build.agent_created_at + latest_build.agent_connection_timeout_seconds * INTERVAL '1 second' < NOW())
189189
WHEN @has_agent = 'connecting' THEN
190-
latest_build.agent_first_connected_at IS NULL
190+
latest_build.agent_first_connected_at IS NULL AND (latest_build.agent_created_at + latest_build.agent_connection_timeout_seconds * INTERVAL '1 second' >= NOW())
191191
WHEN @has_agent = 'disconnected' THEN
192192
(
193193
latest_build.agent_disconnected_at IS NOT NULL AND
@@ -197,7 +197,13 @@ WHERE
197197
latest_build.agent_last_connected_at + INTERVAL '1 second' * @agent_inactive_disconnect_timeout_seconds :: bigint < NOW()
198198
)
199199
WHEN @has_agent = 'connected' THEN
200-
latest_build.agent_last_connected_at IS NOT NULL
200+
(
201+
latest_build.agent_disconnected_at IS NOT NULL AND
202+
latest_build.agent_disconnected_at <= latest_build.agent_last_connected_at
203+
) OR (
204+
latest_build.agent_last_connected_at IS NOT NULL AND
205+
latest_build.agent_last_connected_at + INTERVAL '1 second' * @agent_inactive_disconnect_timeout_seconds :: bigint >= NOW()
206+
)
201207
ELSE true
202208
END
203209
ELSE true

0 commit comments

Comments
 (0)