MINOR: applet: Make .init callback more generic

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.
This commit is contained in:
Christopher Faulet 2022-01-13 16:01:35 +01:00
parent 91449b0351
commit 4aa1d2838c
4 changed files with 22 additions and 31 deletions

View 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;

View 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 */

View 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;

View 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);