-
-
Notifications
You must be signed in to change notification settings - Fork 28
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
0bc1ca2
commit f8dbdbc
Showing
9 changed files
with
138 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .echo_client import EchoClient | ||
from .echo_client import EchoClient | ||
from .token_bucket import TokenBucket |
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,33 @@ | ||
from threading import Timer | ||
|
||
class RepeatedTimer(object): | ||
|
||
def __init__(self, interval, function): | ||
|
||
self._timer = None | ||
self.interval = interval | ||
self.function = function | ||
self.is_running = False | ||
self.stopped = False | ||
self.start() | ||
|
||
def _run(self): | ||
|
||
if self.stopped: | ||
return | ||
self.is_running = False | ||
self.start() | ||
self.function() | ||
|
||
def start(self): | ||
|
||
if not self.is_running: | ||
self._timer = Timer(self.interval, self._run) | ||
self._timer.start() | ||
self.is_running = True | ||
|
||
def stop(self): | ||
|
||
self._timer.cancel() | ||
self.is_running = False | ||
self.stopped = True |
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,31 @@ | ||
from .repeated_timer import RepeatedTimer | ||
|
||
|
||
class TokenBucket: | ||
|
||
def __init__(self, current: int, maximum: int, refresh_rate, refresh_amount) -> None: | ||
|
||
self.current = current | ||
self.max = maximum | ||
self.refresh_rate = refresh_rate | ||
self.refresh_amount = refresh_amount | ||
|
||
self.refiller = RepeatedTimer(self.refresh_rate, self.refresh) | ||
|
||
def can_consume(self, amount: int = 1) -> bool: | ||
|
||
if (self.current - amount) >= 0: | ||
return True | ||
|
||
return False | ||
|
||
def consume(self, amount: int = 1) -> None: | ||
|
||
self.current -= amount | ||
|
||
def refresh(self) -> None: | ||
|
||
if (self.max - self.current) > self.refresh_amount: | ||
self.current += self.refresh_amount | ||
else: | ||
self.current = self.max |
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 +1 @@ | ||
from .exceptions import ClientError, ImageError, ServerNotResponsiveError, ClientNotConnectedError, PrefixNotDefinedError, CommandsNotEnabledError | ||
from .exceptions import ClientError, ImageError, ServerNotResponsiveError, ClientNotConnectedError, PrefixNotDefinedError, CommandsNotEnabledError, RustSocketDestroyedError, RateLimitError |
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