Skip to content

Commit 9b16e25

Browse files
doujiang24agentzh
authored andcommitted
feature: implemented the ngx.timer.every() API function for creating recurring timers.
Signed-off-by: Yichun Zhang (agentzh) <[email protected]>
1 parent 2a87704 commit 9b16e25

File tree

5 files changed

+634
-16
lines changed

5 files changed

+634
-16
lines changed

README.markdown

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,8 +1471,8 @@ This hook is often used to create per-worker reoccurring timers (via the [ngx.ti
14711471
end
14721472
end
14731473
1474-
local ok, err = new_timer(delay, check)
1475-
if not ok then
1474+
local hdl, err = new_timer(delay, check)
1475+
if not hdl then
14761476
log(ERR, "failed to create timer: ", err)
14771477
return
14781478
end
@@ -3217,6 +3217,7 @@ Nginx API for Lua
32173217
* [ngx.thread.kill](#ngxthreadkill)
32183218
* [ngx.on_abort](#ngxon_abort)
32193219
* [ngx.timer.at](#ngxtimerat)
3220+
* [ngx.timer.every](#ngxtimerevery)
32203221
* [ngx.timer.running_count](#ngxtimerrunning_count)
32213222
* [ngx.timer.pending_count](#ngxtimerpending_count)
32223223
* [ngx.config.subsystem](#ngxconfigsubsystem)
@@ -7568,7 +7569,7 @@ See also [lua_check_client_abort](#lua_check_client_abort).
75687569

75697570
ngx.timer.at
75707571
------------
7571-
**syntax:** *ok, err = ngx.timer.at(delay, callback, user_arg1, user_arg2, ...)*
7572+
**syntax:** *hdl, err = ngx.timer.at(delay, callback, user_arg1, user_arg2, ...)*
75727573

75737574
**context:** *init_worker_by_lua&#42;, set_by_lua&#42;, rewrite_by_lua&#42;, access_by_lua&#42;, content_by_lua&#42;, header_filter_by_lua&#42;, body_filter_by_lua&#42;, log_by_lua&#42;, ngx.timer.&#42;, balancer_by_lua&#42;, ssl_certificate_by_lua&#42;, ssl_session_fetch_by_lua&#42;, ssl_session_store_by_lua&#42;*
75747575

@@ -7594,7 +7595,7 @@ Premature timer expiration happens when the Nginx worker process is
75947595
trying to shut down, as in an Nginx configuration reload triggered by
75957596
the `HUP` signal or in an Nginx server shutdown. When the Nginx worker
75967597
is trying to shut down, one can no longer call `ngx.timer.at` to
7597-
create new timers with nonzero delays and in that case `ngx.timer.at` will return `nil` and
7598+
create new timers with nonzero delays and in that case `ngx.timer.at` will return a "conditional false" value and
75987599
a string describing the error, that is, "process exiting".
75997600

76007601
Starting from the `v0.9.3` release, it is allowed to create zero-delay timers even when the Nginx worker process starts shutting down.
@@ -7653,6 +7654,9 @@ One can also create infinite re-occurring timers, for instance, a timer getting
76537654
end
76547655
```
76557656

7657+
It is recommended, however, to use the [ngx.timer.every](#ngxtimerevery) API function
7658+
instead for creating recurring timers since it is more robust.
7659+
76567660
Because timer callbacks run in the background and their running time
76577661
will not add to any client request's response time, they can easily
76587662
accumulate in the server and exhaust system resources due to either
@@ -7692,6 +7696,25 @@ This API was first introduced in the `v0.8.0` release.
76927696

76937697
[Back to TOC](#nginx-api-for-lua)
76947698

7699+
ngx.timer.every
7700+
---------------
7701+
**syntax:** *hdl, err = ngx.timer.every(delay, callback, user_arg1, user_arg2, ...)*
7702+
7703+
**context:** *init_worker_by_lua&#42;, set_by_lua&#42;, rewrite_by_lua&#42;, access_by_lua&#42;, content_by_lua&#42;, header_filter_by_lua&#42;, body_filter_by_lua&#42;, log_by_lua&#42;, ngx.timer.&#42;, balancer_by_lua&#42;, ssl_certificate_by_lua&#42;, ssl_session_fetch_by_lua&#42;, ssl_session_store_by_lua&#42;*
7704+
7705+
Similar to the [ngx.timer.at](#ngxtimerat) API function, but
7706+
7707+
1. `delay` *cannot* be zero,
7708+
1. timer will be created every `delay` seconds until the current Nginx worker process starts exiting.
7709+
7710+
When success, returns a "conditional true" value (but not a `true`). Otherwise, returns a "conditional false" value and a string describing the error.
7711+
7712+
This API also respect the [lua_max_pending_timers](#lua_max_pending_timers) and [lua_max_running_timers](#lua_max_running_timers).
7713+
7714+
This API was first introduced in the `v0.10.9` release.
7715+
7716+
[Back to TOC](#nginx-api-for-lua)
7717+
76957718
ngx.timer.running_count
76967719
-----------------------
76977720
**syntax:** *count = ngx.timer.running_count()*

doc/HttpLuaModule.wiki

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,8 +1180,8 @@ This hook is often used to create per-worker reoccurring timers (via the [[#ngx.
11801180
end
11811181
end
11821182
1183-
local ok, err = new_timer(delay, check)
1184-
if not ok then
1183+
local hdl, err = new_timer(delay, check)
1184+
if not hdl then
11851185
log(ERR, "failed to create timer: ", err)
11861186
return
11871187
end
@@ -6412,7 +6412,7 @@ This API was first introduced in the <code>v0.7.4</code> release.
64126412
See also [[#lua_check_client_abort|lua_check_client_abort]].
64136413
64146414
== ngx.timer.at ==
6415-
'''syntax:''' ''ok, err = ngx.timer.at(delay, callback, user_arg1, user_arg2, ...)''
6415+
'''syntax:''' ''hdl, err = ngx.timer.at(delay, callback, user_arg1, user_arg2, ...)''
64166416
64176417
'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*''
64186418
@@ -6438,7 +6438,7 @@ Premature timer expiration happens when the Nginx worker process is
64386438
trying to shut down, as in an Nginx configuration reload triggered by
64396439
the <code>HUP</code> signal or in an Nginx server shutdown. When the Nginx worker
64406440
is trying to shut down, one can no longer call <code>ngx.timer.at</code> to
6441-
create new timers with nonzero delays and in that case <code>ngx.timer.at</code> will return <code>nil</code> and
6441+
create new timers with nonzero delays and in that case <code>ngx.timer.at</code> will return a "conditional false" value and
64426442
a string describing the error, that is, "process exiting".
64436443
64446444
Starting from the <code>v0.9.3</code> release, it is allowed to create zero-delay timers even when the Nginx worker process starts shutting down.
@@ -6495,6 +6495,9 @@ One can also create infinite re-occurring timers, for instance, a timer getting
64956495
end
64966496
</geshi>
64976497
6498+
It is recommended, however, to use the [[#ngx.timer.every|ngx.timer.every]] API function
6499+
instead for creating recurring timers since it is more robust.
6500+
64986501
Because timer callbacks run in the background and their running time
64996502
will not add to any client request's response time, they can easily
65006503
accumulate in the server and exhaust system resources due to either
@@ -6532,6 +6535,22 @@ You can pass most of the standard Lua values (nils, booleans, numbers, strings,
65326535
65336536
This API was first introduced in the <code>v0.8.0</code> release.
65346537
6538+
== ngx.timer.every ==
6539+
'''syntax:''' ''hdl, err = ngx.timer.every(delay, callback, user_arg1, user_arg2, ...)''
6540+
6541+
'''context:''' ''init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*''
6542+
6543+
Similar to the [[#ngx.timer.at|ngx.timer.at]] API function, but
6544+
6545+
# <code>delay</code> ''cannot'' be zero,
6546+
# timer will be created every <code>delay</code> seconds until the current Nginx worker process starts exiting.
6547+
6548+
When success, returns a "conditional true" value (but not a <code>true</code>). Otherwise, returns a "conditional false" value and a string describing the error.
6549+
6550+
This API also respect the [[#lua_max_pending_timers|lua_max_pending_timers]] and [[#lua_max_running_timers|lua_max_running_timers]].
6551+
6552+
This API was first introduced in the <code>v0.10.9</code> release.
6553+
65356554
== ngx.timer.running_count ==
65366555
'''syntax:''' ''count = ngx.timer.running_count()''
65376556

0 commit comments

Comments
 (0)