refactor(lua): 对象池类型安全优化
- 使用 any 替代 interface{} (Go 1.18+)
- 添加类型断言检查防止 Pool 误用
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
eccdcde901
commit
49f1e26760
@ -25,7 +25,7 @@ type LuaContext struct {
|
|||||||
|
|
||||||
// luaContextPool LuaContext 对象池
|
// luaContextPool LuaContext 对象池
|
||||||
var luaContextPool = sync.Pool{
|
var luaContextPool = sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() any {
|
||||||
return &LuaContext{
|
return &LuaContext{
|
||||||
Variables: make(map[string]string),
|
Variables: make(map[string]string),
|
||||||
}
|
}
|
||||||
@ -34,7 +34,13 @@ var luaContextPool = sync.Pool{
|
|||||||
|
|
||||||
// AcquireContext 从池中获取 LuaContext
|
// AcquireContext 从池中获取 LuaContext
|
||||||
func AcquireContext(engine *LuaEngine, req *fasthttp.RequestCtx) *LuaContext {
|
func AcquireContext(engine *LuaEngine, req *fasthttp.RequestCtx) *LuaContext {
|
||||||
lc := luaContextPool.Get().(*LuaContext)
|
v := luaContextPool.Get()
|
||||||
|
lc, ok := v.(*LuaContext)
|
||||||
|
if !ok {
|
||||||
|
// Pool 的 New 函数返回 *LuaContext,类型断言不应失败
|
||||||
|
// 如果失败说明 Pool 被错误使用,panic 是合理的
|
||||||
|
panic("luaContextPool returned unexpected type")
|
||||||
|
}
|
||||||
lc.Engine = engine
|
lc.Engine = engine
|
||||||
lc.RequestCtx = req
|
lc.RequestCtx = req
|
||||||
lc.Phase = PhaseInit
|
lc.Phase = PhaseInit
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user