1
0
mirror of http://git.haproxy.org/git/haproxy.git/ synced 2025-04-07 01:31:35 +00:00

MINOR: Add nbsrv sample converter

This is like the nbsrv() sample fetch function except that it works as
a converter so it can count the number of available servers of a backend
name retrieved using a sample fetch or an environment variable.

Signed-off-by: Nenad Merdanovic <nmerdan@haproxy.com>
This commit is contained in:
Nenad Merdanovic 2017-03-12 21:56:55 +01:00 committed by Willy Tarreau
parent 96c1571943
commit b7e7c4720a
3 changed files with 44 additions and 0 deletions

View File

@ -12530,6 +12530,12 @@ mul(<value>)
This prefix is followed by a name. The separator is a '.'. The name may only This prefix is followed by a name. The separator is a '.'. The name may only
contain characters 'a-z', 'A-Z', '0-9', '.' and '_'. contain characters 'a-z', 'A-Z', '0-9', '.' and '_'.
nbsrv
Takes an input value of type string, interprets it as a backend name and
returns the number of usable servers in that backend. Can be used in places
where we want to look up a backend from a dynamic name, like a result of a
map lookup.
neg neg
Takes the input value of type signed integer, computes the opposite value, Takes the input value of type signed integer, computes the opposite value,
and returns the remainder as an signed integer. 0 is identity. This operator and returns the remainder as an signed integer. 0 is identity. This operator

View File

@ -46,6 +46,19 @@ struct server *get_server_sh(struct proxy *px, const char *addr, int len);
struct server *get_server_uh(struct proxy *px, char *uri, int uri_len); struct server *get_server_uh(struct proxy *px, char *uri, int uri_len);
int be_lastsession(const struct proxy *be); int be_lastsession(const struct proxy *be);
/* Returns number of usable servers in backend */
static inline int be_usable_srv(struct proxy *be)
{
if (be->state == PR_STSTOPPED)
return 0;
else if (be->srv_act)
return be->srv_act;
else if (be->lbprm.fbck)
return 1;
else
return be->srv_bck;
}
/* set the time of last session on the backend */ /* set the time of last session on the backend */
static void inline be_set_sess_last(struct proxy *be) static void inline be_set_sess_last(struct proxy *be)
{ {

View File

@ -1821,6 +1821,24 @@ smp_fetch_srv_sess_rate(const struct arg *args, struct sample *smp, const char *
return 1; return 1;
} }
static int sample_conv_nbsrv(const struct arg *args, struct sample *smp, void *private)
{
struct proxy *px;
if (!smp_make_safe(smp))
return 0;
px = proxy_find_by_name(smp->data.u.str.str, PR_CAP_BE, 0);
if (!px)
return 0;
smp->data.type = SMP_T_SINT;
smp->data.u.sint = be_usable_srv(px);
return 1;
}
/* Note: must not be declared <const> as its list will be overwritten. /* Note: must not be declared <const> as its list will be overwritten.
* Please take care of keeping this list alphabetically sorted. * Please take care of keeping this list alphabetically sorted.
@ -1841,6 +1859,12 @@ static struct sample_fetch_kw_list smp_kws = {ILH, {
{ /* END */ }, { /* END */ },
}}; }};
/* 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 },
{ /* END */ },
}};
/* Note: must not be declared <const> as its list will be overwritten. /* Note: must not be declared <const> as its list will be overwritten.
* Please take care of keeping this list alphabetically sorted. * Please take care of keeping this list alphabetically sorted.
@ -1854,6 +1878,7 @@ __attribute__((constructor))
static void __backend_init(void) static void __backend_init(void)
{ {
sample_register_fetches(&smp_kws); sample_register_fetches(&smp_kws);
sample_register_convs(&sample_conv_kws);
acl_register_keywords(&acl_kws); acl_register_keywords(&acl_kws);
} }