]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: applet: Make .init callback more generic
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 13 Jan 2022 15:01:35 +0000 (16:01 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 13 Apr 2022 13:10:13 +0000 (15:10 +0200)
For now there is no much change. Only the appctx is passed as argument when
the .init callback function is called. And it is not possible to yield at
this stage. It is not a problem because the feature is not used. Only the
lua defines this callback function for the lua TCP/HTTP services. The idea
is to be able to use it for all applets to initialize the appctx context.

addons/promex/service-prometheus.c
include/haproxy/applet-t.h
src/hlua.c
src/stream.c

index 48bf1602d6a9c2f9e858ae5455f8966e1b9e246d..61bf4653fe8238ca80b590cdf81da4ccc61bab1f 100644 (file)
@@ -1454,7 +1454,7 @@ static int promex_send_headers(struct appctx *appctx, struct stream_interface *s
  * an errors occurs and -1 if more data are required for initializing
  * the applet.
  */
-static int promex_appctx_init(struct appctx *appctx, struct proxy *px, struct stream *strm)
+static int promex_appctx_init(struct appctx *appctx)
 {
        appctx->st0 = PROMEX_ST_INIT;
        return 1;
index da8dfc87d8cd6408c0338bb7efd89822df15a3ac..8c565dce734cf327fbe64ca2f5765fc37c775df6 100644 (file)
@@ -41,7 +41,7 @@ struct applet {
        enum obj_type obj_type;            /* object type = OBJ_TYPE_APPLET */
        /* 3 unused bytes here */
        char *name;                        /* applet's name to report in logs */
-       int (*init)(struct appctx *, struct proxy *px, struct stream *strm);   /* callback to init resources, may be NULL.
+       int (*init)(struct appctx *);   /* callback to init resources, may be NULL.
                                             expect 1 if ok, 0 if an error occurs, -1 if miss data. */
        void (*fct)(struct appctx *);      /* internal I/O handler, may never be NULL */
        void (*release)(struct appctx *);  /* callback to release resources, may be NULL */
index ee48323d55551eab4b8215f7086206b92deffd23..ecc0762013265dd90295022b9b3c56047a16e547 100644 (file)
@@ -9187,9 +9187,10 @@ struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int stat
        return t;
 }
 
-static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
+static int hlua_applet_tcp_init(struct appctx *ctx)
 {
        struct stream_interface *si = cs_si(ctx->owner);
+       struct stream *strm = si_strm(si);
        struct hlua *hlua;
        struct task *task;
        char **arg;
@@ -9197,7 +9198,7 @@ static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct str
 
        hlua = pool_alloc(pool_head_hlua);
        if (!hlua) {
-               SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
+               SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n",
                         ctx->rule->arg.hlua_rule->fcn->name);
                return 0;
        }
@@ -9208,7 +9209,7 @@ static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct str
        /* Create task used by signal to wakeup applets. */
        task = task_new_here();
        if (!task) {
-               SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
+               SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n",
                         ctx->rule->arg.hlua_rule->fcn->name);
                return 0;
        }
@@ -9223,7 +9224,7 @@ static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct str
         * Lua initialization cause 5% performances loss.
         */
        if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task, 0)) {
-               SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
+               SEND_ERR(strm->be, "Lua applet tcp '%s': can't initialize Lua context.\n",
                         ctx->rule->arg.hlua_rule->fcn->name);
                return 0;
        }
@@ -9237,14 +9238,14 @@ static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct str
                        error = lua_tostring(hlua->T, -1);
                else
                        error = "critical error";
-               SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
+               SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n",
                         ctx->rule->arg.hlua_rule->fcn->name, error);
                return 0;
        }
 
        /* Check stack available size. */
        if (!lua_checkstack(hlua->T, 1)) {
-               SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
+               SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n",
                         ctx->rule->arg.hlua_rule->fcn->name);
                RESET_SAFE_LJMP(hlua);
                return 0;
@@ -9255,7 +9256,7 @@ static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct str
 
        /* Create and and push object stream in the stack. */
        if (!hlua_applet_tcp_new(hlua->T, ctx)) {
-               SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
+               SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n",
                         ctx->rule->arg.hlua_rule->fcn->name);
                RESET_SAFE_LJMP(hlua);
                return 0;
