MINOR: lua: add core.done() function

This function immediately give back the control to HAProxy core.
This commit is contained in:
Thierry FOURNIER 2015-08-26 00:14:17 +02:00 committed by Willy Tarreau
parent bc965348d7
commit 0a99b89531
3 changed files with 36 additions and 0 deletions

View File

@ -402,6 +402,19 @@ Core class
:returns: A socket class object. :returns: A socket class object.
.. js:function:: core.done(data)
**context**: body, init, task, action, sample-fetch, converter
:param any data: Return some data for the caller. It is useful with
sample-fetches and sample-converters.
Immediately stops the current Lua execution and returns to the caller which
may be a sample fetch, a converter or an action and returns the specified
value (ignored for actions). It is used when the LUA process finishes 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.yield() .. js:function:: core.yield()
**context**: task, action, sample-fetch, converter **context**: task, action, sample-fetch, converter

View File

@ -24,6 +24,7 @@ struct stream;
#define HLUA_CTRLYIELD 0x00000002 #define HLUA_CTRLYIELD 0x00000002
#define HLUA_WAKERESWR 0x00000004 #define HLUA_WAKERESWR 0x00000004
#define HLUA_WAKEREQWR 0x00000008 #define HLUA_WAKEREQWR 0x00000008
#define HLUA_EXIT 0x00000010
enum hlua_exec { enum hlua_exec {
HLUA_E_OK = 0, HLUA_E_OK = 0,

View File

@ -991,6 +991,16 @@ timeout_reached:
break; break;
case LUA_ERRRUN: case LUA_ERRRUN:
/* Special exit case. The traditionnal exit is returned as an error
* because the errors ares the only one mean to return immediately
* from and lua execution.
*/
if (lua->flags & HLUA_EXIT) {
ret = HLUA_E_OK;
break;
}
lua->wake_time = TICK_ETERNITY; lua->wake_time = TICK_ETERNITY;
if (!lua_checkstack(lua->T, 1)) { if (!lua_checkstack(lua->T, 1)) {
ret = HLUA_E_ERR; ret = HLUA_E_ERR;
@ -1070,6 +1080,17 @@ timeout_reached:
return ret; return ret;
} }
/* This function exit the current code. */
__LJMP static int hlua_done(lua_State *L)
{
struct hlua *hlua = hlua_gethlua(L);
hlua->flags |= HLUA_EXIT;
WILL_LJMP(lua_error(L));
return 0;
}
/* This function is an LUA binding. It provides a function /* This function is an LUA binding. It provides a function
* for deleting ACL from a referenced ACL file. * for deleting ACL from a referenced ACL file.
*/ */
@ -4704,6 +4725,7 @@ void hlua_init(void)
hlua_class_function(gL.T, "Info", hlua_log_info); hlua_class_function(gL.T, "Info", hlua_log_info);
hlua_class_function(gL.T, "Warning", hlua_log_warning); hlua_class_function(gL.T, "Warning", hlua_log_warning);
hlua_class_function(gL.T, "Alert", hlua_log_alert); hlua_class_function(gL.T, "Alert", hlua_log_alert);
hlua_class_function(gL.T, "done", hlua_done);
lua_setglobal(gL.T, "core"); lua_setglobal(gL.T, "core");