diff options
Diffstat (limited to 'src/slskd/Common/Compute.cs')
| -rw-r--r-- | src/slskd/Common/Compute.cs | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/slskd/Common/Compute.cs b/src/slskd/Common/Compute.cs index dec0e661..ef1f1882 100644 --- a/src/slskd/Common/Compute.cs +++ b/src/slskd/Common/Compute.cs @@ -24,11 +24,17 @@ namespace slskd /// </summary> public static class Compute { - public static int ExponentialBackoffDelay(int iteration, int delayInSeconds, int backoffRate, int maxDelayInSeconds = int.MaxValue) + public static (int Delay, int Jitter) ExponentialBackoffDelay(int iteration, int maxDelayInMilliseconds = int.MaxValue) { - var computedDelay = Math.Pow(delayInSeconds * backoffRate, Math.Min(50, iteration)) - delayInSeconds; + iteration = Math.Min(100, iteration); + + var computedDelay = Math.Floor((Math.Pow(2, iteration) - 1) / 2) * 1000; var safeDelay = (int)Math.Min(computedDelay, int.MaxValue); - return Math.Min(safeDelay, maxDelayInSeconds); + var clampedDelay = Math.Min(safeDelay, maxDelayInMilliseconds); + + var jitter = new Random().Next(1000); + + return (clampedDelay, jitter); } } -} +}
\ No newline at end of file |
