mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-09-21 22:01:31 +02:00
[MEDIUM] session: add concurrent connections counter
The new "conn_cur" session counter has been added. It is automatically updated upon "track XXX" directives, and the entry is touched at the moment we increment the value so that we don't consider further counter updates as real updates, otherwise we would end up updating upon completion, which may not be desired. Probably that some other event counters (eg: HTTP requests) will have to be updated upon each event though. This counter can be matched against current session's source address using the "src_conn_cur" ACL.
This commit is contained in:
parent
8b22a71a4d
commit
38285c18f4
@ -52,6 +52,11 @@ int parse_track_counters(char **args, int *arg,
|
|||||||
*/
|
*/
|
||||||
static inline void session_store_counters(struct session *s)
|
static inline void session_store_counters(struct session *s)
|
||||||
{
|
{
|
||||||
|
if (s->tracked_counters) {
|
||||||
|
void *ptr = stktable_data_ptr(s->tracked_table, s->tracked_counters, STKTABLE_DT_CONN_CUR);
|
||||||
|
if (ptr)
|
||||||
|
stktable_data_cast(ptr, conn_cur)--;
|
||||||
|
}
|
||||||
s->tracked_counters->ref_cnt--;
|
s->tracked_counters->ref_cnt--;
|
||||||
s->tracked_counters = NULL;
|
s->tracked_counters = NULL;
|
||||||
}
|
}
|
||||||
@ -65,6 +70,13 @@ static inline void session_track_counters(struct session *s, struct stktable *t,
|
|||||||
ts->ref_cnt++;
|
ts->ref_cnt++;
|
||||||
s->tracked_table = t;
|
s->tracked_table = t;
|
||||||
s->tracked_counters = ts;
|
s->tracked_counters = ts;
|
||||||
|
if (ts) {
|
||||||
|
void *ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
|
||||||
|
if (ptr)
|
||||||
|
stktable_data_cast(ptr, conn_cur)++;
|
||||||
|
if (tick_isset(t->expire))
|
||||||
|
ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void inline trace_term(struct session *s, unsigned int code)
|
static void inline trace_term(struct session *s, unsigned int code)
|
||||||
|
@ -43,13 +43,15 @@ enum {
|
|||||||
enum {
|
enum {
|
||||||
STKTABLE_DT_SERVER_ID, /* the server ID to use with this session if > 0 */
|
STKTABLE_DT_SERVER_ID, /* the server ID to use with this session if > 0 */
|
||||||
STKTABLE_DT_CONN_CNT, /* cumulated number of connections */
|
STKTABLE_DT_CONN_CNT, /* cumulated number of connections */
|
||||||
|
STKTABLE_DT_CONN_CUR, /* concurrent number of connections */
|
||||||
STKTABLE_DATA_TYPES /* Number of data types, must always be last */
|
STKTABLE_DATA_TYPES /* Number of data types, must always be last */
|
||||||
};
|
};
|
||||||
|
|
||||||
/* stick_table extra data. This is mainly used for casting or size computation */
|
/* stick_table extra data. This is mainly used for casting or size computation */
|
||||||
union stktable_data {
|
union stktable_data {
|
||||||
unsigned int conn_cnt;
|
|
||||||
int server_id;
|
int server_id;
|
||||||
|
unsigned int conn_cnt;
|
||||||
|
unsigned int conn_cur;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* known data types */
|
/* known data types */
|
||||||
|
@ -2127,11 +2127,45 @@ acl_fetch_src_updt_conn_cnt(struct proxy *px, struct session *l4, void *l7, int
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* set test->i to the number of concurrent connections from the session's source
|
||||||
|
* address in the table pointed to by expr.
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
acl_fetch_src_conn_cur(struct proxy *px, struct session *l4, void *l7, int dir,
|
||||||
|
struct acl_expr *expr, struct acl_test *test)
|
||||||
|
{
|
||||||
|
struct stksess *ts;
|
||||||
|
struct stktable_key *key;
|
||||||
|
|
||||||
|
key = tcpv4_src_to_stktable_key(l4);
|
||||||
|
if (!key)
|
||||||
|
return 0; /* only TCPv4 is supported right now */
|
||||||
|
|
||||||
|
if (expr->arg_len)
|
||||||
|
px = find_stktable(expr->arg.str);
|
||||||
|
|
||||||
|
if (!px)
|
||||||
|
return 0; /* table not found */
|
||||||
|
|
||||||
|
test->flags = ACL_TEST_F_VOL_TEST;
|
||||||
|
test->i = 0;
|
||||||
|
|
||||||
|
if ((ts = stktable_lookup_key(&px->table, key)) != NULL) {
|
||||||
|
void *ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CUR);
|
||||||
|
if (!ptr)
|
||||||
|
return 0; /* parameter not stored */
|
||||||
|
test->i = stktable_data_cast(ptr, conn_cur);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 */
|
||||||
static struct acl_kw_list acl_kws = {{ },{
|
static struct acl_kw_list acl_kws = {{ },{
|
||||||
{ "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
|
{ "src_conn_cnt", acl_parse_int, acl_fetch_src_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
|
||||||
{ "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
|
{ "src_updt_conn_cnt", acl_parse_int, acl_fetch_src_updt_conn_cnt, acl_match_int, ACL_USE_TCP4_VOLATILE },
|
||||||
|
{ "src_conn_cur", acl_parse_int, acl_fetch_src_conn_cur, acl_match_int, ACL_USE_TCP4_VOLATILE },
|
||||||
{ NULL, NULL, NULL, NULL },
|
{ NULL, NULL, NULL, NULL },
|
||||||
}};
|
}};
|
||||||
|
|
||||||
|
@ -525,8 +525,9 @@ int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_t
|
|||||||
|
|
||||||
/* Extra data types processing */
|
/* Extra data types processing */
|
||||||
struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
|
struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
|
||||||
[STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .data_length = stktable_data_size(conn_cnt) },
|
|
||||||
[STKTABLE_DT_SERVER_ID] = { .name = "server_id", .data_length = stktable_data_size(server_id) },
|
[STKTABLE_DT_SERVER_ID] = { .name = "server_id", .data_length = stktable_data_size(server_id) },
|
||||||
|
[STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .data_length = stktable_data_size(conn_cnt) },
|
||||||
|
[STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .data_length = stktable_data_size(conn_cur) },
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user