From: Frederic Lecaille Date: Mon, 24 Aug 2026 10:14:04 +0000 (+0200) Subject: CLEANUP: haload: factor out the per-thread rate share computation X-Git-Tag: v3.5-dev6~81 X-Git-Url: https://git.kaiwu.me/?a=commitdiff_plain;h=b6b8ba303ac1e28b708cb526a00b68723b83bf9b;p=haproxy.git CLEANUP: haload: factor out the per-thread rate share computation hld_usr_schedule(), hld_usr_task(), and hld_rate_task() each had the same three lines to compute this thread's exact share of a rate (throttle-aware, or not). Move this into hld_thr_rate_share() and call it from all three places instead. Also fix a typo in hld_usr_schedule()'s comment ( -> ). --- diff --git a/src/haload.c b/src/haload.c index 4d0850eda..a30a260dd 100644 --- a/src/haload.c +++ b/src/haload.c @@ -1391,7 +1391,18 @@ static int hld_be_reuse_conn(struct connection **conn, int64_t *hash, return ret; } -/* Schedule user, depending on conn/req rate value */ +/* This thread's exact share of , split evenly across arg_thrd + * threads. Thread gets (rate + tid) / arg_thrd. The shares + * always add up to exactly , with nothing extra from rounding. + */ +static inline uint32_t hld_thr_rate_share(int rate) +{ + if (throttle) + return (mul32hi(rate, throttle) + tid) / arg_thrd; + return (rate + tid) / arg_thrd; +} + +/* Schedule user, depending on conn/req rate value */ static inline void hld_usr_schedule(struct hld_usr *usr, int rate) { uint32_t max, wait; @@ -1412,10 +1423,8 @@ static inline void hld_usr_schedule(struct hld_usr *usr, int rate) if (ti->curusrs < maxusrs && throttle) max = 0; - else if (throttle) - max = (mul32hi(rate, throttle) + tid) / arg_thrd; else - max = (rate + tid) / arg_thrd; + max = hld_thr_rate_share(rate); if (!max) { /* this thread has no budget right now (its exact share of @@ -2231,10 +2240,7 @@ static struct task *hld_rate_task(struct task *t, void *context, unsigned int st budget = nb_usr; } else { - if (throttle) - max = (mul32hi(HA_ATOMIC_LOAD(&arg_rate), throttle) + tid) / arg_thrd; - else - max = (HA_ATOMIC_LOAD(&arg_rate) + tid) / arg_thrd; + max = hld_thr_rate_share(HA_ATOMIC_LOAD(&arg_rate)); /* max may be 0 here: this thread's exact share of the rate is * 0 right now (either a real rate cap, or the initial