Module resty.timer
Extended timer.
Provides recurring, cancellable, node-wide timers, beyond what the basic OpenResty timers do.
Info:
- Copyright: 2017 - 2020 Kong Inc.
- License: Apache 2.0
- Author: Thijs Schreijer
Functions
cancel () | Cancel the timer. |
new (opts, ...) | Create a new timer. |
Functions
- cancel ()
-
Cancel the timer.
Will run the 'cancel'-callback if provided. Will only cancel the timer
in the current worker.
Returns:
-
results of the 'cancel' callback, or
true
if no callback was provided ornil + "already cancelled"
if called repeatedlyUsage:
local t, err = resty_timer(options) -- create a timer if t then t:cancel() -- immediately cancel the timer again end
- new (opts, ...)
-
Create a new timer. The
opts
table is not stored nor altered, and can hence be safely reused to create multiple timers. It supports the following parameters:interval
: (number) interval in seconds after which the timer expiresrecurring
: (boolean) set totrue
to make it a recurring timerjitter
: (optional, number) variable interval to add to the first interval, default 0. If set to 1 second then the first interval will be set betweeninterval
andinterval + 1
. This makes sure if large numbers of timers are used, their execution gets randomly distributed.immediate
: (boolean) will do the first run immediately (the initial interval will be set to 0 seconds). This option requires therecurring
option. The first run will not include thejitter
interval, it will be added to second run.detached
: (boolean) if set totrue
the timer will keep running detached, if set tofalse
the timer will be garbage collected unless anchored by the user.expire
: (function) callback called asfunction(...)
with the arguments passed as extra beyond theopts
table to this new function.cancel : (optional, function) callback called as
function(reason, ...)
. Wherereason
indicates why it was cancelled. The additional arguments will be the arguments as passed to this new function, beyond theopts
table. See the usage example below for possible values forreason
.shm_name
: (optional, string) name of the shm to use to synchronize with the other workers ifkey_name
is set.key_name
: (optional, string) key name to use in shmshm_name
. If this key is given the timer will only be executed in a single worker. All timers (across all workers) with the same key will share this. The key will always be prefixed with this module's name to prevent name collisions in the shm. This option requires theshm_name
option.sub_interval
: (optional, number) interval in seconds to check whether the timer needs to run. Only used for cross-worker timers. This setting reduces the maximum delay when a worker that currently runs the timer exits. In this case the maximum delay could beinterval * 2
before another worker picks it up. With this option set, the maximum delay will beinterval + sub_interval
. This option requires theimmediate
andkey_name
options.
Parameters:
- opts table with options
- ...
arguments to pass to the callbacks
expire
and cancel.
Returns:
-
timer object or
nil + err
Usage:
local object = { name = "myName", } function object:timer_callback(...) -- Note: here we use colon-":" syntax print("starting ", self.name, ": ", ...) --> "starting myName: 1 two 3" end function object.cancel_callback(reason, self, ...) -- Note: here we cannot use colon-":" syntax, due to the 'reason' parameter print("stopping ", self.name, ": ", ...) --> "stopping myName: 1 two 3" if reason == resty_timer.CANCEL_USER then -- user called timer:cancel elseif reason == resty_timer.CANCEL_GC then -- the timer was garbage-collected elseif reason == resty_timer.CANCEL_SYSTEM then -- prematurely cancelled by the system (worker is exiting) else -- should not happen end end function object:start() if self.timer then return end self.timer = resty_timer({ interval = 1, expire = self.timer_callback, cancel = self.cancel_callback, }, self, 1, " two ", 3) -- 'self' + 3 parameters to pass to the callbacks function object:stop() if self.timer then self.timer:cancel() self.timer = nil end end