refactor(variable): 将 PoolStats 的 Mutex 改为 atomic.Int64 计数器

使用 sync/atomic 替代 sync.RWMutex,消除统计操作中的锁开销,
提升并发性能。保持 PoolStats 公开 API 不变(返回 plain int64)。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-14 14:24:52 +08:00
parent 0e3ce8cf52
commit 1b73df77b6

View File

@ -7,6 +7,7 @@ package variable
import (
"sync"
"sync/atomic"
"github.com/valyala/fasthttp"
)
@ -26,20 +27,26 @@ type PoolStats struct {
}
var (
// stats 全局池统计信息
stats PoolStats
// statsMu 保护统计信息的读写锁
statsMu sync.RWMutex
// gets 从池中获取对象的次数
gets atomic.Int64
// puts 放回池中对象的次数
puts atomic.Int64
// newCount 调用 New 函数创建对象的次数
newCount atomic.Int64
// active 当前活跃对象数量Gets - Puts
active atomic.Int64
)
// GetStats 获取池统计信息的副本。
//
// 返回当前统计信息的快照,线程安全。
func GetStats() PoolStats {
statsMu.RLock()
s := stats
statsMu.RUnlock()
return s
return PoolStats{
Gets: gets.Load(),
Puts: puts.Load(),
NewCount: newCount.Load(),
Active: active.Load(),
}
}
// GetPool 获取底层的 sync.Pool用于测试和调试
@ -54,10 +61,8 @@ func PoolGet(ctx *fasthttp.RequestCtx) *Context {
vc := NewContext(ctx)
// 更新统计
statsMu.Lock()
stats.Gets++
stats.Active = stats.Gets - stats.Puts
statsMu.Unlock()
gets.Add(1)
active.Store(gets.Load() - puts.Load())
return vc
}
@ -73,17 +78,16 @@ func PoolPut(vc *Context) {
ReleaseContext(vc)
// 更新统计
statsMu.Lock()
stats.Puts++
stats.Active = stats.Gets - stats.Puts
statsMu.Unlock()
puts.Add(1)
active.Store(gets.Load() - puts.Load())
}
// ResetStats 重置统计信息。
//
// 将所有统计计数器清零,线程安全。
func ResetStats() {
statsMu.Lock()
stats = PoolStats{}
statsMu.Unlock()
gets.Store(0)
puts.Store(0)
newCount.Store(0)
active.Store(0)
}