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

Skip to content

Commit 0565611

Browse files
committed
fix: agent stats websocket blocking until next interval
For `httpapi.Heartbeat` to work properly, a call to `wsjson.Read` must always be in flight, otherwise pings can't be exchanged. This caused the ping loop to hang until the next stat interval. Since the loop was blocking until the next interval it would also never exit until it fired.
1 parent d2ee18c commit 0565611

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

coderd/workspaceagents.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -757,18 +757,30 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
757757

758758
// Allow overriding the stat interval for debugging and testing purposes.
759759
timer := time.NewTicker(api.AgentStatsRefreshInterval)
760-
for {
761-
err := wsjson.Write(ctx, conn, codersdk.AgentStatsReportRequest{})
762-
if err != nil {
763-
api.Logger.Debug(ctx, "write report request", slog.Error(err))
764-
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("write report request: %s", err))
765-
return
760+
defer timer.Stop()
761+
762+
go func() {
763+
for {
764+
err = wsjson.Write(ctx, conn, codersdk.AgentStatsReportRequest{})
765+
if err != nil {
766+
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("write report request: %s", err))
767+
return
768+
}
769+
770+
select {
771+
case <-timer.C:
772+
continue
773+
case <-ctx.Done():
774+
conn.Close(websocket.StatusNormalClosure, "")
775+
return
776+
}
766777
}
767-
var rep codersdk.AgentStatsReportResponse
778+
}()
768779

780+
for {
781+
var rep codersdk.AgentStatsReportResponse
769782
err = wsjson.Read(ctx, conn, &rep)
770783
if err != nil {
771-
api.Logger.Debug(ctx, "read report response", slog.Error(err))
772784
conn.Close(websocket.StatusInternalError, httpapi.WebsocketCloseSprintf("read report response: %s", err))
773785
return
774786
}
@@ -827,14 +839,6 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
827839
return
828840
}
829841
}
830-
831-
select {
832-
case <-timer.C:
833-
continue
834-
case <-ctx.Done():
835-
conn.Close(websocket.StatusNormalClosure, "")
836-
return
837-
}
838842
}
839843
}
840844

0 commit comments

Comments
 (0)