Added set-timeout for frontend side of session, so it can be used to set
custom per-client timeouts if needed. Added cur_client_timeout to fetch
client timeout samples.
---
 doc/configuration.txt                      | 17 +++++---
 include/haproxy/action-t.h                 |  1 +
 include/haproxy/action.h                   |  3 +-
 reg-tests/http-set-timeout/set_timeout.vtc | 46 +++++++++++++++++++++-
 src/action.c                               | 27 ++++++++++---
 src/http_act.c                             |  8 +---
 src/stream.c                               | 16 ++++++++
 7 files changed, 99 insertions(+), 19 deletions(-)

diff --git a/doc/configuration.txt b/doc/configuration.txt
index d49d359a2..db6ed866a 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -7010,7 +7010,7 @@ http-request <action> [options...] [ { if | unless } 
<condition> ]
     - set-query <fmt>
     - set-src <expr>
     - set-src-port <expr>
-    - set-timeout { server | tunnel } { <timeout> | <expr> }
+    - set-timeout { client | server | tunnel } { <timeout> | <expr> }
     - set-tos <tos>
     - set-uri <fmt>
     - set-var(<var-name>[,<cond>...]) <expr>
@@ -7925,10 +7925,10 @@ http-request set-src-port <expr> [ { if | unless } 
<condition> ]
   the address family supports a port, otherwise it forces the source address to
   IPv4 "0.0.0.0" before rewriting the port.
 
-http-request set-timeout { server | tunnel } { <timeout> | <expr> }
+http-request set-timeout { client | server | tunnel } { <timeout> | <expr> }
                                        [ { if | unless } <condition> ]
 
-  This action overrides the specified "server" or "tunnel" timeout for the
+  This action overrides the specified "client", "server" or "tunnel" timeout 
for the
   current stream only. The timeout can be specified in millisecond or with any
   other unit if the number is suffixed by the unit as explained at the top of
   this document. It is also possible to write an expression which must returns
@@ -7936,8 +7936,8 @@ http-request set-timeout { server | tunnel } { <timeout> 
| <expr> }
 
   Note that the server/tunnel timeouts are only relevant on the backend side
   and thus this rule is only available for the proxies with backend
-  capabilities. Also the timeout value must be non-null to obtain the expected
-  results.
+  capabilities. As well as client timeout is only relevant for frontend side.
+  Also the timeout value must be non-null to obtain the expected results.
 
   Example:
     http-request set-timeout tunnel 5s
@@ -20084,6 +20084,11 @@ cur_tunnel_timeout : integer
   In the default case, this will be equal to be_tunnel_timeout unless a
   "set-timeout" rule has been applied. See also "be_tunnel_timeout".
 
+cur_client_timeout : integer
+  Returns the currently applied client timeout in millisecond for the stream.
+  In the default case, this will be equal to fe_client_timeout unless a
+  "set-timeout" rule has been applied. See also "fe_client_timeout".
+
 dst : ip
   This is the destination IP address of the connection on the client side,
   which is the address the client connected to. Any tcp/http rules may alter
@@ -20339,7 +20344,7 @@ fe_name : string
 
 fe_client_timeout : integer
   Returns the configuration value in millisecond for the client timeout of the
-  current frontend.
+  current frontend. This timeout can be overwritten by a "set-timeout" rule.
 
 res.timer.data : integer
   this is the total transfer time of the response payload till the last byte
