]> git.kaiwu.me - haproxy.git/commitdiff
CLEANUP: haload: factor out the per-thread rate share computation
authorFrederic Lecaille <flecaille@haproxy.com>
Mon, 24 Aug 2026 10:14:04 +0000 (12:14 +0200)
committerFrederic Lecaille <flecaille@haproxy.com>
Mon, 24 Aug 2026 17:16:05 +0000 (19:16 +0200)
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 (<urs> -> <usr>).

src/haload.c

index 4d0850edac4a69fcaf7a11b9c5c60ca772774f47..a30a260dd5857d52895aaf23e66af2fa7cde732c 100644 (file)
@@ -1391,7 +1391,18 @@ static int hld_be_reuse_conn(struct connection **conn, int64_t *hash,
        return ret;
 }
 
-/* Schedule <urs> user, depending on <rate> conn/req rate value */
+/* This thread's exact share of <rate>, split evenly across arg_thrd
+ * threads. Thread <tid> gets (rate + tid) / arg_thrd. The shares
+ * always add up to exactly <rate>, 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 <usr> user, depending on <rate> 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