]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: httpclient/lua: ability to set a server timeout
authorWilliam Lallemand <wlallemand@haproxy.org>
Wed, 23 Feb 2022 13:18:16 +0000 (14:18 +0100)
committerWilliam Lallemand <wlallemand@haproxy.org>
Wed, 23 Feb 2022 14:11:11 +0000 (15:11 +0100)
Add the ability to set a "server timeout" on the httpclient with either
the httpclient_set_timeout() API or the timeout argument in a request.

Issue #1470.

doc/lua-api/index.rst
include/haproxy/http_client-t.h
include/haproxy/http_client.h
src/hlua.c
src/http_client.c

index 9aed82c4a2656c73784713287498f635381ad0b1..f22fac3fba7a3fe0b0c5990671fa4af7a3edc290 100644 (file)
@@ -1870,6 +1870,7 @@ HTTPClient class
   :param string request.body: Is an optional parameter for the request that contains the body to send.
   :param table request.headers: Is an optional parameter for the request that contains the headers to send.
   :param table request.dst: Is an optional parameter for the destination in haproxy address format.
+  :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections.
   :returns: Lua table containing the response
 
 
index 8cebc70a32fa2a592a6a1ae7d88fb73b79df531b..6efb671a92265f3b8951a4e2eaf0e20570b27ea9 100644 (file)
@@ -29,6 +29,7 @@ struct httpclient {
        } ops;
        struct sockaddr_storage *dst;         /* destination address */
        struct appctx *appctx;                /* HTTPclient appctx */
+       int timeout_server;                   /* server timeout in ms */
        void *caller;                         /* ptr of the caller */
        unsigned int flags;                   /* other flags */
 };
index 96ec09294fc4b99331b6a65dae849538da00fdfc..cc4c9fe6259cfb52936bd2605ea1edadbac6d78f 100644 (file)
@@ -9,6 +9,7 @@ struct httpclient *httpclient_new(void *caller, enum http_meth_t meth, struct is
 
 struct appctx *httpclient_start(struct httpclient *hc);
 int httpclient_set_dst(struct httpclient *hc, const char *dst);
+void httpclient_set_timeout(struct httpclient *hc, int timeout);
 int httpclient_res_xfer(struct httpclient *hc, struct buffer *dst);
 int httpclient_req_gen(struct httpclient *hc, const struct ist url, enum http_meth_t meth, const struct http_hdr *hdrs, const struct ist payload);
 int httpclient_req_xfer(struct httpclient *hc, struct ist src, int end);
index 0098d02b8fb54a687dc4ff9c4b1da0420ee94030..323a86bcd18ef7629c9c05acac443a5317c4c9ac 100644 (file)
@@ -7218,6 +7218,7 @@ __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth)
        struct hlua *hlua;
        const char *url_str = NULL;
        const char *body_str = NULL;
+       int timeout;
        size_t buf_len;
        int ret;
 
@@ -7245,6 +7246,12 @@ __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth)
        }
        lua_pop(L, 1);
 
+       ret = lua_getfield(L, -1, "timeout");
+       if (ret == LUA_TNUMBER) {
+               timeout = lua_tointeger(L, -1);
+               httpclient_set_timeout(hlua_hc->hc, timeout);
+       }
+
        ret = lua_getfield(L, -1, "headers");
        if (ret == LUA_TTABLE) {
                hdrs = hlua_httpclient_table_to_hdrs(L);
index f2b60ae2fb5deb1bf8b2e6d44482058e49912df2..26bfaa8fce06318fc1f6f00536f4a14339059347 100644 (file)
@@ -405,6 +405,12 @@ error:
        return ret;
 }
 
+/* Set the 'timeout server' in ms for the next httpclient request */
+void httpclient_set_timeout(struct httpclient *hc, int timeout)
+{
+       hc->timeout_server = timeout;
+}
+
 /*
  * Sets a destination for the httpclient from an HAProxy addr format
  * This will prevent to determine the destination from the URL
@@ -484,6 +490,10 @@ struct appctx *httpclient_start(struct httpclient *hc)
                goto out_free_appctx;
        }
 
+       /* set the "timeout server" */
+       s->req.wto = hc->timeout_server;
+       s->res.rto = hc->timeout_server;
+
        /* if httpclient_set_dst() was used, sets the alternative address */
        if (hc->dst)
                ss_dst = hc->dst;