From: Amaury Denoyelle Date: Mon, 3 Nov 2025 08:49:06 +0000 (+0100) Subject: OPTIM: backend: skip conn reuse for incompatible proxies X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6bfabfdc77af5443bafdad901644119120993117;p=thirdparty%2Fhaproxy.git OPTIM: backend: skip conn reuse for incompatible proxies When trying to reuse a backend connection, a connection hash is calculated to match an entry with similar parameters. Previously, this operation was skipped if the stream content wasn't based on HTTP, as it would have been incompatible with http-reuse. With the introduction of SPOP backends, this condition was removed, so that it can also benefit from connection reuse. However, this means that now hash calcul is always performed when connecting to a server, even for TCP or log backends. This is unnecessary as these proxies cannot perform connection reuse. Note also that reuse mode is resetted on postparsing for incompatible backends. This at least guarantees that no tree lookup will be performed via be_reuse_connection(). However, connection lookup is still performed in the session via session_get_conn() which is another unnecessary operation. Thus, this patch restores the condition so that reuse operations are now entirely skipped if a backend mode is incompatible. This is implemented via a new utility function named be_supports_conn_reuse(). This could be backported up to 3.1, as this commit could be considered as a performance regression for tcp/log backend modes. --- diff --git a/include/haproxy/backend.h b/include/haproxy/backend.h index 3fae24280..41ad4de6e 100644 --- a/include/haproxy/backend.h +++ b/include/haproxy/backend.h @@ -167,6 +167,12 @@ void set_backend_down(struct proxy *be); unsigned int gen_hash(const struct proxy* px, const char* key, unsigned long len); +/* Returns true if connection reuse is supported by backend. */ +static inline int be_supports_conn_reuse(const struct proxy *be) +{ + return be->mode == PR_MODE_HTTP || be->mode == PR_MODE_SPOP; +} + #endif /* _HAPROXY_BACKEND_H */ /* diff --git a/src/backend.c b/src/backend.c index d0c1b8077..6412eb2b8 100644 --- a/src/backend.c +++ b/src/backend.c @@ -1664,6 +1664,10 @@ int64_t be_calculate_conn_hash(struct server *srv, struct stream *strm, * conducted on session. This allows to use a connection not yet * labelled as safe under http-reuse safe policy. * + * This function should only be called for backends which supports connection + * reuse. It may be necessary to extend be_supports_conn_reuse() when + * implementing a new proxy mode. + * * Returns SF_ERR_NONE if a connection has been reused. The connection instance * can be retrieve via stconn. SF_ERR_RESOURCE is returned if no matching * connection found. SF_ERR_INTERNAL is used on internal error. @@ -1814,6 +1818,9 @@ int connect_server(struct stream *s) if (err != SRV_STATUS_OK) return SF_ERR_INTERNAL; + if (!be_supports_conn_reuse(s->be)) + goto skip_reuse; + /* disable reuse if websocket stream and the protocol to use is not the * same as the main protocol of the server. */ @@ -1904,6 +1911,7 @@ int connect_server(struct stream *s) } } + skip_reuse: /* no reuse or failed to reuse the connection above, pick a new one */ if (!srv_conn) { unsigned int total_conns;