]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: lua: Add act:wake_time() function to set a timeout when an action yields
authorChristopher Faulet <cfaulet@haproxy.com>
Fri, 31 Jan 2020 18:07:52 +0000 (19:07 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Thu, 6 Feb 2020 14:13:04 +0000 (15:13 +0100)
This function may be used to defined a timeout when a lua action returns
act:YIELD. It is a way to force to reexecute the script after a short time
(defined in milliseconds).

Unlike core:sleep() or core:yield(), the script is fully reexecuted if it
returns act:YIELD. With core functions to yield, the script is interrupted and
restarts from the yield point. When a script returns act:YIELD, it is finished
but the message analysis is blocked on the action waiting its end.

doc/lua-api/index.rst
src/hlua.c

index b50551c84b7294fee153454fc2945862272fa7b2..3aeb8d16ff489416611ba9884fa3797ba4057e3a 100644 (file)
@@ -2642,6 +2642,18 @@ Action class
 
   :returns: integer
 
+.. js:function:: act:wake_time(milliseconds)
+
+  **context**: action
+
+  Set the script pause timeout to the specified time, defined in
+  milliseconds.
+
+  :param integer milliseconds: the required milliseconds.
+
+  This function may be used when a lua action returns `act.YIELD`, to force its
+  wake-up at most after the specified number of milliseconds.
+
 External Lua libraries
 ======================
 
index 916530658b813d8dd5e92418bab5fd8efdff2038..4cde68c3d99329a17d2b7836ac2b760114451fd0 100644 (file)
@@ -6053,6 +6053,24 @@ __LJMP static int hlua_register_fetches(lua_State *L)
        return 0;
 }
 
+/* This function is a lua binding to set the wake_time from an action. It is
+ * only used if the action return ACT_RET_YIELD.
+ */
+__LJMP static int hlua_action_wake_time(lua_State *L)
+{
+       struct hlua *hlua = hlua_gethlua(L);
+       unsigned int delay;
+       unsigned int wakeup_ms;
+
+       MAY_LJMP(check_args(L, 1, "wake_time"));
+
+       delay = MAY_LJMP(luaL_checkinteger(L, 1));
+       wakeup_ms = tick_add(now_ms, delay);
+       hlua->wake_time = wakeup_ms;
+       return 0;
+}
+
+
 /* This function is a wrapper to execute each LUA function declared as an action
  * wrapper during the initialisation period. This function may return any
  * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
@@ -6157,6 +6175,15 @@ static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
                if (lua_gettop(s->hlua->T) > 0)
                        act_ret = lua_tointeger(s->hlua->T, -1);
 
+               /* Set timeout in the required channel. */
+               if (act_ret == ACT_RET_YIELD && s->hlua->wake_time != TICK_ETERNITY) {
+                       if (dir == SMP_OPT_DIR_REQ)
+                               s->req.analyse_exp = s->hlua->wake_time;
+                       else
+                               s->res.analyse_exp = s->hlua->wake_time;
+               }
+               goto end;
+
        /* yield. */
        case HLUA_E_AGAIN:
                /* Set timeout in the required channel. */
@@ -7637,6 +7664,8 @@ void hlua_init(void)
        hlua_class_const_int(gL.T, "ABORT",    ACT_RET_ABRT);
        hlua_class_const_int(gL.T, "INVALID",  ACT_RET_INV);
 
+       hlua_class_function(gL.T, "wake_time", hlua_action_wake_time);
+
        lua_setglobal(gL.T, "act");
 
        /*