From 8a533ba0cafe11f2e7c6fc0f4c7810d341b7745b Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 13 Apr 2026 11:12:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(lua):=20=E6=8F=90=E5=8F=96=20API=20?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E8=BE=85=E5=8A=A9=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 RegisterAPIMethods 和 RegisterUnsafeAPI 函数,简化 Lua API 批量注册逻辑。 Co-Authored-By: Claude Opus 4.6 --- internal/lua/api_ctx.go | 9 +++++++-- internal/lua/api_req.go | 33 +++++++++++++-------------------- internal/lua/api_resp.go | 23 ++++++++--------------- internal/lua/register.go | 29 +++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 internal/lua/register.go diff --git a/internal/lua/api_ctx.go b/internal/lua/api_ctx.go index c9cc536..ad754db 100644 --- a/internal/lua/api_ctx.go +++ b/internal/lua/api_ctx.go @@ -19,8 +19,13 @@ func RegisterNgxCtxAPI(L *glua.LState, ngxTable *glua.LTable) { func RegisterSchedulerUnsafeCtxAPI(L *glua.LState, ngx *glua.LTable) { ctxTable := L.NewTable() mt := L.NewTable() - mt.RawSetString("__index", L.NewFunction(luaSchedulerUnsafeCtx)) - mt.RawSetString("__newindex", L.NewFunction(luaSchedulerUnsafeCtx)) + + methods := []APIMethod{ + {Name: "__index", Func: luaSchedulerUnsafeCtx}, + {Name: "__newindex", Func: luaSchedulerUnsafeCtx}, + } + RegisterAPIMethods(L, mt, methods) + L.SetMetatable(ctxTable, mt) ngx.RawSetString("ctx", ctxTable) } diff --git a/internal/lua/api_req.go b/internal/lua/api_req.go index 1a6d4c9..5418c5a 100644 --- a/internal/lua/api_req.go +++ b/internal/lua/api_req.go @@ -570,24 +570,17 @@ func (api *ngxReqAPI) ResetMetrics() { // RegisterSchedulerUnsafeReqAPI 为 Scheduler LState 注册不安全的 ngx.req API func RegisterSchedulerUnsafeReqAPI(L *glua.LState, ngx *glua.LTable) { - ngxReq := L.NewTable() - - ngxReq.RawSetString("get_method", L.NewFunction(luaSchedulerUnsafeReq)) - ngxReq.RawSetString("get_uri", L.NewFunction(luaSchedulerUnsafeReq)) - ngxReq.RawSetString("set_uri", L.NewFunction(luaSchedulerUnsafeReq)) - ngxReq.RawSetString("get_uri_args", L.NewFunction(luaSchedulerUnsafeReq)) - ngxReq.RawSetString("set_uri_args", L.NewFunction(luaSchedulerUnsafeReq)) - ngxReq.RawSetString("get_headers", L.NewFunction(luaSchedulerUnsafeReq)) - ngxReq.RawSetString("set_header", L.NewFunction(luaSchedulerUnsafeReq)) - ngxReq.RawSetString("clear_header", L.NewFunction(luaSchedulerUnsafeReq)) - ngxReq.RawSetString("get_body_data", L.NewFunction(luaSchedulerUnsafeReq)) - ngxReq.RawSetString("read_body", L.NewFunction(luaSchedulerUnsafeReq)) - - ngx.RawSetString("req", ngxReq) -} - -// luaSchedulerUnsafeReq 在 scheduler 模式下调用 ngx.req API 时返回错误 -func luaSchedulerUnsafeReq(L *glua.LState) int { - L.RaiseError("API ngx.req not available in timer callback context") - return 0 + methods := []string{ + "get_method", + "get_uri", + "set_uri", + "get_uri_args", + "set_uri_args", + "get_headers", + "set_header", + "clear_header", + "get_body_data", + "read_body", + } + RegisterUnsafeAPI(L, ngx, "ngx.req", methods) } diff --git a/internal/lua/api_resp.go b/internal/lua/api_resp.go index 6b780e3..b892e81 100644 --- a/internal/lua/api_resp.go +++ b/internal/lua/api_resp.go @@ -201,19 +201,12 @@ func (api *ngxRespAPI) SetHeader(name, value string) { // RegisterSchedulerUnsafeRespAPI 为 Scheduler LState 注册不安全的 ngx.resp API func RegisterSchedulerUnsafeRespAPI(L *glua.LState, ngx *glua.LTable) { - ngxResp := L.NewTable() - - ngxResp.RawSetString("get_status", L.NewFunction(luaSchedulerUnsafeResp)) - ngxResp.RawSetString("set_status", L.NewFunction(luaSchedulerUnsafeResp)) - ngxResp.RawSetString("get_headers", L.NewFunction(luaSchedulerUnsafeResp)) - ngxResp.RawSetString("set_header", L.NewFunction(luaSchedulerUnsafeResp)) - ngxResp.RawSetString("clear_header", L.NewFunction(luaSchedulerUnsafeResp)) - - ngx.RawSetString("resp", ngxResp) -} - -// luaSchedulerUnsafeResp 在 scheduler 模式下调用 ngx.resp API 时返回错误 -func luaSchedulerUnsafeResp(L *glua.LState) int { - L.RaiseError("API ngx.resp not available in timer callback context") - return 0 + methods := []string{ + "get_status", + "set_status", + "get_headers", + "set_header", + "clear_header", + } + RegisterUnsafeAPI(L, ngx, "ngx.resp", methods) } diff --git a/internal/lua/register.go b/internal/lua/register.go new file mode 100644 index 0000000..e68cc69 --- /dev/null +++ b/internal/lua/register.go @@ -0,0 +1,29 @@ +// Package lua 提供 Lua API 注册辅助函数 +package lua + +import glua "github.com/yuin/gopher-lua" + +// APIMethod 表示一个 Lua API 方法 +type APIMethod struct { + Name string + Func func(*glua.LState) int +} + +// RegisterAPIMethods 批量注册 API 方法到 Lua 表 +func RegisterAPIMethods(L *glua.LState, tbl *glua.LTable, methods []APIMethod) { + for _, m := range methods { + tbl.RawSetString(m.Name, L.NewFunction(m.Func)) + } +} + +// RegisterUnsafeAPI 注册不可用于 timer context 的安全桩 +func RegisterUnsafeAPI(L *glua.LState, ngx *glua.LTable, apiName string, methods []string) { + tbl := L.NewTable() + for _, m := range methods { + tbl.RawSetString(m, L.NewFunction(func(L *glua.LState) int { + L.RaiseError("API %s.%s not available in timer callback context", apiName, m) + return 0 + })) + } + ngx.RawSetString(apiName, tbl) +}