From 49f1e267608464b2085e1be047977f94c87e564d Mon Sep 17 00:00:00 2001 From: xfy Date: Thu, 16 Apr 2026 11:13:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor(lua):=20=E5=AF=B9=E8=B1=A1=E6=B1=A0?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=AE=89=E5=85=A8=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用 any 替代 interface{} (Go 1.18+) - 添加类型断言检查防止 Pool 误用 Co-Authored-By: Claude Opus 4.6 --- internal/lua/context.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/lua/context.go b/internal/lua/context.go index 56af8cf..adcb486 100644 --- a/internal/lua/context.go +++ b/internal/lua/context.go @@ -25,7 +25,7 @@ type LuaContext struct { // luaContextPool LuaContext 对象池 var luaContextPool = sync.Pool{ - New: func() interface{} { + New: func() any { return &LuaContext{ Variables: make(map[string]string), } @@ -34,7 +34,13 @@ var luaContextPool = sync.Pool{ // AcquireContext 从池中获取 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.RequestCtx = req lc.Phase = PhaseInit