Hi there,
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 | 5 +++
reg-tests/sample_fetches/srv_name.vtc | 46 +++++++++++++++++++++++++++
src/backend.c | 21 ++++++++++++
3 files changed, 72 insertions(+)
create mode 100644 reg-tests/sample_fetches/srv_name.vtc
diff --git a/doc/configuration.txt b/doc/configuration.txt
index 029b37da8..8dedbfc48 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -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
index 000000000..d209c6e0d
--- /dev/null
+++ b/reg-tests/sample_fetches/srv_name.vtc
@@ -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
diff --git a/src/backend.c b/src/backend.c
index a7bcfc4a4..b5b83bd5a 100644
--- a/src/backend.c
+++ b/src/backend.c
@@ -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.
@@ -2340,6 +2360,7 @@ static struct sample_fetch_kw_list smp_kws = {ILH, {
{ "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_name", smp_fetch_srv_name, 0, NULL,
SMP_T_STR, 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, },
{ "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL,
SMP_T_SINT, SMP_USE_INTRN, },
--
2.23.0
From 76694a4548c1760579358fa9305cfa602198bf98 Mon Sep 17 00:00:00 2001
From: vkill <[email protected]>
Date: Wed, 30 Oct 2019 16:58:14 +0800
Subject: [PATCH] MINOR: backend: Add srv_name sample fetche
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 | 5 +++
reg-tests/sample_fetches/srv_name.vtc | 46 +++++++++++++++++++++++++++
src/backend.c | 21 ++++++++++++
3 files changed, 72 insertions(+)
create mode 100644 reg-tests/sample_fetches/srv_name.vtc
diff --git a/doc/configuration.txt b/doc/configuration.txt
index 029b37da8..8dedbfc48 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -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
index 000000000..d209c6e0d
--- /dev/null
+++ b/reg-tests/sample_fetches/srv_name.vtc
@@ -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
diff --git a/src/backend.c b/src/backend.c
index a7bcfc4a4..b5b83bd5a 100644
--- a/src/backend.c
+++ b/src/backend.c
@@ -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.
@@ -2340,6 +2360,7 @@ static struct sample_fetch_kw_list smp_kws = {ILH, {
{ "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_name", smp_fetch_srv_name, 0, NULL, SMP_T_STR, 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, },
{ "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
--
2.23.0