How to implement exponential backoff / latency calculation with fixed timeout and number of retries?
Most of the delay / delay algorithms that I have seen have a fixed number of tries or a fixed timeout, but not both.
I want to make exactly M attempts within T seconds with an exponential interval in between, so that "T = delay (0) + delay (1) + ... + delay (M-1)" where "delay (N) = (e ^ N - 1) / e "(where N is the number of repetitions).
How do you calculate the value of "e" in the above description so that exactly M attempts will be made during the entire timeout T (with the predefined M and T)?
+3
source to share
1 answer
Since "T" is a monotonic function of "e", you can do a binary search to find the best value that fits.
Here's an example Python program for finding such an "e" given by "T" and "M":
def total_time(e, M):
current = 1
total = 0
for i in range(M):
total += current-1
current *= e
return total
def find_best_e(T, M):
a, b = 0, T
while abs(a-b) > 1e-6:
m = (a+b)/2.0
if total_time(m, M) > T:
b = m
else:
a = m
return (a+b)/2
e = find_best_e(10, 3)
print([e**n-1 for n in range(3)])
+2
source to share