lolly/internal/lua/config.go
xfy fd4e164ae6 refactor(security): 滑动窗口限流器使用分段锁优化并发性能
将单一 counters map + 全局 mutex 改为 16 buckets 分段锁结构:

- 新增 limiterBucket 结构体,每个桶独立持有 RW 锁和计数器 map
- 使用 FNV-1a 哈希算法将键均匀分布到 16 个桶中
- 各方法修改为按 bucket 分发操作:
  - Allow() / allowApproximate() / allowPrecise()
  - Reset() / ResetAll() / Cleanup()
  - GetStats() / GetCount()

收益:
- 并发场景下锁竞争降低约 94% (16 个桶并行)
- 基准测试显示并行 Allow 操作约 89ns/op

测试验证:
- go test -race 通过并发安全测试
- 基准测试显示吞吐提升

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-14 14:26:01 +08:00

42 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package lua 提供 Lua 脚本嵌入能力
// 采用 Server 级单 LState + 请求级临时协程架构
package lua
import (
"time"
)
// Config Lua 引擎配置
type Config struct {
MaxConcurrentCoroutines int
CoroutineTimeout time.Duration
CodeCacheSize int
CodeCacheTTL time.Duration
MaxExecutionTime time.Duration
EnableFileWatch bool
EnableOSLib bool
EnableIOLib bool
EnableLoadLib bool
CoroutineStackSize int // 协程栈大小默认64最大256
MinimizeStackMemory bool // 启用栈内存自动收缩以减少内存占用
CoroutinePoolWarmup int // 协程池预热数量,启动时预创建
}
// DefaultConfig 返回默认配置
func DefaultConfig() *Config {
return &Config{
MaxConcurrentCoroutines: 1000,
CoroutineTimeout: 30 * time.Second,
CodeCacheSize: 1000,
CodeCacheTTL: time.Hour,
EnableFileWatch: true,
MaxExecutionTime: 30 * time.Second,
EnableOSLib: false,
EnableIOLib: false,
EnableLoadLib: false,
CoroutineStackSize: 64, // 优化:较小的栈减少内存分配
MinimizeStackMemory: true,
CoroutinePoolWarmup: 4, // 预热4个协程结构
}
}