]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: backend: Add srv_name sample fetche
authorvkill <vkill.net@gmail.com>
Wed, 30 Oct 2019 08:58:14 +0000 (16:58 +0800)
committerWilly Tarreau <w@1wt.eu>
Fri, 1 Nov 2019 04:40:24 +0000 (05:40 +0100)
The sample fetche can get srv_name without foreach
`core.backends["bk"].servers`.

Then we can get Server class quickly via
`core.backends[txn.f:be_name()].servers[txn.f:srv_name()]`.

Issue#342

doc/configuration.txt
reg-tests/sample_fetches/srv_name.vtc [new file with mode: 0644]
src/backend.c

index 029b37da8359ffe3ed8451dc4435010992cd9c4c..8dedbfc481fb453e7ed7d8cbe50a7a887603de9c 100644 (file)
@@ -14977,6 +14977,11 @@ srv_id : integer
   While it's almost only used with ACLs, it may be used for logging or
   debugging.
 
+srv_name : string
+  Returns a string containing the server's name when processing the response.
+  While it's almost only used with ACLs, it may be used for logging or
+  debugging.
+
 7.3.4. Fetching samples at Layer 5
 ----------------------------------
 
diff --git a/reg-tests/sample_fetches/srv_name.vtc b/reg-tests/sample_fetches/srv_name.vtc
new file mode 100644 (file)
index 0000000..d209c6e
--- /dev/null
@@ -0,0 +1,46 @@
+varnishtest "srv_name sample fetche Test"
+
+#REQUIRE_VERSION=2.1
+
+feature ignore_unknown_macro
+
+server s1 {
+    rxreq
+    txresp
+} -start
+
+server s2 {
+    rxreq
+    txresp
+} -start
+
+haproxy h1 -conf {
+    defaults
+        mode http
+        timeout connect 1s
+        timeout client  1s
+        timeout server  1s
+
+    frontend fe
+        bind "fd@${fe}"
+        http-response set-header srv-id   "%[srv_id]"
+        http-response set-header srv-name   "%[srv_name]"
+        default_backend be
+
+    backend be
+        server srv1 ${s1_addr}:${s1_port}
+        server srv2 ${s2_addr}:${s2_port}
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+    txreq -url "/"
+    rxresp
+    expect resp.status == 200
+    expect resp.http.srv-id == "1"
+    expect resp.http.srv-name == "srv1"
+    txreq -url "/"
+    rxresp
+    expect resp.status == 200
+    expect resp.http.srv-id == "2"
+    expect resp.http.srv-name == "srv2"
+} -run
index a7bcfc4a4323cd949e96c823bd46706389a43b03..049a1aee486ac74f1241aa612e807eca430be771 100644 (file)
@@ -2100,6 +2100,26 @@ smp_fetch_srv_id(const struct arg *args, struct sample *smp, const char *kw, voi
        return 1;
 }
 
+/* set string to the name of the server */
+static int
+smp_fetch_srv_name(const struct arg *args, struct sample *smp, const char *kw, void *private)
+{
+       if (!smp->strm)
+               return 0;
+
+       if (!objt_server(smp->strm->target))
+               return 0;
+
+       smp->data.u.str.area = (char *)__objt_server(smp->strm->target)->id;
+       if (!smp->data.u.str.area)
+               return 0;
+
+       smp->data.type = SMP_T_STR;
+       smp->data.u.str.data = strlen(smp->data.u.str.area);
+
+       return 1;
+}
+
 /* set temp integer to the number of connections per second reaching the backend.
  * Accepts exactly 1 argument. Argument is a backend, other types will lead to
  * undefined behaviour.
@@ -2341,6 +2361,7 @@ static struct sample_fetch_kw_list smp_kws = {ILH, {
        { "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_name",      smp_fetch_srv_name,       0,           NULL, SMP_T_STR,  SMP_USE_SERVR, },
        { "srv_queue",     smp_fetch_srv_queue,      ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
        { "srv_sess_rate", smp_fetch_srv_sess_rate,  ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
        { /* END */ },