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
This commit is contained in:
vkill 2019-10-30 16:58:14 +08:00 committed by Willy Tarreau
parent 40f2f1e341
commit 1dfd16536f
3 changed files with 72 additions and 0 deletions

View File

@ -14977,6 +14977,11 @@ srv_id : integer
While it's almost only used with ACLs, it may be used for logging or While it's almost only used with ACLs, it may be used for logging or
debugging. 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 7.3.4. Fetching samples at Layer 5
---------------------------------- ----------------------------------

View File

@ -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

View File

@ -2100,6 +2100,26 @@ smp_fetch_srv_id(const struct arg *args, struct sample *smp, const char *kw, voi
return 1; 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. /* 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 * Accepts exactly 1 argument. Argument is a backend, other types will lead to
* undefined behaviour. * 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_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_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_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_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, }, { "srv_sess_rate", smp_fetch_srv_sess_rate, ARG1(1,SRV), NULL, SMP_T_SINT, SMP_USE_INTRN, },
{ /* END */ }, { /* END */ },