From c7cbfafa3853d973156bfa43de5c0c6950dfb289 Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Tue, 1 Apr 2025 15:22:29 +0200 Subject: [PATCH] MINOR: hlua: core.wait() takes optional delay paramater core.wait() now accepts optional delay parameter in ms. Passed this delay the task is woken up if no event woke the task before. Lua documentation was updated. --- doc/lua-api/index.rst | 8 ++++++-- src/hlua.c | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst index 267b9f42f..4231e32e1 100644 --- a/doc/lua-api/index.rst +++ b/doc/lua-api/index.rst @@ -926,13 +926,17 @@ Core class its work and wants to give back the control to HAProxy without executing the remaining code. It can be seen as a multi-level "return". -.. js:function:: core.wait() +.. js:function:: core.wait([milliseconds]) **context**: task, action Give back the hand at the HAProxy scheduler. Unlike :js:func:`core.yield` the task will not be woken up automatically to resume as fast as possible. - Instead, it will wait for an event to wake the task. + Instead, it will wait for an event to wake the task. If milliseconds argument + is provided then the Lua excecution will be automatically resumed passed this + delay even if no event caused the task to wake itself up. + + :param integer milliseconds: automatic wakeup passed this delay. (optional) .. js:function:: core.yield() diff --git a/src/hlua.c b/src/hlua.c index ac253361b..2e7c93bfb 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -9251,10 +9251,23 @@ __LJMP static int hlua_yield(lua_State *L) * automatically woken up to resume ASAP, instead it means we will wait for * an event to occur to wake the task. Only use when you're confident that * something or someone will wake the task at some point. + * + * Takes optional argument as parameter, in which case an automatic + * wake will be performed after */ __LJMP static int hlua_wait(lua_State *L) { - MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, 0)); + int nb_arg; + unsigned int delay; + int wakeup_ms = TICK_ETERNITY; // tick value + + nb_arg = lua_gettop(L); + if (nb_arg >= 1) { + delay = MAY_LJMP(luaL_checkinteger(L, 1)); + wakeup_ms = tick_add(now_ms, delay); + } + + MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, wakeup_ms, 0)); return 0; }