diff --git a/internal/config/config.go b/internal/config/config.go index 52ef086..52c7b28 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1378,6 +1378,9 @@ type LuaScriptConfig struct { // - MaxConcurrentCoroutines 控制最大并发协程数 // - CoroutineTimeout 控制协程执行超时 // - CodeCacheSize 控制字节码缓存大小 +// - CoroutineStackSize 控制协程栈大小(默认64) +// - MinimizeStackMemory 启用栈内存自动收缩 +// - CoroutinePoolWarmup 协程池预热数量 // // 使用示例: // @@ -1387,6 +1390,9 @@ type LuaScriptConfig struct { // code_cache_size: 1000 // enable_file_watch: true // max_execution_time: 30s +// coroutine_stack_size: 64 +// minimize_stack_memory: true +// coroutine_pool_warmup: 4 type LuaGlobalSettings struct { // MaxConcurrentCoroutines 最大并发协程数 MaxConcurrentCoroutines int `yaml:"max_concurrent_coroutines"` @@ -1402,6 +1408,16 @@ type LuaGlobalSettings struct { // MaxExecutionTime 单脚本最大执行时间 MaxExecutionTime time.Duration `yaml:"max_execution_time"` + + // CoroutineStackSize 协程栈大小(默认64,最大256) + // 较小的栈减少内存分配,适用于简单脚本 + CoroutineStackSize int `yaml:"coroutine_stack_size"` + + // MinimizeStackMemory 启用栈内存自动收缩以减少内存占用 + MinimizeStackMemory bool `yaml:"minimize_stack_memory"` + + // CoroutinePoolWarmup 协程池预热数量,启动时预创建 + CoroutinePoolWarmup int `yaml:"coroutine_pool_warmup"` } // StreamConfig TCP/UDP Stream 代理配置。 diff --git a/internal/lua/middleware_config.go b/internal/lua/middleware_config.go index 9fdda30..afb6d2c 100644 --- a/internal/lua/middleware_config.go +++ b/internal/lua/middleware_config.go @@ -45,6 +45,16 @@ type GlobalLuaSettings struct { // MaxExecutionTime 单脚本最大执行时间 MaxExecutionTime time.Duration `yaml:"max_execution_time"` + + // CoroutineStackSize 协程栈大小(默认64,最大256) + // 较小的栈减少内存分配,适用于简单脚本 + CoroutineStackSize int `yaml:"coroutine_stack_size"` + + // MinimizeStackMemory 启用栈内存自动收缩以减少内存占用 + MinimizeStackMemory bool `yaml:"minimize_stack_memory"` + + // CoroutinePoolWarmup 协程池预热数量,启动时预创建 + CoroutinePoolWarmup int `yaml:"coroutine_pool_warmup"` } // DefaultMiddlewareConfig 默认 Lua 中间件配置 @@ -141,7 +151,7 @@ func ParsePhase(s string) (Phase, error) { // ToEngineConfig 将全局设置转换为引擎配置 func (s *GlobalLuaSettings) ToEngineConfig() *Config { - return &Config{ + cfg := &Config{ MaxConcurrentCoroutines: s.MaxConcurrentCoroutines, CoroutineTimeout: s.CoroutineTimeout, CodeCacheSize: s.CodeCacheSize, @@ -152,4 +162,23 @@ func (s *GlobalLuaSettings) ToEngineConfig() *Config { EnableIOLib: false, EnableLoadLib: false, } + + // 设置协程栈优化选项 + if s.CoroutineStackSize > 0 { + cfg.CoroutineStackSize = s.CoroutineStackSize + } else { + cfg.CoroutineStackSize = 64 // 默认优化值 + } + + // 设置栈内存优化选项 + cfg.MinimizeStackMemory = s.MinimizeStackMemory + + // 设置协程池预热 + if s.CoroutinePoolWarmup > 0 { + cfg.CoroutinePoolWarmup = s.CoroutinePoolWarmup + } else { + cfg.CoroutinePoolWarmup = 4 // 默认预热数量 + } + + return cfg } diff --git a/internal/server/init.go b/internal/server/init.go index d2e5a3f..2643fea 100644 --- a/internal/server/init.go +++ b/internal/server/init.go @@ -113,6 +113,19 @@ func initLuaEngine(luaCfg *config.LuaMiddlewareConfig) (*lua.LuaEngine, error) { EnableLoadLib: false, } + // 设置协程栈优化选项 + if luaCfg.GlobalSettings.CoroutineStackSize > 0 { + engineCfg.CoroutineStackSize = luaCfg.GlobalSettings.CoroutineStackSize + } else { + engineCfg.CoroutineStackSize = 64 // 默认优化值 + } + engineCfg.MinimizeStackMemory = luaCfg.GlobalSettings.MinimizeStackMemory + if luaCfg.GlobalSettings.CoroutinePoolWarmup > 0 { + engineCfg.CoroutinePoolWarmup = luaCfg.GlobalSettings.CoroutinePoolWarmup + } else { + engineCfg.CoroutinePoolWarmup = 4 // 默认预热数量 + } + // 设置默认值 if engineCfg.MaxConcurrentCoroutines == 0 { engineCfg.MaxConcurrentCoroutines = 1000