- 添加 b2s/s2b 零分配字节-字符串转换工具函数 - WebSocket 数据转发使用 sync.Pool 复用 32KB buffer - 条件化 Debug 日志避免非 Debug 级别的字符串分配 - 缓存键哈希计算直接写入 []byte 避免 string 转换 - 使用 bytes.EqualFold 替代 strings.ToLower 进行大小写不敏感比较 - generateETag 使用 strconv.AppendInt 避免 fmt.Sprintf - 支持 Dial timeout 配置,区分 TCP 连接建立和总连接超时 - MaxConnsPerHost 默认值改为 512(fasthttp 推荐) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
37 lines
917 B
Go
37 lines
917 B
Go
package proxy
|
|
|
|
import (
|
|
"bytes"
|
|
"unsafe"
|
|
)
|
|
|
|
// b2s converts byte slice to string without allocation.
|
|
// WARNING: The returned string shares memory with the original slice.
|
|
// Do not modify the slice after calling this function.
|
|
func b2s(b []byte) string {
|
|
if len(b) == 0 {
|
|
return ""
|
|
}
|
|
return unsafe.String(&b[0], len(b))
|
|
}
|
|
|
|
// s2b converts string to byte slice without allocation.
|
|
// WARNING: The returned slice shares memory with the original string.
|
|
// Do not modify the slice contents.
|
|
func s2b(s string) []byte {
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
return unsafe.Slice(unsafe.StringData(s), len(s))
|
|
}
|
|
|
|
// isInWhitelist checks if a header key is in the whitelist.
|
|
// Uses bytes.EqualFold for case-insensitive comparison without allocation.
|
|
func isInWhitelist(key []byte, whitelist map[string]bool) bool {
|
|
for wKey := range whitelist {
|
|
if bytes.EqualFold(key, s2b(wKey)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
} |