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 +}