-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
223d7b4
commit 710a5ae
Showing
10 changed files
with
440 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .base import BaseRateLimit | ||
from .fixed_window import FixedWindowElasticExpiryLimit, FixedWindowElasticExpiryLimitInfo | ||
from .leaky_bucket import LeakyBucketLimit, LeakyBucketLimitInfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from dataclasses import dataclass | ||
from typing import Final | ||
|
||
|
||
@dataclass | ||
class BaseRateLimitInfo: | ||
hits: int #: Hits | ||
skips: int #: Skips | ||
limit: int #: Boundary | ||
|
||
@property | ||
def hits_remaining(self) -> int: | ||
return self.limit - self.hits | ||
|
||
|
||
class BaseRateLimit: | ||
def __init__(self, allowed: int, interval: int): | ||
super().__init__() | ||
assert allowed > 0, allowed | ||
assert interval > 0, interval | ||
|
||
self.interval: Final = interval | ||
self.allowed: Final = allowed | ||
|
||
self.hits: int = 0 | ||
self.skips: int = 0 | ||
|
||
def repr_text(self) -> str: | ||
return '' | ||
|
||
def __repr__(self): | ||
return ( | ||
f'<{self.__class__.__name__} hits={self.hits:d}/{self.allowed:d} interval={self.interval:d}s ' | ||
f'{self.repr_text():s}>' | ||
) | ||
|
||
def do_test_allow(self): | ||
raise NotImplementedError() | ||
|
||
def do_allow(self): | ||
raise NotImplementedError() | ||
|
||
def do_deny(self): | ||
raise NotImplementedError() | ||
|
||
def info(self) -> BaseRateLimitInfo: | ||
raise NotImplementedError() | ||
|
||
def allow(self, weight: int = 1) -> bool: | ||
if not isinstance(weight, int) or weight <= 0: | ||
msg = f'weight must be an int > 0, is {weight}' | ||
raise ValueError(msg) | ||
|
||
self.do_allow() | ||
|
||
self.hits += weight | ||
if self.hits <= self.allowed: | ||
return True | ||
|
||
self.skips += 1 | ||
self.hits = self.allowed | ||
|
||
if self.do_deny: | ||
self.do_deny() | ||
return False | ||
|
||
def test_allow(self, weight: int = 1) -> bool: | ||
if not isinstance(weight, int) or weight <= 0: | ||
msg = f'weight must be an int > 0, is {weight}' | ||
raise ValueError(msg) | ||
|
||
self.do_test_allow() | ||
return self.hits + weight <= self.allowed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from dataclasses import dataclass | ||
from time import monotonic | ||
|
||
from .base import BaseRateLimit, BaseRateLimitInfo | ||
|
||
|
||
@dataclass | ||
class FixedWindowElasticExpiryLimitInfo(BaseRateLimitInfo): | ||
time_remaining: float #: Time remaining until this window will reset | ||
|
||
|
||
class FixedWindowElasticExpiryLimit(BaseRateLimit): | ||
def __init__(self, allowed: int, interval: int): | ||
super().__init__(allowed, interval) | ||
|
||
self.start: float = -1.0 | ||
self.stop: float = -1.0 | ||
|
||
def repr_text(self): | ||
return f'window={self.stop - self.start:.0f}s' | ||
|
||
def do_test_allow(self): | ||
if self.stop <= monotonic(): | ||
self.hits = 0 | ||
self.skips = 0 | ||
|
||
def do_allow(self): | ||
now = monotonic() | ||
|
||
if self.stop <= now: | ||
self.hits = 0 | ||
self.skips = 0 | ||
self.start = now | ||
self.stop = now + self.interval | ||
|
||
def do_deny(self): | ||
self.stop = monotonic() + self.interval | ||
|
||
def info(self) -> FixedWindowElasticExpiryLimitInfo: | ||
self.do_test_allow() | ||
|
||
remaining = self.stop - monotonic() | ||
if remaining <= 0: | ||
remaining = 0 | ||
|
||
if not remaining and not self.hits: | ||
remaining = self.interval | ||
|
||
return FixedWindowElasticExpiryLimitInfo( | ||
time_remaining=remaining, hits=self.hits, skips=self.skips, limit=self.allowed | ||
) |
Oops, something went wrong.