-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
10 changed files
with
540 additions
and
556 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
--- | ||
major: 1 | ||
minor: 1 | ||
patch: 4 | ||
patch: 5 |
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
Empty file.
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,30 @@ | ||
""" | ||
Test the 'math.analysis.rate.limiter' module. | ||
""" | ||
|
||
# third-party | ||
from pytest import approx | ||
|
||
# module under test | ||
from vcorelib.math.analysis.rate.limiter import RateLimiter | ||
|
||
|
||
def test_rate_limiter_basic(): | ||
"""Test that the rate-limiter works correctly.""" | ||
|
||
second = int(1e9) | ||
|
||
lim = RateLimiter(second) | ||
|
||
curr = second | ||
lim(curr) | ||
|
||
for _ in range(100): | ||
curr += second | ||
assert lim(curr, lambda: None) | ||
|
||
assert not lim(curr) | ||
|
||
assert lim.rate_hz == approx(1.0) | ||
|
||
assert lim() |
File renamed without changes.
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
File renamed without changes.
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,58 @@ | ||
""" | ||
A module for simplifying limiting the rate of a function call or hot loop. | ||
""" | ||
|
||
# built-in | ||
from typing import Callable as _Callable | ||
|
||
# internal | ||
from vcorelib.math.analysis.rate import RateTracker | ||
from vcorelib.math.time import default_time_ns as _default_time_ns | ||
|
||
|
||
class RateLimiter: | ||
"""A class for limiting the rate of runtime work.""" | ||
|
||
def __init__(self, period_ns: int, **kwargs) -> None: | ||
"""Initialize this rate-limiter.""" | ||
|
||
assert period_ns >= 0 | ||
self.period_ns = period_ns | ||
self.prev_time_ns: int = 0 | ||
self.rate = RateTracker(**kwargs) | ||
|
||
@property | ||
def rate_hz(self) -> float: | ||
""" | ||
Get the underlying rate that this limiter is governing. This is useful | ||
to determine if the rate limitation is or isn't impacting the rate of | ||
some task. | ||
""" | ||
return self.rate.value | ||
|
||
def __call__( | ||
self, time_ns: int = None, task: _Callable[[], None] = None | ||
) -> bool: | ||
""" | ||
Query the limiter to determine if the current time would allow a | ||
governed task to run. | ||
""" | ||
|
||
result = False | ||
|
||
# Use a default time if one wasn't provided. | ||
if time_ns is None: | ||
time_ns = _default_time_ns() | ||
|
||
if time_ns >= self.prev_time_ns + self.period_ns: | ||
self.prev_time_ns = time_ns | ||
result = True | ||
|
||
# Call the task if provided. | ||
if task is not None: | ||
task() | ||
|
||
# Update rate tracking. | ||
self.rate(time_ns=time_ns) | ||
|
||
return result |