|
7 | 7 | "errors"
|
8 | 8 | "fmt"
|
9 | 9 | "io"
|
10 |
| - "net" |
11 | 10 | "net/http"
|
12 | 11 | "net/url"
|
13 | 12 | "sort"
|
@@ -544,7 +543,7 @@ func (api *API) workspaceAgentLogs(rw http.ResponseWriter, r *http.Request) {
|
544 | 543 | }
|
545 | 544 | go httpapi.Heartbeat(ctx, conn)
|
546 | 545 |
|
547 |
| - ctx, wsNetConn := websocketNetConn(ctx, conn, websocket.MessageText) |
| 546 | + ctx, wsNetConn := codersdk.WebsocketNetConn(ctx, conn, websocket.MessageText) |
548 | 547 | defer wsNetConn.Close() // Also closes conn.
|
549 | 548 |
|
550 | 549 | // The Go stdlib JSON encoder appends a newline character after message write.
|
@@ -881,7 +880,7 @@ func (api *API) derpMapUpdates(rw http.ResponseWriter, r *http.Request) {
|
881 | 880 | })
|
882 | 881 | return
|
883 | 882 | }
|
884 |
| - ctx, nconn := websocketNetConn(ctx, ws, websocket.MessageBinary) |
| 883 | + ctx, nconn := codersdk.WebsocketNetConn(ctx, ws, websocket.MessageBinary) |
885 | 884 | defer nconn.Close()
|
886 | 885 |
|
887 | 886 | // Slurp all packets from the connection into io.Discard so pongs get sent
|
@@ -990,7 +989,7 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
|
990 | 989 | return
|
991 | 990 | }
|
992 | 991 |
|
993 |
| - ctx, wsNetConn := websocketNetConn(ctx, conn, websocket.MessageBinary) |
| 992 | + ctx, wsNetConn := codersdk.WebsocketNetConn(ctx, conn, websocket.MessageBinary) |
994 | 993 | defer wsNetConn.Close()
|
995 | 994 |
|
996 | 995 | closeCtx, closeCtxCancel := context.WithCancel(ctx)
|
@@ -1077,7 +1076,7 @@ func (api *API) workspaceAgentClientCoordinate(rw http.ResponseWriter, r *http.R
|
1077 | 1076 | })
|
1078 | 1077 | return
|
1079 | 1078 | }
|
1080 |
| - ctx, wsNetConn := websocketNetConn(ctx, conn, websocket.MessageBinary) |
| 1079 | + ctx, wsNetConn := codersdk.WebsocketNetConn(ctx, conn, websocket.MessageBinary) |
1081 | 1080 | defer wsNetConn.Close()
|
1082 | 1081 |
|
1083 | 1082 | go httpapi.Heartbeat(ctx, conn)
|
@@ -2104,47 +2103,6 @@ func createExternalAuthResponse(typ, token string, extra pqtype.NullRawMessage)
|
2104 | 2103 | return resp, err
|
2105 | 2104 | }
|
2106 | 2105 |
|
2107 |
| -// wsNetConn wraps net.Conn created by websocket.NetConn(). Cancel func |
2108 |
| -// is called if a read or write error is encountered. |
2109 |
| -type wsNetConn struct { |
2110 |
| - cancel context.CancelFunc |
2111 |
| - net.Conn |
2112 |
| -} |
2113 |
| - |
2114 |
| -func (c *wsNetConn) Read(b []byte) (n int, err error) { |
2115 |
| - n, err = c.Conn.Read(b) |
2116 |
| - if err != nil { |
2117 |
| - c.cancel() |
2118 |
| - } |
2119 |
| - return n, err |
2120 |
| -} |
2121 |
| - |
2122 |
| -func (c *wsNetConn) Write(b []byte) (n int, err error) { |
2123 |
| - n, err = c.Conn.Write(b) |
2124 |
| - if err != nil { |
2125 |
| - c.cancel() |
2126 |
| - } |
2127 |
| - return n, err |
2128 |
| -} |
2129 |
| - |
2130 |
| -func (c *wsNetConn) Close() error { |
2131 |
| - defer c.cancel() |
2132 |
| - return c.Conn.Close() |
2133 |
| -} |
2134 |
| - |
2135 |
| -// websocketNetConn wraps websocket.NetConn and returns a context that |
2136 |
| -// is tied to the parent context and the lifetime of the conn. Any error |
2137 |
| -// during read or write will cancel the context, but not close the |
2138 |
| -// conn. Close should be called to release context resources. |
2139 |
| -func websocketNetConn(ctx context.Context, conn *websocket.Conn, msgType websocket.MessageType) (context.Context, net.Conn) { |
2140 |
| - ctx, cancel := context.WithCancel(ctx) |
2141 |
| - nc := websocket.NetConn(ctx, conn, msgType) |
2142 |
| - return ctx, &wsNetConn{ |
2143 |
| - cancel: cancel, |
2144 |
| - Conn: nc, |
2145 |
| - } |
2146 |
| -} |
2147 |
| - |
2148 | 2106 | func convertWorkspaceAgentLogs(logs []database.WorkspaceAgentLog) []codersdk.WorkspaceAgentLog {
|
2149 | 2107 | sdk := make([]codersdk.WorkspaceAgentLog, 0, len(logs))
|
2150 | 2108 | for _, logEntry := range logs {
|
|
0 commit comments