Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Dec 22, 2023
1 parent ba73243 commit b470a4d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/HABApp/util/rate_limiter/limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ def __repr__(self):
return f'<{self.__class__.__name__} {self._name:s}>'

def add_limit(self, allowed: int, interval: int, *,
hits: int = 0,
initial_hits: int = 0,
algorithm: LIMITER_ALGORITHM_HINT = 'leaky_bucket') -> 'Limiter':
"""Add a new rate limit
:param allowed: How many hits are allowed
:param interval: Interval in seconds
:param hits: How many hits the limit already has when it gets initially created
:param initial_hits: How many hits the limit already has when it gets initially created
:param algorithm: Which algorithm should this limit use
"""
_check_arg('allowed', allowed)
_check_arg('interval', interval)
_check_arg('hits', hits, allow_0=True)
if not hits <= allowed:
msg = f'Parameter hits must be <= parameter allowed! {hits:d} <= {allowed:d}!'
_check_arg('hits', initial_hits, allow_0=True)
if not initial_hits <= allowed:
msg = f'Parameter hits must be <= parameter allowed! {initial_hits:d} <= {allowed:d}!'
raise ValueError(msg)

if algorithm == get_args(_LITERAL_LEAKY_BUCKET)[0]:
Expand All @@ -63,22 +63,22 @@ def add_limit(self, allowed: int, interval: int, *,
if isinstance(window, cls) and window.allowed == allowed and window.interval == interval:
return self

limit = cls(allowed, interval, hits=hits)
limit = cls(allowed, interval, hits=initial_hits)
self._limits = tuple(sorted([*self._limits, limit], key=lambda x: x.interval))
return self

def parse_limits(self, *text: str,
hits: int = 0,
initial_hits: int = 0,
algorithm: LIMITER_ALGORITHM_HINT = 'leaky_bucket') -> 'Limiter':
"""Add one or more limits in textual form, e.g. ``5 in 60s``, ``10 per hour`` or ``10/15 mins``.
If the limit does already exist it will not be added again.
:param text: textual description of limit
:param hits: How many hits the limit already has when it gets initially created
:param initial_hits: How many hits the limit already has when it gets initially created
:param algorithm: Which algorithm should these limits use
"""
for limit in [parse_limit(t) for t in text]:
self.add_limit(*limit, hits=hits, algorithm=algorithm)
self.add_limit(*limit, initial_hits=initial_hits, algorithm=algorithm)
return self

def allow(self) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_utils/test_rate_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ def test_limiter_add(time):
assert str(e.value) == "Parameter interval must be an int > 0, is 0.5 (<class 'float'>)"

with pytest.raises(ValueError) as e:
limiter.add_limit(3, 5, hits=-1)
limiter.add_limit(3, 5, initial_hits=-1)
assert str(e.value) == "Parameter hits must be an int >= 0, is -1 (<class 'int'>)"

with pytest.raises(ValueError) as e:
limiter.add_limit(3, 5, hits=5)
limiter.add_limit(3, 5, initial_hits=5)
assert str(e.value) == "Parameter hits must be <= parameter allowed! 5 <= 3!"


Expand Down

0 comments on commit b470a4d

Please sign in to comment.