refactor(variable): 移除废弃的 PoolGet/PoolPut 和别名函数
移除 deprecated PoolGet、PoolPut 函数及 NewVariableContext、 ReleaseVariableContext 别名,统一使用 NewContext 和 ReleaseContext。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
f04f804834
commit
37d8f9eebc
@ -8,8 +8,6 @@ package variable
|
|||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/valyala/fasthttp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// PoolStats 池统计信息。
|
// PoolStats 池统计信息。
|
||||||
@ -54,34 +52,6 @@ func GetPool() *sync.Pool {
|
|||||||
return &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 重置统计信息。
|
// ResetStats 重置统计信息。
|
||||||
//
|
//
|
||||||
// 将所有统计计数器清零,线程安全。
|
// 将所有统计计数器清零,线程安全。
|
||||||
|
|||||||
@ -140,11 +140,6 @@ func NewContext(ctx *fasthttp.RequestCtx) *Context {
|
|||||||
return vc
|
return vc
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewVariableContext 是 NewContext 的别名(向后兼容)
|
|
||||||
func NewVariableContext(ctx *fasthttp.RequestCtx) *Context {
|
|
||||||
return NewContext(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReleaseContext 释放 Context 回池中
|
// ReleaseContext 释放 Context 回池中
|
||||||
func ReleaseContext(vc *Context) {
|
func ReleaseContext(vc *Context) {
|
||||||
if vc == nil {
|
if vc == nil {
|
||||||
@ -163,11 +158,6 @@ func ReleaseContext(vc *Context) {
|
|||||||
pool.Put(vc)
|
pool.Put(vc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReleaseVariableContext 是 ReleaseContext 的别名(向后兼容)
|
|
||||||
func ReleaseVariableContext(vc *Context) {
|
|
||||||
ReleaseContext(vc)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetResponseInfo 设置响应信息(用于需要 status、body_bytes_sent、request_time 的场景)
|
// SetResponseInfo 设置响应信息(用于需要 status、body_bytes_sent、request_time 的场景)
|
||||||
func (vc *Context) SetResponseInfo(status int, bodySize int64, durationNs int64) {
|
func (vc *Context) SetResponseInfo(status int, bodySize int64, durationNs int64) {
|
||||||
vc.status = status
|
vc.status = status
|
||||||
|
|||||||
@ -677,30 +677,30 @@ func TestExpandOnlyDollar(t *testing.T) {
|
|||||||
func TestPoolFunctions(t *testing.T) {
|
func TestPoolFunctions(t *testing.T) {
|
||||||
ctx := mockRequestCtx(t)
|
ctx := mockRequestCtx(t)
|
||||||
|
|
||||||
// 测试 PoolGet 和 PoolPut
|
// 测试 NewContext 和 ReleaseContext
|
||||||
vc := PoolGet(ctx)
|
vc := NewContext(ctx)
|
||||||
if vc == nil {
|
if vc == nil {
|
||||||
t.Fatal("PoolGet returned nil")
|
t.Fatal("NewContext returned nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置一些值
|
// 设置一些值
|
||||||
vc.Set("test", "value")
|
vc.Set("test", "value")
|
||||||
|
|
||||||
// 释放
|
// 释放
|
||||||
PoolPut(vc)
|
ReleaseContext(vc)
|
||||||
|
|
||||||
// 再次获取应该被清空
|
// 再次获取应该被清空
|
||||||
vc2 := PoolGet(ctx)
|
vc2 := NewContext(ctx)
|
||||||
if _, ok := vc2.Get("test"); ok {
|
if _, ok := vc2.Get("test"); ok {
|
||||||
t.Error("expected context to be cleared after PoolPut")
|
t.Error("expected context to be cleared after ReleaseContext")
|
||||||
}
|
}
|
||||||
PoolPut(vc2)
|
ReleaseContext(vc2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestPoolPutNil 测试 PoolPut nil
|
// TestPoolPutNil 测试 ReleaseContext nil
|
||||||
func TestPoolPutNil(_ *testing.T) {
|
func TestPoolPutNil(_ *testing.T) {
|
||||||
// 不应该 panic
|
// 不应该 panic
|
||||||
PoolPut(nil)
|
ReleaseContext(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestStatsFunctions 测试统计相关函数
|
// TestStatsFunctions 测试统计相关函数
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user