0

I'm using an API that rate-limits me, forcing me to wait and retry my request if I hit a URL too frequently. Let's say for the sake of argument that I don't know what the specific rate limit threshold is (and don't want to hardcode one into my app even if I did know).

I can make an API call and either watch it succeed, or get back a response saying I've been rate limited, then try again shortly, and I can record the total time elapsed for all tries before I was able to successfully complete an API call.

What algorithms would be a good fit for predicting the minimum time I need to wait before retrying an API call after hitting the rate limit?

spiffytech
  • 6,161
  • 7
  • 41
  • 57
  • Why exactly do you need machine learning to find a threshold? Just count how many times you hit the URL within a given timeframe. – Thomas Jungblut Jun 09 '13 at 19:13

1 Answers1

0

Use something like a one sided binary search.

See page 134 of The Algorithm Design Manual:

Now suppose we have an array A consisting of a run of 0's, followed by an unbounded run of 1's, and would like to identify the exact point of transition between them. Binary search on the array would provide the transition point in ⌈lg n ⌉ tests, if we had a bound n on the number of elements in the array. In the absence of such a bound, we can test repeatedly at larger intervals (A[1], A[2], A[4], A[8], A[16], ...) until we find a first non-zero value. Now we have a window containing the target and can proceed with binary search. This one-sided binary search finds the transition point p using at most 2 ⌈lg(p)⌉ comparisons, regardless of how large the array actally is. One-sided binary search is most useful whenever we are looking for a key that probably lies close to our current position.

Wesley Baugh
  • 3,720
  • 4
  • 24
  • 42