Measure rate limits from start times
The RateLimiter utility used by several drivers measures time from the exit of one context manager to the beginning of the next. This means that if the API call itself takes substantial time, it will be added to the expected delay. In other words, if the expected rate is one API call every two seconds, and the API call itself takes one second, the actual rate will be one call every three seconds. To more closely approximate the overall expected rate, measure from start to start, so that the duration of the API call itself is included. Change-Id: Ia62a6bfa6a3e6cac65f0c20179edcfbc94a5dcc5
This commit is contained in:
parent
a2e5e640ad
commit
a62060a14e
|
@ -445,16 +445,18 @@ class RateLimiter:
|
|||
if self.last_ts is None:
|
||||
return total_delay
|
||||
while True:
|
||||
delta = time.monotonic() - self.last_ts
|
||||
now = time.monotonic()
|
||||
delta = now - self.last_ts
|
||||
if delta >= self.delta:
|
||||
break
|
||||
delay = self.delta - delta
|
||||
time.sleep(delay)
|
||||
total_delay += delay
|
||||
self.last_ts = time.monotonic()
|
||||
return total_delay
|
||||
|
||||
def __exit__(self, etype, value, tb):
|
||||
self._exit(etype, value, tb)
|
||||
|
||||
def _exit(self, etype, value, tb):
|
||||
self.last_ts = time.monotonic()
|
||||
pass
|
||||
|
|
Loading…
Reference in New Issue