From 7f2939a7e0876583f4adb471b5ee982195cd808f Mon Sep 17 00:00:00 2001 From: xfy Date: Sun, 12 Apr 2026 11:21:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(lua):=20=E9=9B=86=E6=88=90=E5=85=B1?= =?UTF-8?q?=E4=BA=AB=E5=AD=97=E5=85=B8=E3=80=81=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E5=92=8C=E5=AD=90=E8=AF=B7=E6=B1=82=E5=88=B0=E5=BC=95=E6=93=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 扩展 LuaEngine 以支持新的 ngx API: - 添加 SharedDictManager/TimerManager/LocationManager - 在 setupNgxAPI 中注册 ngx.shared/timer/location API - 实现优雅关闭时清理定时器和共享字典 - 提供管理器访问方法和便捷创建接口 Co-Authored-By: Claude Opus 4.6 --- internal/lua/coroutine.go | 11 +++++++- internal/lua/engine.go | 54 ++++++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/internal/lua/coroutine.go b/internal/lua/coroutine.go index 343d2a9..9a3618d 100644 --- a/internal/lua/coroutine.go +++ b/internal/lua/coroutine.go @@ -167,7 +167,7 @@ func (c *LuaCoroutine) setupSecureCoroutineLib() { } // setupNgxAPI 创建 ngx API -// 注册 ngx.req、ngx.resp、ngx.var、ngx.ctx、ngx.log 和 ngx.socket API +// 注册 ngx.req、ngx.resp、ngx.var、ngx.ctx、ngx.log、ngx.socket 和 ngx.shared API func (c *LuaCoroutine) setupNgxAPI() { // 创建 ngx 表 ngx := c.Co.NewTable() @@ -199,6 +199,15 @@ func (c *LuaCoroutine) setupNgxAPI() { // 注册 ngx.socket API RegisterTCPSocketAPI(c.Co, c.Engine) + + // 注册 ngx.shared.DICT API + RegisterSharedDictAPI(c.Co, c.Engine.SharedDictManager(), ngx) + + // 注册 ngx.timer API + RegisterTimerAPI(c.Co, c.Engine.TimerManager(), ngx) + + // 注册 ngx.location API + RegisterLocationAPI(c.Co, c.Engine.LocationManager(), ngx) } // Execute 在协程中执行 Lua 脚本(支持 Yield/Resume) diff --git a/internal/lua/engine.go b/internal/lua/engine.go index 37e7330..20b185b 100644 --- a/internal/lua/engine.go +++ b/internal/lua/engine.go @@ -38,6 +38,15 @@ type LuaEngine struct { ctx context.Context cancel context.CancelFunc + // 共享字典管理器 + sharedDictManager *SharedDictManager + + // 定时器管理器 + timerManager *TimerManager + + // location 管理器 + locationManager *LocationManager + // 统计 stats EngineStats } @@ -80,12 +89,13 @@ func NewEngine(config *Config) (*LuaEngine, error) { ctx, cancel := context.WithCancel(context.Background()) engine := &LuaEngine{ - L: L, - config: config, - codeCache: NewCodeCache(config.CodeCacheSize, config.CodeCacheTTL, config.EnableFileWatch), - maxCoroutines: config.MaxConcurrentCoroutines, - ctx: ctx, - cancel: cancel, + L: L, + config: config, + codeCache: NewCodeCache(config.CodeCacheSize, config.CodeCacheTTL, config.EnableFileWatch), + maxCoroutines: config.MaxConcurrentCoroutines, + ctx: ctx, + cancel: cancel, + sharedDictManager: NewSharedDictManager(), coroutinePool: sync.Pool{ New: func() interface{} { // 注意:这里只是创建空的协程对象结构 @@ -95,12 +105,24 @@ func NewEngine(config *Config) (*LuaEngine, error) { }, } + // 创建定时器管理器(需要在 engine 创建后初始化) + engine.timerManager = NewTimerManager(engine) + + // 创建 location 管理器 + engine.locationManager = NewLocationManager() + return engine, nil } // Close 关闭引擎 func (e *LuaEngine) Close() { e.cancel() + if e.timerManager != nil { + e.timerManager.Close() + } + if e.sharedDictManager != nil { + e.sharedDictManager.Close() + } if e.L != nil { e.L.Close() } @@ -188,3 +210,23 @@ func (e *LuaEngine) Stats() EngineStats { func (e *LuaEngine) ActiveCoroutines() int32 { return atomic.LoadInt32(&e.activeCount) } + +// SharedDictManager 返回共享字典管理器 +func (e *LuaEngine) SharedDictManager() *SharedDictManager { + return e.sharedDictManager +} + +// CreateSharedDict 创建共享字典 +func (e *LuaEngine) CreateSharedDict(name string, maxItems int) *SharedDict { + return e.sharedDictManager.CreateDict(name, maxItems) +} + +// TimerManager 返回定时器管理器 +func (e *LuaEngine) TimerManager() *TimerManager { + return e.timerManager +} + +// LocationManager 返回 location 管理器 +func (e *LuaEngine) LocationManager() *LocationManager { + return e.locationManager +}