forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrate_limiter.py
More file actions
70 lines (62 loc) · 2.31 KB
/
rate_limiter.py
File metadata and controls
70 lines (62 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import math
import threading
import time
from typing import Optional
class TokenBucketRateLimiter:
def __init__(self, rate: float, interval: float = 1.0, percent_usage: float = 0.6):
"""
Args:
rate: Maximum tokens added per interval (writes per interval)
interval: Refill interval in seconds
percent_usage: Fraction of available tokens allowed for writing
"""
self.rate = float(rate)
self.interval = float(interval)
self.max_tokens = float(rate)
self.tokens = float(rate)
self.last_refill = time.monotonic()
self.lock = threading.Lock()
self.cond = threading.Condition(self.lock)
self.percent_usage = float(percent_usage)
def _refill(self):
"""Refill tokens based on elapsed time."""
now = time.monotonic()
elapsed = now - self.last_refill
if elapsed <= 0:
return
added = (self.rate * elapsed) / self.interval
if added > 0:
self.tokens = min(self.max_tokens, self.tokens + added)
self.last_refill = now
def get_available_tokens(self) -> int:
"""
Return the current number of tokens available for use,
considering percent_usage.
"""
with self.lock:
self._refill()
return math.floor(self.tokens * self.percent_usage)
def wait_for_tokens(self, num: int, timeout: Optional[float] = None) -> bool:
"""
Block until `num` tokens are available, then consume them.
"""
if num <= 0:
return True
end_time = None if timeout is None else (time.monotonic() + timeout)
with self.cond:
while True:
self._refill()
available = self.tokens * self.percent_usage
if available >= num:
# Consume atomically
self.tokens -= num
self.cond.notify_all()
return True
if end_time is not None:
remaining = end_time - time.monotonic()
if remaining <= 0:
return False
wait_time = min(0.05, remaining)
else:
wait_time = 0.05
self.cond.wait(wait_time)