|
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) |
@@ -2108,47 +2107,6 @@ func createExternalAuthResponse(typ, token string, extra pqtype.NullRawMessage) |
2108 | 2107 | return resp, err |
2109 | 2108 | } |
2110 | 2109 |
|
2111 | | -// wsNetConn wraps net.Conn created by websocket.NetConn(). Cancel func |
2112 | | -// is called if a read or write error is encountered. |
2113 | | -type wsNetConn struct { |
2114 | | - cancel context.CancelFunc |
2115 | | - net.Conn |
2116 | | -} |
2117 | | - |
2118 | | -func (c *wsNetConn) Read(b []byte) (n int, err error) { |
2119 | | - n, err = c.Conn.Read(b) |
2120 | | - if err != nil { |
2121 | | - c.cancel() |
2122 | | - } |
2123 | | - return n, err |
2124 | | -} |
2125 | | - |
2126 | | -func (c *wsNetConn) Write(b []byte) (n int, err error) { |
2127 | | - n, err = c.Conn.Write(b) |
2128 | | - if err != nil { |
2129 | | - c.cancel() |
2130 | | - } |
2131 | | - return n, err |
2132 | | -} |
2133 | | - |
2134 | | -func (c *wsNetConn) Close() error { |
2135 | | - defer c.cancel() |
2136 | | - return c.Conn.Close() |
2137 | | -} |
2138 | | - |
2139 | | -// websocketNetConn wraps websocket.NetConn and returns a context that |
2140 | | -// is tied to the parent context and the lifetime of the conn. Any error |
2141 | | -// during read or write will cancel the context, but not close the |
2142 | | -// conn. Close should be called to release context resources. |
2143 | | -func websocketNetConn(ctx context.Context, conn *websocket.Conn, msgType websocket.MessageType) (context.Context, net.Conn) { |
2144 | | - ctx, cancel := context.WithCancel(ctx) |
2145 | | - nc := websocket.NetConn(ctx, conn, msgType) |
2146 | | - return ctx, &wsNetConn{ |
2147 | | - cancel: cancel, |
2148 | | - Conn: nc, |
2149 | | - } |
2150 | | -} |
2151 | | - |
2152 | 2110 | func convertWorkspaceAgentLogs(logs []database.WorkspaceAgentLog) []codersdk.WorkspaceAgentLog { |
2153 | 2111 | sdk := make([]codersdk.WorkspaceAgentLog, 0, len(logs)) |
2154 | 2112 | for _, logEntry := range logs { |
|
0 commit comments