From 34e4085f8a1e1b18f72ceeb2fee6c844965b88d7 Mon Sep 17 00:00:00 2001 From: Maciej Zdeb Date: Mon, 16 May 2022 17:27:28 +0200 Subject: [PATCH] MEDIUM: peers: Balance applets across threads When creating a new applet for peer outgoing connection, we check the load on each thread. Threads with least applet count are preferred. With this solution we avoid a situation when many outgoing connections run on the same thread causing significant load on single CPU core. --- src/peers.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/peers.c b/src/peers.c index d46afcfba..6f71ded9f 100644 --- a/src/peers.c +++ b/src/peers.c @@ -3196,7 +3196,8 @@ void peers_setup_frontend(struct proxy *fe) static struct appctx *peer_session_create(struct peers *peers, struct peer *peer) { struct appctx *appctx; - unsigned int thr = tid; + unsigned int thr = 0; + int idx; peer->new_conn++; peer->reconnect = tick_add(now_ms, MS_TO_TICKS(PEER_RECONNECT_TIMEOUT)); @@ -3204,7 +3205,9 @@ static struct appctx *peer_session_create(struct peers *peers, struct peer *peer peer->statuscode = PEER_SESS_SC_CONNECTCODE; peer->last_hdshk = now_ms; - appctx = appctx_new_here(&peer_applet, NULL); + for (idx = 0; idx < global.nbthread; idx++) + thr = peers->applet_count[idx] < peers->applet_count[thr] ? idx : thr; + appctx = appctx_new_on(&peer_applet, NULL, thr); if (!appctx) goto out_close; appctx->svcctx = (void *)peer; @@ -3212,10 +3215,8 @@ static struct appctx *peer_session_create(struct peers *peers, struct peer *peer appctx->st0 = PEER_SESS_ST_CONNECT; peer->appctx = appctx; - if (appctx_init(appctx) == -1) - goto out_free_appctx; - HA_ATOMIC_INC(&peers->applet_count[thr]); + appctx_wakeup(appctx); return appctx; /* Error unrolling */