disco.api.ratelimit

Classes

RouteState

Inherits From LoggingClass

An object which stores ratelimit state for a given method/url route combination (as specified in :class:disco.api.http.Routes).

Parameters
Name Type Description
route tuple(HTTPMethod, str) The route which this RouteState is for.
response :class:`requests.Response` The response object for the last request made to the route, should contain the standard rate limit headers.
Attributes
Name Type Description
route tuple(HTTPMethod, str) The route which this RouteState is for.
remaining int The number of remaining requests to the route before the rate limit will be hit, triggering a 429 response.
reset_time int A unix epoch timestamp (in seconds) after which this rate limit is reset
event :class:`gevent.event.Event` An event that is used to block all requests while a route is in the cooldown stage.

Functions

init(self, route, response)

repr(self)

chilled(self)

Whether this route is currently being cooldown (aka waiting until reset_time).

next_will_ratelimit(self)

Whether the next request to the route (at this moment in time) will trigger the rate limit.

update(self, response)

Updates this route with a given Requests response object. Its expected the response has the required headers, however in the case it doesn't this function has no effect.

wait(self, timeout)

Waits until this route is no longer under a cooldown.

Returns
The duration we waited for, in seconds or zero if we didn't have to
wait at all.

cooldown(self)

Waits for the current route to be cooled-down (aka waiting until reset time).

RateLimiter

Inherits From LoggingClass

A in-memory store of ratelimit states for all routes we've ever called.

Attributes
Name Type Description
states dict(tuple(HTTPMethod, str), :class:`RouteState`) Contains a :class:`RouteState` for each route the RateLimiter is currently tracking.

Functions

init(self)

check(self, route)

Checks whether a given route can be called. This function will return immediately if no rate-limit cooldown is being imposed for the given route, or will wait indefinitely until the route is finished being cooled down. This function should be called before making a request to the specified route.

Returns
The number of seconds we had to wait for this rate limit, or zero
if no time was waited.
Parameters
Name Type Description
route tuple(HTTPMethod, str) The route that will be checked.

_check(self, route)

update(self, route, response)

Updates the given routes state with the rate-limit headers inside the response from a previous call to the route.

Parameters
Name Type Description
route tuple(HTTPMethod, str) The route that will be updated.
response :class:`requests.Response` The response object for the last request to the route, whose headers will be used to update the routes rate limit state.

results matching ""

    No results matching ""