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

Skip to content

Commit 44564c5

Browse files
committed
fix: Ensure WebSockets routinely transfer data
Fixes #4351.
1 parent 45c05a0 commit 44564c5

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

coderd/httpapi/websocket.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package httpapi
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"nhooyr.io/websocket"
8+
)
9+
10+
// Heartbeat loops to ping a WebSocket to keep it alive.
11+
// Default idle connection timeouts are typically 60 seconds.
12+
// See: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/application-load-balancers.html#connection-idle-timeout
13+
func Heartbeat(ctx context.Context, conn *websocket.Conn) {
14+
ticker := time.NewTicker(15 * time.Second)
15+
defer ticker.Stop()
16+
for {
17+
select {
18+
case <-ctx.Done():
19+
return
20+
case <-ticker.C:
21+
}
22+
err := conn.Ping(ctx)
23+
if err != nil {
24+
return
25+
}
26+
}
27+
}

coderd/provisionerjobs.go

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func (api *API) provisionerJobLogs(rw http.ResponseWriter, r *http.Request, job
151151
})
152152
return
153153
}
154+
go httpapi.Heartbeat(ctx, conn)
154155

155156
ctx, wsNetConn := websocketNetConn(ctx, conn, websocket.MessageText)
156157
defer wsNetConn.Close() // Also closes conn.

coderd/workspaceagents.go

+8
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ func (api *API) workspaceAgentPTY(rw http.ResponseWriter, r *http.Request) {
195195
})
196196
return
197197
}
198+
go httpapi.Heartbeat(ctx, conn)
198199

199200
_, wsNetConn := websocketNetConn(ctx, conn, websocket.MessageBinary)
200201
defer wsNetConn.Close() // Also closes conn.
@@ -356,6 +357,8 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
356357
})
357358
return
358359
}
360+
go httpapi.Heartbeat(ctx, conn)
361+
359362
ctx, wsNetConn := websocketNetConn(ctx, conn, websocket.MessageBinary)
360363
defer wsNetConn.Close()
361364

@@ -477,6 +480,8 @@ func (api *API) workspaceAgentClientCoordinate(rw http.ResponseWriter, r *http.R
477480
})
478481
return
479482
}
483+
go httpapi.Heartbeat(ctx, conn)
484+
480485
defer conn.Close(websocket.StatusNormalClosure, "")
481486
err = api.TailnetCoordinator.ServeClient(websocket.NetConn(ctx, conn, websocket.MessageBinary), uuid.New(), workspaceAgent.ID)
482487
if err != nil {
@@ -582,6 +587,7 @@ func convertWorkspaceAgent(derpMap *tailcfg.DERPMap, coordinator *tailnet.Coordi
582587

583588
return workspaceAgent, nil
584589
}
590+
585591
func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Request) {
586592
ctx := r.Context()
587593

@@ -628,6 +634,8 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
628634
})
629635
return
630636
}
637+
go httpapi.Heartbeat(ctx, conn)
638+
631639
defer conn.Close(websocket.StatusGoingAway, "")
632640

633641
var lastReport codersdk.AgentStatsReportResponse

0 commit comments

Comments
 (0)