@@ -9265,7 +9266,7 @@ static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct str
        /* push keywords in the stack. */
        for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
                if (!lua_checkstack(hlua->T, 1)) {
-                       SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
+                       SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n",
                                 ctx->rule->arg.hlua_rule->fcn->name);
                        RESET_SAFE_LJMP(hlua);
                        return 0;
@@ -9374,9 +9375,10 @@ static void hlua_applet_tcp_release(struct appctx *ctx)
  * an errors occurs and -1 if more data are required for initializing
  * the applet.
  */
-static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
+static int hlua_applet_http_init(struct appctx *ctx)
 {
        struct stream_interface *si = cs_si(ctx->owner);
+       struct stream *strm = si_strm(si);
        struct http_txn *txn;
        struct hlua *hlua;
        char **arg;
@@ -9386,7 +9388,7 @@ static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct st
        txn = strm->txn;
        hlua = pool_alloc(pool_head_hlua);
        if (!hlua) {
-               SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
+               SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n",
                         ctx->rule->arg.hlua_rule->fcn->name);
                return 0;
        }
@@ -9401,7 +9403,7 @@ static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct st
        /* Create task used by signal to wakeup applets. */
        task = task_new_here();
        if (!task) {
-               SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
+               SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n",
                         ctx->rule->arg.hlua_rule->fcn->name);
                return 0;
        }
@@ -9416,7 +9418,7 @@ static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct st
         * Lua initialization cause 5% performances loss.
         */
        if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task, 0)) {
-               SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
+               SEND_ERR(strm->be, "Lua applet http '%s': can't initialize Lua context.\n",
                         ctx->rule->arg.hlua_rule->fcn->name);
                return 0;
        }
@@ -9430,14 +9432,14 @@ static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct st
                        error = lua_tostring(hlua->T, -1);
                else
                        error = "critical error";
-               SEND_ERR(px, "Lua applet http '%s': %s.\n",
+               SEND_ERR(strm->be, "Lua applet http '%s': %s.\n",
                         ctx->rule->arg.hlua_rule->fcn->name, error);
                return 0;
        }
 
        /* Check stack available size. */
        if (!lua_checkstack(hlua->T, 1)) {
-               SEND_ERR(px, "Lua applet http '%s': full stack.\n",
+               SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n",
                         ctx->rule->arg.hlua_rule->fcn->name);
                RESET_SAFE_LJMP(hlua);
                return 0;
@@ -9448,7 +9450,7 @@ static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct st
 
        /* Create and and push object stream in the stack. */
        if (!hlua_applet_http_new(hlua->T, ctx)) {
-               SEND_ERR(px, "Lua applet http '%s': full stack.\n",
+               SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n",
                         ctx->rule->arg.hlua_rule->fcn->name);
                RESET_SAFE_LJMP(hlua);
                return 0;
@@ -9458,7 +9460,7 @@ static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct st
        /* push keywords in the stack. */
        for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
                if (!lua_checkstack(hlua->T, 1)) {
-                       SEND_ERR(px, "Lua applet http '%s': full stack.\n",
+                       SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n",
                                 ctx->rule->arg.hlua_rule->fcn->name);
                        RESET_SAFE_LJMP(hlua);
                        return 0;
index bca146a88f02e5a7f4029032f39a625fe6ab6b2a..3aeba1844d1674f815a5bd1e1e840d12a9e7fcef 100644 (file)
@@ -991,23 +991,12 @@ enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
                /* Initialise the context. */
                memset(&appctx->ctx, 0, sizeof(appctx->ctx));
                appctx->rule = rule;
+               if (appctx->applet->init && !appctx->applet->init(appctx))
+                       return ACT_RET_ERR;
        }
        else
                appctx = __cs_appctx(s->csb);
 
-       /* Stops the applet scheduling, in case of the init function miss
-        * some data.
-        */
-       si_stop_get(cs_si(s->csb));
-
-       /* Call initialisation. */
-       if (rule->applet.init)
-               switch (rule->applet.init(appctx, px, s)) {
-               case 0: return ACT_RET_ERR;
-               case 1: break;
-               default: return ACT_RET_YIELD;
-       }
-
        if (rule->from != ACT_F_HTTP_REQ) {
                if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
                        _HA_ATOMIC_INC(&sess->fe->fe_counters.intercepted_req);