使用 sync/atomic 替代 sync.RWMutex,消除统计操作中的锁开销, 提升并发性能。保持 PoolStats 公开 API 不变(返回 plain int64)。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
94 lines
2.0 KiB
Go
94 lines
2.0 KiB
Go
// Package variable 提供 sync.Pool 用于复用 Context,减少 GC 压力。
|
||
//
|
||
// 包含池统计信息、Get/Put 包装方法和统计重置功能。
|
||
//
|
||
// 作者:xfy
|
||
package variable
|
||
|
||
import (
|
||
"sync"
|
||
"sync/atomic"
|
||
|
||
"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 (
|
||
// gets 从池中获取对象的次数
|
||
gets atomic.Int64
|
||
// puts 放回池中对象的次数
|
||
puts atomic.Int64
|
||
// newCount 调用 New 函数创建对象的次数
|
||
newCount atomic.Int64
|
||
// active 当前活跃对象数量(Gets - Puts)
|
||
active atomic.Int64
|
||
)
|
||
|
||
// GetStats 获取池统计信息的副本。
|
||
//
|
||
// 返回当前统计信息的快照,线程安全。
|
||
func GetStats() PoolStats {
|
||
return PoolStats{
|
||
Gets: gets.Load(),
|
||
Puts: puts.Load(),
|
||
NewCount: newCount.Load(),
|
||
Active: active.Load(),
|
||
}
|
||
}
|
||
|
||
// GetPool 获取底层的 sync.Pool(用于测试和调试)。
|
||
func GetPool() *sync.Pool {
|
||
return &pool
|
||
}
|
||
|
||
// PoolGet 从池中获取 Context(包装方法,用于统计)
|
||
//
|
||
// Deprecated: 使用 NewContext 代替,该函数保持向后兼容。
|
||
func PoolGet(ctx *fasthttp.RequestCtx) *Context {
|
||
vc := NewContext(ctx)
|
||
|
||
// 更新统计
|
||
gets.Add(1)
|
||
active.Store(gets.Load() - puts.Load())
|
||
|
||
return vc
|
||
}
|
||
|
||
// PoolPut 将 Context 放回池中(包装方法,用于统计)
|
||
//
|
||
// Deprecated: 使用 ReleaseContext 代替,该函数保持向后兼容。
|
||
func PoolPut(vc *Context) {
|
||
if vc == nil {
|
||
return
|
||
}
|
||
|
||
ReleaseContext(vc)
|
||
|
||
// 更新统计
|
||
puts.Add(1)
|
||
active.Store(gets.Load() - puts.Load())
|
||
}
|
||
|
||
// ResetStats 重置统计信息。
|
||
//
|
||
// 将所有统计计数器清零,线程安全。
|
||
func ResetStats() {
|
||
gets.Store(0)
|
||
puts.Store(0)
|
||
newCount.Store(0)
|
||
active.Store(0)
|
||
}
|