Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf295200

Browse files
authored
fix: Ensure WebSockets routinely transfer data (#4367)
Fixes#4351.
1 parenta6bb3b2 commitf295200

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

‎coderd/httpapi/websocket.go

Lines changed: 27 additions & 0 deletions
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+
funcHeartbeat(ctx context.Context,conn*websocket.Conn) {
14+
ticker:=time.NewTicker(15*time.Second)
15+
deferticker.Stop()
16+
for {
17+
select {
18+
case<-ctx.Done():
19+
return
20+
case<-ticker.C:
21+
}
22+
err:=conn.Ping(ctx)
23+
iferr!=nil {
24+
return
25+
}
26+
}
27+
}

‎coderd/provisionerjobs.go

Lines changed: 1 addition & 0 deletions
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+
gohttpapi.Heartbeat(ctx,conn)
154155

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

‎coderd/workspaceagents.go

Lines changed: 8 additions & 0 deletions
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+
gohttpapi.Heartbeat(ctx,conn)
198199

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

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

583588
returnworkspaceAgent,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+
gohttpapi.Heartbeat(ctx,conn)
638+
631639
deferconn.Close(websocket.StatusGoingAway,"")
632640

633641
varlastReport codersdk.AgentStatsReportResponse

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp