From: Amaury Denoyelle Date: Wed, 25 Oct 2023 13:32:28 +0000 (+0200) Subject: MINOR: frontend: implement a dedicated actconn increment function X-Git-Tag: v2.9-dev9~26 X-Git-Url: http://git.haproxy.org/?a=commitdiff_plain;h=fffd435bbd6f83f79bc5326d007365711ae0252d;p=haproxy-3.0.git MINOR: frontend: implement a dedicated actconn increment function When a new frontend connection is instantiated, actconn global counter is incremented. If global maxconn value is reached, the connection is cancelled. This ensures that system limit are under control. Prior to this patch, the atomic check/increment operations were done directly into listener_accept(). Move them in a dedicated function increment_actconn() in frontend module. This will be useful when QUIC connections will be counted in actconn counter. --- diff --git a/include/haproxy/frontend.h b/include/haproxy/frontend.h index eb986fb..8cd1a0a 100644 --- a/include/haproxy/frontend.h +++ b/include/haproxy/frontend.h @@ -26,6 +26,7 @@ int frontend_accept(struct stream *s); +int increment_actconn(); #endif /* _HAPROXY_FRONTEND_H */ diff --git a/src/frontend.c b/src/frontend.c index 105b24b..ad2e39e 100644 --- a/src/frontend.c +++ b/src/frontend.c @@ -165,6 +165,33 @@ int frontend_accept(struct stream *s) return -1; } +/* Increment current active connection counter. This ensures that global + * maxconn is not reached or exceeded. This must be done for every new frontend + * connection allocation. + * + * Returns the new actconn global value. If maxconn reached or exceeded, 0 is + * returned : the connection allocation should be cancelled. + */ +int increment_actconn() +{ + unsigned int count, next_actconn; + + do { + count = actconn; + if (unlikely(count >= global.maxconn)) { + /* maxconn reached */ + next_actconn = 0; + goto end; + } + + /* try to increment actconn */ + next_actconn = count + 1; + } while (!_HA_ATOMIC_CAS(&actconn, (int *)(&count), next_actconn) && __ha_cpu_relax()); + + end: + return next_actconn; +} + /************************************************************************/ /* All supported sample and ACL keywords must be declared here. */ /************************************************************************/ diff --git a/src/listener.c b/src/listener.c index 8a951f3..bbbf180 100644 --- a/src/listener.c +++ b/src/listener.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -1114,18 +1115,14 @@ void listener_accept(struct listener *l) } if (!(l->bind_conf->options & BC_O_UNLIMITED)) { - do { - count = actconn; - if (unlikely(count >= global.maxconn)) { - /* the process was marked full or another - * thread is going to do it. - */ - next_actconn = 0; - expire = tick_add(now_ms, 1000); /* try again in 1 second */ - goto limit_global; - } - next_actconn = count + 1; - } while (!_HA_ATOMIC_CAS(&actconn, (int *)(&count), next_actconn)); + next_actconn = increment_actconn(); + if (!next_actconn) { + /* the process was marked full or another + * thread is going to do it. + */ + expire = tick_add(now_ms, 1000); /* try again in 1 second */ + goto limit_global; + } } /* be careful below, the listener might be shutting down in