perf(proxy): eliminate string allocations in isWebSocketRequest

Replace string(connection)/strings.EqualFold/strings.ToLower with
bytes.EqualFold and utils.BytesContainsFold. Removes 2-4 heap
allocations per proxied request.
This commit is contained in:
xfy 2026-06-04 10:48:57 +08:00
parent 613c5f8ff0
commit 02775de641

View File

@ -977,16 +977,16 @@ func (p *Proxy) selectTarget(ctx *fasthttp.RequestCtx) *loadbalance.Target {
// 返回值: // 返回值:
// - bool: true 表示是 WebSocket 升级请求 // - bool: true 表示是 WebSocket 升级请求
func isWebSocketRequest(ctx *fasthttp.RequestCtx) bool { func isWebSocketRequest(ctx *fasthttp.RequestCtx) bool {
// 检查 Connection 请求头
connection := ctx.Request.Header.Peek("Connection") connection := ctx.Request.Header.Peek("Connection")
if !strings.EqualFold(string(connection), "upgrade") { if len(connection) == 0 {
// 也检查 "Upgrade" 子串(例如 "keep-alive, Upgrade" return false
if !strings.Contains(strings.ToLower(string(connection)), "upgrade") { }
return false upgradeBytes := []byte("upgrade")
} if !bytes.EqualFold(connection, upgradeBytes) &&
!utils.BytesContainsFold(connection, upgradeBytes) {
return false
} }
// 检查 Upgrade 请求头
upgrade := ctx.Request.Header.Peek("Upgrade") upgrade := ctx.Request.Header.Peek("Upgrade")
return strings.EqualFold(string(upgrade), "websocket") return bytes.EqualFold(upgrade, []byte("websocket"))
} }