]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: backend: Add srv_queue converter
authorNenad Merdanovic <nmerdan@haproxy.com>
Mon, 26 Aug 2019 23:58:13 +0000 (01:58 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 27 Aug 2019 02:32:06 +0000 (04:32 +0200)
The converter can be useful to look up a server queue from a dynamic value.

It takes an input value of type string, either a server name or
<backend>/<server> format and returns the number of queued sessions
on that server. Can be used in places where we want to look up
queued sessions from a dynamic name, like a cookie value (e.g.
req.cook(SRVID),srv_queue) and then make a decision to break
persistence or direct a request elsewhere.

Signed-off-by: Nenad Merdanovic <nmerdan@haproxy.com>
doc/configuration.txt
src/backend.c

index 4e18f0f6e5584fcfe1da59bb879dae434badc8d6..20c39c7735384ecbeced67af10b805043044a8cf 100644 (file)
@@ -13622,6 +13622,13 @@ sha2([<bits>])
   Please note that this converter is only available when haproxy has been
   compiled with USE_OPENSSL.
 
+srv_queue
+  Takes an input value of type string, either a server name or <backend>/<server>
+  format and returns the number of queued sessions on that server. Can be used
+  in places where we want to look up queued sessions from a dynamic name, like a
+  cookie value (e.g. req.cook(SRVID),srv_queue) and then make a decision to break
+  persistence or direct a request elsewhere.
+
 strcmp(<var>)
   Compares the contents of <var> with the input value of type string. Returns
   the result as a signed integer compatible with strcmp(3): 0 if both strings
index 917b612b48324622787c45e8e90165f15ecdb50a..1b01536c1de9323ce785a7a7aa4e1213e1e85cbd 100644 (file)
@@ -2286,6 +2286,38 @@ static int sample_conv_nbsrv(const struct arg *args, struct sample *smp, void *p
        return 1;
 }
 
+static int
+sample_conv_srv_queue(const struct arg *args, struct sample *smp, void *private)
+{
+       struct proxy *px;
+       struct server *srv;
+       char *bksep;
+
+       if (!smp_make_safe(smp))
+               return 0;
+
+       bksep = strchr(smp->data.u.str.area, '/');
+
+       if (bksep) {
+               *bksep = '\0';
+               px = proxy_find_by_name(smp->data.u.str.area, PR_CAP_BE, 0);
+               if (!px)
+                       return 0;
+               smp->data.u.str.area = bksep + 1;
+       } else {
+               if (!(smp->px->cap & PR_CAP_BE))
+                       return 0;
+               px = smp->px;
+       }
+
+       srv = server_find_by_name(px, smp->data.u.str.area);
+       if (!srv)
+               return 0;
+
+       smp->data.type = SMP_T_SINT;
+       smp->data.u.sint = srv->nbpend;
+       return 1;
+}
 
 /* Note: must not be declared <const> as its list will be overwritten.
  * Please take care of keeping this list alphabetically sorted.
@@ -2313,7 +2345,8 @@ INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
 
 /* Note: must not be declared <const> as its list will be overwritten */
 static struct sample_conv_kw_list sample_conv_kws = {ILH, {
-       { "nbsrv", sample_conv_nbsrv, 0, NULL, SMP_T_STR, SMP_T_SINT },
+       { "nbsrv",     sample_conv_nbsrv,     0, NULL, SMP_T_STR, SMP_T_SINT },
+       { "srv_queue", sample_conv_srv_queue, 0, NULL, SMP_T_STR, SMP_T_SINT },
        { /* END */ },
 }};