Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wait parameter to rate limits #131

Merged
merged 17 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/common.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

Common
======
Common reference
----------------

.. autoclass:: Dispatcher
:members:
Expand All @@ -17,3 +19,8 @@ Common
:type: UndefinedType

A alias for :attr:`UndefinedType.UNDEFINED`

Errors
------
.. automodule:: nextcore.common.errors
:members:
33 changes: 33 additions & 0 deletions nextcore/common/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# The MIT License (MIT)
# Copyright (c) 2021-present nextcore developers
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Final

__all__: Final[tuple[str, ...]] = ()


class RateLimitedError(Exception):
"""A error for when a :class:`~nextcore.common.TimesPer` is rate limited and ``wait`` was :data:`False`"""
15 changes: 14 additions & 1 deletion nextcore/common/times_per/times_per.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from queue import PriorityQueue
from typing import TYPE_CHECKING, AsyncIterator

from ..errors import RateLimitedError
from .priority_queue_container import PriorityQueueContainer

if TYPE_CHECKING:
Expand Down Expand Up @@ -59,14 +60,22 @@ def __init__(self, limit: int, per: float) -> None:
self._pending_reset: bool = False

@asynccontextmanager
async def acquire(self, *, priority: int = 0) -> AsyncIterator[None]:
async def acquire(self, *, priority: int = 0, wait: bool = True) -> AsyncIterator[None]:
"""Use a spot in the rate-limit.

Parameters
----------
priority:
The priority. **Lower** number means it will be requested earlier.
wait:
Wait for a spot in the rate limit.

If this is :data:`False`, this will raise :exc:`RateLimitedError` instead.

Raises
------
RateLimitedError
``wait`` was set to :data:`False` and there was no more spots in the rate limit.
Returns
-------
:class:`typing.AsyncContextManager`
Expand All @@ -78,6 +87,10 @@ async def acquire(self, *, priority: int = 0) -> AsyncIterator[None]:
logger.debug("Reserved requests: %s", self._in_progress)

if calculated_remaining == 0:
if not wait:
raise RateLimitedError()

# Wait for a spot
future: Future[None] = Future()
item = PriorityQueueContainer(priority, future)

Expand Down
Loading