]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: hlua: Save the lua socket's timeout in its context
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 27 Sep 2023 15:34:24 +0000 (17:34 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 4 Oct 2023 13:34:03 +0000 (15:34 +0200)
When the lua socket timeout is set, it is now saved in its context. If there
is already a stream attached to the appctx, the timeout is then immediately
modified. Otherwise, it is modified when the stream is created, thus during
the appctx initialization.

For now, the appctx is initialized when it is created. But this will change
to fix issues with the lua sockets. Thus, this patch is mandatory.

src/hlua.c

index 34af6023ab20cdcad5bb427502b269e01ee0788c..f9f7c300685fb87dbd1994f28250aefa597e5ea3 100644 (file)
@@ -278,6 +278,7 @@ struct hlua_csk_ctx {
        struct list wake_on_read;
        struct list wake_on_write;
        struct appctx *appctx;
+       int timeout;
        int die;
 };
 
@@ -2327,6 +2328,7 @@ static void hlua_socket_handler(struct appctx *appctx)
 
 static int hlua_socket_init(struct appctx *appctx)
 {
+       struct hlua_csk_ctx *csk_ctx = appctx->svcctx;
        struct stream *s;
 
        if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1)
@@ -2344,6 +2346,12 @@ static int hlua_socket_init(struct appctx *appctx)
        s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
        s->target = &socket_tcp->obj_type;
 
+       if (csk_ctx->timeout) {
+               s->sess->fe->timeout.connect = csk_ctx->timeout;
+               s->scf->ioto = csk_ctx->timeout;
+               s->scb->ioto = csk_ctx->timeout;
+       }
+
        return 0;
 
   error:
@@ -3273,6 +3281,8 @@ __LJMP static int hlua_socket_settimeout(struct lua_State *L)
        int tmout;
        double dtmout;
        struct xref *peer;
+       struct hlua_csk_ctx *csk_ctx;
+       struct appctx *appctx;
        struct stream *s;
 
        MAY_LJMP(check_args(L, 2, "settimeout"));
@@ -3307,17 +3317,25 @@ __LJMP static int hlua_socket_settimeout(struct lua_State *L)
                return 0;
        }
 
-       s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx);
+       csk_ctx = container_of(peer, struct hlua_csk_ctx, xref);
+       csk_ctx->timeout = tmout;
+
+       appctx = csk_ctx->appctx;
+       if (!appctx_sc(appctx))
+               goto end;
+
+       s = appctx_strm(csk_ctx->appctx);
 
        s->sess->fe->timeout.connect = tmout;
        s->scf->ioto = tmout;
        s->scb->ioto = tmout;
 
-       s->task->expire = tick_add_ifset(now_ms, tmout);
+       s->task->expire = (tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire);
+       s->task->expire = tick_first(s->task->expire, tick_add_ifset(now_ms, tmout));
        task_queue(s->task);
 
+  end:
        xref_unlock(&socket->xref, peer);
-
        lua_pushinteger(L, 1);
        return 1;
 }
@@ -3360,6 +3378,7 @@ __LJMP static int hlua_socket_new(lua_State *L)
        ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
        ctx->connected = 0;
        ctx->die = 0;
+       ctx->timeout = 0;
        ctx->appctx = appctx;
        LIST_INIT(&ctx->wake_on_write);
        LIST_INIT(&ctx->wake_on_read);