fix(proxy): 修复 WebSocket 升级失败时的资源泄漏
在升级失败(状态码非 101)和写入响应失败时,确保 resp.Body.Close()。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
1ab49af76d
commit
8180ee9bea
@ -393,14 +393,21 @@ func WebSocket(ctx *fasthttp.RequestCtx, target *loadbalance.Target, timeout tim
|
||||
|
||||
// 步骤5: 检查响应状态码(期望 101 Switching Protocols)
|
||||
if resp.StatusCode != http.StatusSwitchingProtocols {
|
||||
// 关闭响应 body(升级失败时)
|
||||
_ = resp.Body.Close()
|
||||
return fmt.Errorf("backend rejected WebSocket upgrade: %s", resp.Status)
|
||||
}
|
||||
|
||||
// 步骤6: 将升级响应发送回客户端
|
||||
if err := writeUpgradeResponse(clientConn, resp); err != nil {
|
||||
// 关闭响应 body(写入失败时)
|
||||
_ = resp.Body.Close()
|
||||
return fmt.Errorf("failed to send upgrade response to client: %w", err)
|
||||
}
|
||||
|
||||
// 注意: WebSocket 升级成功后,resp.Body 不需要显式关闭
|
||||
// 因为底层连接已被 bridge 用于双向数据传输
|
||||
|
||||
// 步骤7: 启动桥接(阻塞直到连接关闭)
|
||||
return bridge.Bridge()
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user