return task;
}
+/* Helper function to prepare the lua ctx for a given stream
+ *
+ * ctx will be enforced in <state_id> parent stack on initial creation
+ *
+ * Returns 1 for success and 0 for failure
+ */
+static int hlua_stream_ctx_prepare(struct stream *s, int state_id)
+{
+ /* In the execution wrappers linked with a stream, the
+ * Lua context can be not initialized. This behavior
+ * permits to save performances because a systematic
+ * Lua initialization cause 5% performances loss.
+ */
+ if (!s->hlua) {
+ struct hlua *hlua;
+
+ hlua = pool_alloc(pool_head_hlua);
+ if (!hlua)
+ return 0;
+ HLUA_INIT(hlua);
+ if (!hlua_ctx_init(hlua, state_id, s->task)) {
+ pool_free(pool_head_hlua, hlua);
+ return 0;
+ }
+ s->hlua = hlua;
+ }
+ return 1;
+}
+
/* This function is an LUA binding that register LUA function to be
* executed after the HAProxy configuration parsing and before the
* HAProxy scheduler starts. This function expect only one LUA
if (!stream)
return 0;
- /* In the execution wrappers linked with a stream, the
- * Lua context can be not initialized. This behavior
- * permits to save performances because a systematic
- * Lua initialization cause 5% performances loss.
- */
- if (!stream->hlua) {
- struct hlua *hlua;
-
- hlua = pool_alloc(pool_head_hlua);
- if (!hlua) {
- SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
- return 0;
- }
- HLUA_INIT(hlua);
- stream->hlua = hlua;
- if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task)) {
- SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
- return 0;
- }
+ if (!hlua_stream_ctx_prepare(stream, fcn_ref_to_stack_id(fcn))) {
+ SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
+ return 0;
}
/* If it is the first run, initialize the data for the call. */
if (!stream)
return 0;
- /* In the execution wrappers linked with a stream, the
- * Lua context can be not initialized. This behavior
- * permits to save performances because a systematic
- * Lua initialization cause 5% performances loss.
- */
- if (!stream->hlua) {
- struct hlua *hlua;
-
- hlua = pool_alloc(pool_head_hlua);
- if (!hlua) {
- SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
- return 0;
- }
- hlua->T = NULL;
- stream->hlua = hlua;
- if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task)) {
- SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
- return 0;
- }
+ if (!hlua_stream_ctx_prepare(stream, fcn_ref_to_stack_id(fcn))) {
+ SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
+ return 0;
}
/* If it is the first run, initialize the data for the call. */
goto end;
}
- /* In the execution wrappers linked with a stream, the
- * Lua context can be not initialized. This behavior
- * permits to save performances because a systematic
- * Lua initialization cause 5% performances loss.
- */
- if (!s->hlua) {
- struct hlua *hlua;
-
- hlua = pool_alloc(pool_head_hlua);
- if (!hlua) {
- SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
- rule->arg.hlua_rule->fcn->name);
- goto end;
- }
- HLUA_INIT(hlua);
- s->hlua = hlua;
- if (!hlua_ctx_init(s->hlua, fcn_ref_to_stack_id(rule->arg.hlua_rule->fcn), s->task)) {
- SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
- rule->arg.hlua_rule->fcn->name);
- goto end;
- }
+ if (!hlua_stream_ctx_prepare(s, fcn_ref_to_stack_id(rule->arg.hlua_rule->fcn))) {
+ SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
+ rule->arg.hlua_rule->fcn->name);
+ goto end;
}
/* If it is the first run, initialize the data for the call. */
struct hlua_flt_ctx *flt_ctx = NULL;
int ret = 1;
- /* In the execution wrappers linked with a stream, the
- * Lua context can be not initialized. This behavior
- * permits to save performances because a systematic
- * Lua initialization cause 5% performances loss.
- */
- if (!s->hlua) {
- struct hlua *hlua;
-
- hlua = pool_alloc(pool_head_hlua);
- if (!hlua) {
- SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n",
- conf->reg->name);
- ret = 0;
- goto end;
- }
- HLUA_INIT(hlua);
- s->hlua = hlua;
- if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task)) {
- SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n",
- conf->reg->name);
- ret = 0;
- goto end;
- }
+ if (!hlua_stream_ctx_prepare(s, reg_flt_to_stack_id(conf->reg))) {
+ SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n",
+ conf->reg->name);
+ ret = 0;
+ goto end;
}
flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx);