On 2018/8/22 04:05, Willy Tarreau wrote:
> On Thu, Aug 09, 2018 at 06:46:29PM -0400, Patrick Hemmer wrote:
>> This adds the 'srv_conn_free([<backend>/]<server>)' sample fetch. This fetch
>> provides the number of available connections on the designated server.
> Fine with this as well, though just like with the previous one, I
> disagree with this special case of -1 and would rather only count
> the really available connections (i.e. 0 if it's not possible to
> use the server).
>
> Willy

Adjusted from previous submission to handle dynamic maxconn, maxconn <
currconn, and cleanup documentation note.

-Patrick
From 2e3a908f229a1fcc11381c602aa131284b165a63 Mon Sep 17 00:00:00 2001
From: Patrick Hemmer <[email protected]>
Date: Thu, 14 Jun 2018 18:01:35 -0400
Subject: [PATCH] MINOR: Add srv_conn_free sample fetch

This adds the 'srv_conn_free([<backend>/]<server>)' sample fetch. This fetch
provides the number of available connections on the designated server.
---
 doc/configuration.txt | 19 ++++++++++++++++---
 src/backend.c         | 28 ++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/doc/configuration.txt b/doc/configuration.txt
index 6eec8c10b..513ef0c49 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -13690,7 +13690,8 @@ be_conn_free([<backend>]) : integer
   servers are also not included, unless all other servers are down. If no
   backend name is specified, the current one is used. But it is also possible
   to check another backend. It can be used to use a specific farm when the
-  nominal one is full. See also the "be_conn" and "connslots" criteria.
+  nominal one is full. See also the "be_conn", "connslots", and "srv_conn_free"
+  criteria.
 
   OTHER CAVEATS AND NOTES: if any of the server maxconn, or maxqueue is 0
   (meaning unlimited), then this fetch clearly does not make sense, in which
@@ -13908,8 +13909,20 @@ srv_conn([<backend>/]<server>) : integer
   evaluated. If <backend> is omitted, then the server is looked up in the
   current backend. It can be used to use a specific farm when one server is
   full, or to inform the server about our view of the number of active
-  connections with it. See also the "fe_conn", "be_conn" and "queue" fetch
-  methods.
+  connections with it. See also the "fe_conn", "be_conn", "queue", and
+  "srv_conn_free" fetch methods.
+
+srv_conn_free([<backend>/]<server>) : integer
+  Returns an integer value corresponding to the number of available connections
+  on the designated server, possibly including the connection being evaluated.
+  The value does not include queue slots. If <backend> is omitted, then the
+  server is looked up in the current backend. It can be used to use a specific
+  farm when one server is full, or to inform the server about our view of the
+  number of active connections with it. See also the "be_conn_free" and
+  "srv_conn" fetch methods.
+
+  OTHER CAVEATS AND NOTES: If the server maxconn is 0, then this fetch clearly
+  does not make sense, in which case the value returned will be -1.
 
 srv_is_up([<backend>/]<server>) : boolean
   Returns true when the designated server is UP, and false when it is either
diff --git a/src/backend.c b/src/backend.c
index 01bd4b161..5a22b0fd0 100644
--- a/src/backend.c
+++ b/src/backend.c
@@ -1886,6 +1886,33 @@ smp_fetch_srv_conn(const struct arg *args, struct sample 
*smp, const char *kw, v
        return 1;
 }
 
+/* set temp integer to the number of available connections on the server in 
the backend.
+ * Accepts exactly 1 argument. Argument is a server, other types will lead to
+ * undefined behaviour.
+ */
+static int
+smp_fetch_srv_conn_free(const struct arg *args, struct sample *smp, const char 
*kw, void *private)
+{
+       unsigned int maxconn;
+
+       smp->flags = SMP_F_VOL_TEST;
+       smp->data.type = SMP_T_SINT;
+
+       if (args->data.srv->maxconn == 0) {
+                       /* one active server is unlimited, return -1 */
+                       smp->data.u.sint = -1;
+                       return 1;
+       }
+
+       maxconn = srv_dynamic_maxconn(args->data.srv);
+       if (maxconn > args->data.srv->cur_sess)
+               smp->data.u.sint = maxconn - args->data.srv->cur_sess;
+       else
+               smp->data.u.sint = 0;
+
+       return 1;
+}
+
 /* set temp integer to the number of connections pending in the server's queue.
  * Accepts exactly 1 argument. Argument is a server, other types will lead to
  * undefined behaviour.
@@ -1945,6 +1972,7 @@ static struct sample_fetch_kw_list smp_kws = {ILH, {
        { "nbsrv",         smp_fetch_nbsrv,          ARG1(1,BE),  NULL, 
SMP_T_SINT, SMP_USE_INTRN, },
        { "queue",         smp_fetch_queue_size,     ARG1(1,BE),  NULL, 
SMP_T_SINT, SMP_USE_INTRN, },
        { "srv_conn",      smp_fetch_srv_conn,       ARG1(1,SRV), NULL, 
SMP_T_SINT, SMP_USE_INTRN, },
+       { "srv_conn_free", smp_fetch_srv_conn_free,  ARG1(1,SRV), NULL, 
SMP_T_SINT, SMP_USE_INTRN, },
        { "srv_id",        smp_fetch_srv_id,         0,           NULL, 
SMP_T_SINT, SMP_USE_SERVR, },
        { "srv_is_up",     smp_fetch_srv_is_up,      ARG1(1,SRV), NULL, 
SMP_T_BOOL, SMP_USE_INTRN, },
        { "srv_queue",     smp_fetch_srv_queue,      ARG1(1,SRV), NULL, 
SMP_T_SINT, SMP_USE_INTRN, },
-- 
2.18.0

Reply via email to