lolly/internal/lua/AGENTS.md
xfy 4f6a7be44c docs: 更新 AGENTS.md 项目文档
- 更新各目录 AGENTS.md 的子目录信息
- 添加新增目录的 AGENTS.md 文件
- 更新 docs/prompts.md 添加新的提示词

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-13 13:15:39 +08:00

2.3 KiB
Raw Permalink Blame History

lua

Purpose

Lua 脚本嵌入引擎,提供类似 OpenResty 的 Lua 沙箱环境,支持请求处理、定时器、共享字典等 API。

Key Files

File Description
engine.go LuaEngine 核心:协程池、代码缓存、调度器
config.go 引擎配置:超时、并发限制、库开关
context.go LuaContext请求上下文 Lua 绑定
coroutine.go LuaCoroutine协程生命周期管理
middleware.go 中间件集成access_by_lua、content_by_lua
middleware_config.go 中间件配置解析和验证
shared_dict.go 共享字典:线程安全的键值存储
socket_manager.go cosocket 管理TCP 连接池
timer_manager.go 定时器管理ngx.timer.at 实现
cache.go 字节码缓存:预编译脚本缓存
register.go API 注册ngx 表初始化
filter_writer.go 响应过滤器body_filter_by_lua
api_*.go ngx API 实现req、resp、ctx、var、log、timer、socket、location、shared_dict

For AI Agents

Working In This Directory

  • LuaEngine 是 HTTP Server 实例级单例,通过 NewEngine(config) 创建
  • 协程通过 engine.NewCoroutine(req) 创建,使用后自动释放回池
  • 定时器回调在专用调度器 LState 中执行,不能捕获闭包变量(使用 shared_dict 传递数据)
  • API 分为安全timer 可用)和不安全(仅请求协程可用)两类

Testing Requirements

  • 运行测试:go test ./internal/lua/...
  • 基准测试:go test -bench=. ./internal/lua/...
  • 测试覆盖协程生命周期、API 限制、并发安全

Common Patterns

// 创建引擎
engine, err := lua.NewEngine(config)
engine.InitSchedulerLState() // 启用定时器

// 创建共享字典
engine.CreateSharedDict("cache", 1000)

// 中间件配置
mw := lua.NewMiddleware(mwConfig, engine)
handler = mw.Wrap(handler)

Dependencies

Internal

  • internal/config - Lua 中间件配置结构
  • internal/logging - 日志输出
  • internal/middleware - 中间件接口

External

  • github.com/yuin/gopher-lua - Lua 解释器
  • github.com/valyala/fasthttp - HTTP 请求上下文