Hello!
On Sun, Mar 17, 2013 at 6:36 PM, Matthieu Tourne wrote:
> How would be the best way to have an at_time function that would execute
> after a given time ?
>
> The easiest thing I can think of is :
>
> spawn a thread, and do a ngx.sleep(xx secs); callback ()
>
> but I doubt this would work well, or even at all ..
> Would the main request get cleaned up properly if I did that ?
>
This will not do what you want because the request will not complete
processing until all the light threads terminate. It seems that you
want an orphaned thread that is detached from the current request and
running silently in the background.
> The other idea which seems better is to maintain timers in a separate C
> module ?
We can implement a timer API for ngx_lua:
ngx.timer.once(period, callback)
ngx.timer.repeat(period, callback)
The callback will run in a standalone "light thread" attached with a
faked ngx_http_request_t object that holds no downstream connection at
all.
Actually we've been discussing the timer_by_lua directive for quite
while on the openresty-en mailing list. It seems that a pure Lua land
API is more flexible than a dedicated directive except that the pure
Lua API requires a client request to trigger.
BTW, I'm cc'ing the openresty-en mailing list.
Best regards,
-agentzh