diff --git a/include/haproxy/action-t.h b/include/haproxy/action-t.h
index 7fafd612a..f77bdce5f 100644
--- a/include/haproxy/action-t.h
+++ b/include/haproxy/action-t.h
@@ -99,6 +99,7 @@ enum act_name {
 enum act_timeout_name {
        ACT_TIMEOUT_SERVER,
        ACT_TIMEOUT_TUNNEL,
+       ACT_TIMEOUT_CLIENT,
 };
 
 enum act_normalize_uri {
diff --git a/include/haproxy/action.h b/include/haproxy/action.h
index 8a35664f4..73d0fdd2b 100644
--- a/include/haproxy/action.h
+++ b/include/haproxy/action.h
@@ -105,7 +105,8 @@ int check_capture(struct act_rule *rule, struct proxy *px, 
char **err);
 int cfg_parse_rule_set_timeout(const char **args, int idx, int *out_timeout,
                                enum act_timeout_name *name,
                                struct sample_expr **expr, char **err,
-                               const char *file, int line, struct arg_list 
*al);
+                               const char *file, int line, struct arg_list *al,
+                                                          char *px_id, char 
*px_cap);
 
 static inline void release_timeout_action(struct act_rule *rule)
 {
diff --git a/reg-tests/http-set-timeout/set_timeout.vtc 
b/reg-tests/http-set-timeout/set_timeout.vtc
index ebaa6a3b4..6fa0a35f1 100644
--- a/reg-tests/http-set-timeout/set_timeout.vtc
+++ b/reg-tests/http-set-timeout/set_timeout.vtc
@@ -4,7 +4,7 @@ feature ignore_unknown_macro
 
 #REQUIRE_VERSION=2.4
 
-server srv_h1 -repeat 3 {
+server srv_h1 -repeat 5 {
     rxreq
     txresp
 } -start
@@ -24,6 +24,16 @@ syslog Slog3 -level info {
     expect ~ "^.*timeout: 5000 3000.*$"
 } -start
 
+syslog Slog4 -level info {
+    recv
+    expect ~ "^.*timeout: 5000 5000.*$"
+} -start
+
+syslog Slog5 -level info {
+    recv
+    expect ~ "^.*timeout: 5000 3000.*$"
+} -start
+
 haproxy hap -conf {
     defaults
         timeout connect 5s
@@ -46,6 +56,14 @@ haproxy hap -conf {
         http-request set-timeout server 5s
        server srv_h1 ${srv_h1_addr}:${srv_h1_port}
 
+    listen li3
+        mode http
+        bind "fd@${li3}"
+        log-format "timeout: %[fe_client_timeout] %[cur_client_timeout]"
+        log ${Slog4_addr}:${Slog4_port} len 2048 local0 debug err
+        http-request set-timeout client 5s
+       server srv_h1 ${srv_h1_addr}:${srv_h1_port}
+
     frontend fe1
         mode http
         bind "fd@${fe1}"
@@ -57,6 +75,18 @@ haproxy hap -conf {
        mode http
        http-request set-timeout server int(3),mul(1000)
        server srv_h1 ${srv_h1_addr}:${srv_h1_port}
+
+    frontend fe2
+        mode http
+        bind "fd@${fe2}"
+        log-format "timeout: %[fe_client_timeout] %[cur_client_timeout]"
+        log ${Slog5_addr}:${Slog5_port} len 2048 local0 debug err
+        http-request set-timeout client int(3),mul(1000)
+        default_backend be2
+
+    backend be2
+       mode http
+       server srv_h1 ${srv_h1_addr}:${srv_h1_port}
 } -start
 
 client c1 -connect ${hap_li1_sock} {
@@ -77,6 +107,20 @@ client c3 -connect ${hap_fe1_sock} {
     expect resp.status == 200
 } -run
 
+client c4 -connect ${hap_li3_sock} {
+    txreq
+    rxresp
+    expect resp.status == 200
+} -run
+
+client c5 -connect ${hap_fe2_sock} {
+    txreq
+    rxresp
+    expect resp.status == 200
+} -run
+
 syslog Slog1 -wait
 syslog Slog2 -wait
 syslog Slog3 -wait
+syslog Slog4 -wait
+syslog Slog5 -wait
diff --git a/src/action.c b/src/action.c
index 9d3bfe4b9..c5b00336c 100644
--- a/src/action.c
+++ b/src/action.c
@@ -174,9 +174,10 @@ int act_resolution_error_cb(struct resolv_requester 
*requester, int error_code)
 }
 
 /* Parse a set-timeout rule statement. It first checks if the timeout name is
- * valid and returns it in <name>. Then the timeout is parsed as a plain value
- * and * returned in <out_timeout>. If there is a parsing error, the value is
- * reparsed as an expression and returned in <expr>.
+ * valid and proxy is capable of handling it, and returns it in <name>.
+ * Then the timeout is parsed as a plain value and * returned in <out_timeout>.
+ * If there is a parsing error, the value is reparsed as an expression and
+ * returned in <expr>.
  *
  * Returns -1 if the name is invalid or neither a time or an expression can be
  * parsed, or if the timeout value is 0.
@@ -184,20 +185,36 @@ int act_resolution_error_cb(struct resolv_requester 
*requester, int error_code)
 int cfg_parse_rule_set_timeout(const char **args, int idx, int *out_timeout,
                                enum act_timeout_name *name,
                                struct sample_expr **expr, char **err,
-                               const char *file, int line, struct arg_list *al)
+                               const char *file, int line, struct arg_list *al,
+                                                          char *px_id, char 
*px_cap)
 {
        const char *res;
        const char *timeout_name = args[idx++];
 
        if (strcmp(timeout_name, "server") == 0) {
+               if (!(*px_cap & PR_CAP_BE)) {
+                       memprintf(err, "'%s' has no backend capability", px_id);
+                       return -1;
+               }
                *name = ACT_TIMEOUT_SERVER;
        }
        else if (strcmp(timeout_name, "tunnel") == 0) {
+               if (!(*px_cap & PR_CAP_BE)) {
+                       memprintf(err, "'%s' has no backend capability", px_id);
+                       return -1;
+               }
                *name = ACT_TIMEOUT_TUNNEL;
        }
+       else if (strcmp(timeout_name, "client") == 0) {
+               if (!(*px_cap & PR_CAP_FE)) {
+                       memprintf(err, "'%s' has no frontend capability", 
px_id);
+                       return -1;
+               }
+               *name = ACT_TIMEOUT_CLIENT;
+       }
        else {
                memprintf(err,
-                         "'set-timeout' rule supports 'server'/'tunnel' (got 
'%s')",
+                         "'set-timeout' rule supports 
'server'/'tunnel'/'client' (got '%s')",
                          timeout_name);
                return -1;
        }
diff --git a/src/http_act.c b/src/http_act.c
index d168cf5e0..da160c3f0 100644
--- a/src/http_act.c
+++ b/src/http_act.c
@@ -2192,18 +2192,14 @@ static enum act_parse_ret parse_http_set_timeout(const 
char **args,
                return ACT_RET_PRS_ERR;
        }
 
-       if (!(px->cap & PR_CAP_BE)) {
-               memprintf(err, "proxy '%s' has no backend capability", px->id);
-               return ACT_RET_PRS_ERR;
-       }
-
        if (cfg_parse_rule_set_timeout(args, cur_arg,
                                       &rule->arg.timeout.value,
                                       &rule->arg.timeout.type,
                                       &rule->arg.timeout.expr,
                                       err,
                                       px->conf.args.file,
-                                      px->conf.args.line, &px->conf.args) == 
-1) {
+                                      px->conf.args.line, &px->conf.args,
+                                                                  px->id, 
&px->cap) == -1) {
                return ACT_RET_PRS_ERR;
        }
 
diff --git a/src/stream.c b/src/stream.c
index 45b0c56d9..d59d28f59 100644
--- a/src/stream.c
+++ b/src/stream.c
@@ -867,6 +867,10 @@ void stream_retnclose(struct stream *s, const struct 
buffer *msg)
 int stream_set_timeout(struct stream *s, enum act_timeout_name name, int 
timeout)
 {
        switch (name) {
+       case ACT_TIMEOUT_CLIENT:
+               s->scf->ioto = timeout;
+               return 1;
+
        case ACT_TIMEOUT_SERVER:
                s->scb->ioto = timeout;
                return 1;
@@ -3939,6 +3943,17 @@ static struct action_kw_list 
stream_http_after_res_actions =  { ILH, {
 
 INITCALL1(STG_REGISTER, http_after_res_keywords_register, 
&stream_http_after_res_actions);
 
+static int smp_fetch_cur_client_timeout(const struct arg *args, struct sample 
*smp, const char *km, void *private)
+{
+       smp->flags = SMP_F_VOL_TXN;
+       smp->data.type = SMP_T_SINT;
+       if (!smp->strm)
+               return 0;
+
+       smp->data.u.sint = TICKS_TO_MS(smp->strm->scf->ioto);
+       return 1;
+}
+
 static int smp_fetch_cur_server_timeout(const struct arg *args, struct sample 
*smp, const char *km, void *private)
 {
        smp->flags = SMP_F_VOL_TXN;
@@ -3989,6 +4004,7 @@ static int smp_fetch_last_rule_line(const struct arg 
*args, struct sample *smp,
  * Please take care of keeping this list alphabetically sorted.
  */
 static struct sample_fetch_kw_list smp_kws = {ILH, {
+       { "cur_client_timeout", smp_fetch_cur_client_timeout, 0, NULL, 
SMP_T_SINT, SMP_USE_FTEND, },
        { "cur_server_timeout", smp_fetch_cur_server_timeout, 0, NULL, 
SMP_T_SINT, SMP_USE_BKEND, },
        { "cur_tunnel_timeout", smp_fetch_cur_tunnel_timeout, 0, NULL, 
SMP_T_SINT, SMP_USE_BKEND, },
        { "last_rule_file",     smp_fetch_last_rule_file,     0, NULL, 
SMP_T_STR,  SMP_USE_INTRN, },
-- 
2.42.0


Reply via email to