MINOR: httpclient/lua: ability to set a server timeout

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.
This commit is contained in:
William Lallemand 2022-02-23 14:18:16 +01:00
parent 686501cb1c
commit b4a4ef6a29
5 changed files with 20 additions and 0 deletions

View 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

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

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

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

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