Skip to content

Commit bbd1688

Browse files
authored
upstream: new property to limit the amount of active TCP connections (#7586)
By default, Fluent Bit tries to deliver data as faster as possible and create TCP connections on-demand and in keepalive mode for performance reasons. In high-scalable environments, the user might want to control how many connections are done in parallel by setting a limit. This patch implements a new configuration property called 'net.max_connections' that can be used in the output plugins sections, so Fluent Bit won't open more than net.max_connections if it has been set. If the limit is reached, the output plugins will issue a retry. Configuration example: [OUTPUT] name splunk match * net.max_worker_connections 10 Note that this feature works at upstream/plugin level, and the limit is applied for all the workers if they exists, e.g: if you have 50 workers and net.max_connections=10, only 10 connections will be allowed. --------- Signed-off-by: Eduardo Silva <[email protected]>
1 parent 41d3e17 commit bbd1688

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

include/fluent-bit/flb_network.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ struct flb_net_setup {
7676

7777
/* prioritize ipv4 results when trying to establish a connection*/
7878
int dns_prefer_ipv4;
79+
80+
/* maximum number of allowed active TCP connections */
81+
int max_worker_connections;
7982
};
8083

8184
/* Defines a host service and it properties */

src/flb_upstream.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ struct flb_config_map upstream_net[] = {
9595
FLB_CONFIG_MAP_INT, "net.keepalive_max_recycle", "2000",
9696
0, FLB_TRUE, offsetof(struct flb_net_setup, keepalive_max_recycle),
9797
"Set maximum number of times a keepalive connection can be used "
98-
"before it is retired."
98+
"before it is retried."
99+
},
100+
101+
{
102+
FLB_CONFIG_MAP_INT, "net.max_worker_connections", "0",
103+
0, FLB_TRUE, offsetof(struct flb_net_setup, max_worker_connections),
104+
"Set the maximum number of active TCP connections that can be used per worker thread."
99105
},
100106

101107
/* EOF */
@@ -606,6 +612,7 @@ int flb_upstream_conn_recycle(struct flb_connection *conn, int val)
606612
struct flb_connection *flb_upstream_conn_get(struct flb_upstream *u)
607613
{
608614
int err;
615+
int total_connections = 0;
609616
struct mk_list *tmp;
610617
struct mk_list *head;
611618
struct flb_connection *conn;
@@ -617,12 +624,31 @@ struct flb_connection *flb_upstream_conn_get(struct flb_upstream *u)
617624
"net.connect_timeout = %i seconds\n"
618625
"net.source_address = %s\n"
619626
"net.keepalive = %s\n"
620-
"net.keepalive_idle_timeout = %i seconds",
627+
"net.keepalive_idle_timeout = %i seconds\n"
628+
"net.max_worker_connections = %i",
621629
u->tcp_host, u->tcp_port,
622630
u->base.net.connect_timeout,
623631
u->base.net.source_address ? u->base.net.source_address: "any",
624632
u->base.net.keepalive ? "enabled": "disabled",
625-
u->base.net.keepalive_idle_timeout);
633+
u->base.net.keepalive_idle_timeout,
634+
u->base.net.max_worker_connections);
635+
636+
637+
/* If the upstream is limited by max connections, check current state */
638+
if (u->base.net.max_worker_connections > 0) {
639+
flb_stream_acquire_lock(&u->base, FLB_TRUE);
640+
641+
total_connections = mk_list_size(&uq->av_queue);
642+
total_connections += mk_list_size(&uq->busy_queue);
643+
644+
flb_stream_release_lock(&u->base);
645+
646+
if (total_connections >= u->base.net.max_worker_connections) {
647+
flb_debug("[upstream] max worker connections=%i reached to: %s:%i, cannot connect",
648+
u->base.net.max_worker_connections, u->tcp_host, u->tcp_port);
649+
return NULL;
650+
}
651+
}
626652

627653
conn = NULL;
628654

0 commit comments

Comments
 (0)