xfy edf192ff85 docs(proxy,resolver,variable): 增强公开 API 文档注释
- proxy: 添加 upstreamCache/protoHTTPS 常量说明
- proxy_dns: 添加文件级文档和所有公开方法的详细注释
- stats: 增强 StatsCollector 接口文档和方法注释
- pool: 增强 PoolStats 字段注释和公开函数文档

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-10 15:29:21 +08:00

90 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package variable 提供 sync.Pool 用于复用 Context减少 GC 压力。
//
// 包含池统计信息、Get/Put 包装方法和统计重置功能。
//
// 作者xfy
package variable
import (
"sync"
"github.com/valyala/fasthttp"
)
// PoolStats 池统计信息。
//
// 记录 sync.Pool 的使用统计,用于监控和调试。
type PoolStats struct {
// Gets 从池中获取对象的次数
Gets int64
// Puts 放回池中对象的次数
Puts int64
// NewCount 调用 New 函数创建对象的次数
NewCount int64
// Active 当前活跃对象数量Gets - Puts
Active int64
}
var (
// stats 全局池统计信息
stats PoolStats
// statsMu 保护统计信息的读写锁
statsMu sync.RWMutex
)
// GetStats 获取池统计信息的副本。
//
// 返回当前统计信息的快照,线程安全。
func GetStats() PoolStats {
statsMu.RLock()
s := stats
statsMu.RUnlock()
return s
}
// GetPool 获取底层的 sync.Pool用于测试和调试
func GetPool() *sync.Pool {
return &pool
}
// PoolGet 从池中获取 Context包装方法用于统计
//
// Deprecated: 使用 NewContext 代替,该函数保持向后兼容。
func PoolGet(ctx *fasthttp.RequestCtx) *Context {
vc := NewContext(ctx)
// 更新统计
statsMu.Lock()
stats.Gets++
stats.Active = stats.Gets - stats.Puts
statsMu.Unlock()
return vc
}
// PoolPut 将 Context 放回池中(包装方法,用于统计)
//
// Deprecated: 使用 ReleaseContext 代替,该函数保持向后兼容。
func PoolPut(vc *Context) {
if vc == nil {
return
}
ReleaseContext(vc)
// 更新统计
statsMu.Lock()
stats.Puts++
stats.Active = stats.Gets - stats.Puts
statsMu.Unlock()
}
// ResetStats 重置统计信息。
//
// 将所有统计计数器清零,线程安全。
func ResetStats() {
statsMu.Lock()
stats = PoolStats{}
statsMu.Unlock()
}