It's been quite some time since I've done actual math and I'm hoping you guys can help me out.

I'm consuming a rate-limited API service that provides up to $N$ calls within a a sliding window of $M$ seconds. Each API response provides the number of calls used in the current window, $x$.

Rather than simply tacking on sleep(M/N) to all calls I'd like to implement a function that calculates an sleep that increases exponentially as $x$ approaches $N$ such that the sum of the sleep values is $M$. This should allow low request rates to avoid needlessly long sleeps values, and also ensure that high request rates do not violate the rate limit.

My initial, naive attempt involved something along the lines of increasing a base $\frac{M}{N}$ sleep by a factor of 2 each time an $\frac{N-x}{N}$ threshold of 50%, 75%, 87.5%, etc was passed. But the math evidently didn't work out as I'm currently violating the ratelimit left, right, and center. I believe that part of the problem is that the typical $N$ and $M$ are 150 and 300 respectively, and there's not a lot of resolution for the sleep times to ramp up.

Thanks in advance.

edit: So I figured that my problem looks like:

$M = \int_0^N e^{\frac{x-A}{B}}\, dx$

Where $A$ and $B$ are arbitrary values used to tune how the curve behaves. I plugged this into integral-calculator.com which gave me:

$M = B\mathrm{e}^{\frac{N}{B}-\frac{A}{B}}-B\mathrm{e}^{-\frac{A}{B}}$

$M = Be^{\frac{-A}{B}}(e^{\frac{N}{B}}-1)$

and I seem to have been able to rearrange it with regard to $A$ like:

$A = \ln{{\frac{M}{B(e^{\frac{N}{B}}-1)}}^{-B}}$

But I'm currently stumped as to how to rearrange it with regard to $B$.

edit²: Apparently solving this for B has a non-trivial amount of work associated with it. So far it has stumped a recent CompSci grad from my office, and WolframAlpha won't let me have enough computation time without setting up a Pro account. :I

On the bright side it's arranged my previous solution for A much more nicely:

$A = B ln(-\frac{{B-Be^\frac{N}{B}}}{M})$

edit³: Starting a bounty since I'd really like to see this solved with regard to $B$ as being able to specify $A$ more directly controls the point where $e^{\frac{x-A}{B}} = 1$ and the rate limiting increases more aggressively.

Alternatively, if someone can provide a similar exponential function and solution that accomplishes the same goal I would be open to that as well.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.