feat(lua): 集成共享字典、定时器和子请求到引擎
扩展 LuaEngine 以支持新的 ngx API: - 添加 SharedDictManager/TimerManager/LocationManager - 在 setupNgxAPI 中注册 ngx.shared/timer/location API - 实现优雅关闭时清理定时器和共享字典 - 提供管理器访问方法和便捷创建接口 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
a4a820ab24
commit
7f2939a7e0
@ -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)
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user