xfy 026302465d feat(lua): 实现定时器 API (ngx.timer)
添加定时器管理实现:
- TimerManager: 定时器生命周期管理
- ngx.timer.at: 创建一次性定时器
- ngx.timer.running_count: 活跃定时器计数
- ngx.timer.pending_count: 等待执行定时器计数
- 支持定时器取消和优雅关闭

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-12 11:21:24 +08:00

35 lines
932 B
Lua

-- timer.lua - 定时器示例
-- 此脚本演示 ngx.timer.at 的使用
-- 创建定时器回调函数
local function timer_callback()
-- 注意:定时器回调在独立上下文中执行
-- 不能直接访问请求相关 API
ngx.log(ngx.INFO, "Timer executed!")
end
-- 创建 5 秒后执行的定时器
local handle, err = ngx.timer.at(5, timer_callback)
if handle then
ngx.say("Timer created successfully")
-- 查看活跃定时器数
local count = ngx.timer.running_count()
ngx.say("Active timers: ", count)
else
ngx.say("Failed to create timer: ", err)
end
-- 创建带参数的定时器(简化版暂不支持参数传递)
local function param_callback()
ngx.log(ngx.INFO, "Timer with params executed")
end
handle, err = ngx.timer.at(2, param_callback)
if handle then
ngx.say("Timer with params created")
else
ngx.say("Failed: ", err)
end
ngx.say("Timer demo completed!")