Skip to content

Commit

Permalink
Add additional_rate_limit argument
Browse files Browse the repository at this point in the history
Resolves #73
  • Loading branch information
RazerM committed Nov 24, 2023
1 parent 9eccab9 commit b065356
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Change Log
Unreleased_
-----------

Added
~~~~~

- ``additional_rate_limit`` argument to
:class:`~spacetrack.base.SpaceTrackClient`.

Fixed
~~~~~

Expand Down
2 changes: 2 additions & 0 deletions src/spacetrack/aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def __init__(
rush_store=None,
rush_key_prefix="",
httpx_client=None,
additional_rate_limit=None,
):
if httpx_client is None:
httpx_client = httpx.AsyncClient()
Expand All @@ -49,6 +50,7 @@ def __init__(
rush_store=rush_store,
rush_key_prefix=rush_key_prefix,
httpx_client=httpx_client,
additional_rate_limit=additional_rate_limit,
)

async def _handle_event(self, event):
Expand Down
16 changes: 16 additions & 0 deletions src/spacetrack/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ class SpaceTrackClient:
``SpaceTrackClient`` takes ownership of the httpx client. You should
only provide your own client if you need to configure it first (e.g.
for a proxy).
additional_rate_limit: Optionally, a :class:`rush.quota.Quota` if you
want to restrict the rate limit further than the defaults.
For more information, refer to the `Space-Track documentation`_.
Expand Down Expand Up @@ -268,6 +270,7 @@ def __init__(
rush_store=None,
rush_key_prefix="",
httpx_client=None,
additional_rate_limit=None,
):
if httpx_client is None:
httpx_client = httpx.Client()
Expand Down Expand Up @@ -303,6 +306,15 @@ def __init__(
)
self._per_minute_key = rush_key_prefix + "st_req_min"
self._per_hour_key = rush_key_prefix + "st_req_hr"
self._additional_key = rush_key_prefix + "st_req_custom"
if isinstance(additional_rate_limit, Quota):
self._additional_throttle = Throttle(
limiter=limiter, rate=additional_rate_limit
)
elif additional_rate_limit is None:
self._additional_throttle = None
else:
raise TypeError("additional_rate_limit must be a rush.quota.Quota")

@property
def base_url(self):
Expand Down Expand Up @@ -600,6 +612,10 @@ def _ratelimited_send_generator(self, request, *, stream=False):
if hour_limit.limited:
sleep_time = max(sleep_time, hour_limit.retry_after.total_seconds())

if self._additional_throttle is not None:
additional_limit = self._additional_throttle.check(self._additional_key, 1)
sleep_time = max(sleep_time, additional_limit.retry_after.total_seconds())

if sleep_time > 0:
yield RateLimitWait(sleep_time)

Expand Down

0 comments on commit b065356

Please sign in to comment.