-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add dispose method * Update test & refactor bucket factory * Update Limiter & BucketFactory api * update readme * update readme * update * Update * update
- Loading branch information
1 parent
bcb8b5a
commit b309854
Showing
8 changed files
with
209 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pyrate-limiter" | ||
version = "3.6.2" | ||
version = "3.7.0" | ||
description = "Python Rate-Limiter using Leaky-Bucket Algorithm" | ||
authors = ["vutr <[email protected]>"] | ||
license = "MIT" | ||
|
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,3 +1,4 @@ | ||
from .bucket import * # noqa | ||
from .clock import * # noqa | ||
from .rate import * # noqa | ||
from .wrappers import * # noqa |
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,76 @@ | ||
""" Wrappers over different abstract types | ||
""" | ||
from inspect import isawaitable | ||
from typing import Optional | ||
|
||
from .bucket import AbstractBucket | ||
from .rate import RateItem | ||
|
||
|
||
class BucketAsyncWrapper(AbstractBucket): | ||
"""BucketAsyncWrapper is a wrapping over any bucket | ||
that turns a async/synchronous bucket into an async one | ||
""" | ||
|
||
def __init__(self, bucket: AbstractBucket): | ||
assert isinstance(bucket, AbstractBucket) | ||
self.bucket = bucket | ||
|
||
async def put(self, item: RateItem): | ||
result = self.bucket.put(item) | ||
|
||
while isawaitable(result): | ||
result = await result | ||
|
||
return result | ||
|
||
async def count(self): | ||
result = self.bucket.count() | ||
|
||
while isawaitable(result): | ||
result = await result | ||
|
||
return result | ||
|
||
async def leak(self, current_timestamp: Optional[int] = None) -> int: | ||
result = self.bucket.leak(current_timestamp) | ||
|
||
while isawaitable(result): | ||
result = await result | ||
|
||
assert isinstance(result, int) | ||
return result | ||
|
||
async def flush(self) -> None: | ||
result = self.bucket.flush() | ||
|
||
while isawaitable(result): | ||
result = await result | ||
|
||
return None | ||
|
||
async def peek(self, index: int) -> Optional[RateItem]: | ||
item = self.bucket.peek(index) | ||
|
||
while isawaitable(item): | ||
item = await item | ||
|
||
assert item is None or isinstance(item, RateItem) | ||
return item | ||
|
||
async def waiting(self, item: RateItem) -> int: | ||
wait = super().waiting(item) | ||
|
||
if isawaitable(wait): | ||
wait = await wait | ||
|
||
assert isinstance(wait, int) | ||
return wait | ||
|
||
@property | ||
def failing_rate(self): | ||
return self.bucket.failing_rate | ||
|
||
@property | ||
def rates(self): | ||
return self.bucket.rates |
Oops, something went wrong.