Class SequentialRestRateLimiter
- All Implemented Interfaces:
RestRateLimiter
- Get Hash from Path+Method (we call this route)
- Get bucket from Hash+Major (we call this bucketid)
If no hash is known we default to the constant "uninit" hash. The hash is loaded from HTTP responses using the "X-RateLimit-Bucket" response header. This hash is per Method+Path and can be stored indefinitely once received. Some endpoints don't return a hash, this means that the endpoint is uninit and will be in queue with only the major parameter.
To explain this further, lets look at the example of message history. The endpoint to fetch message history is GET/channels/{channel.id}/messages.
This endpoint does not have any rate limit (uninit) and will thus use the hash uninit+GET/channels/{channel.id}/messages.
The bucket id for this will be uninit+GET/channels/{channel.id}/messages:channel_id={channel.id} where {channel.id} would be replaced with the respective id.
This means you can fetch history concurrently for multiple channels, but it will be in sequence for the same channel.
If the endpoint is not uninit we will receive a hash on the first response. Once this happens every uninit bucket will start moving its queue to the correct bucket. This is done during the queue work iteration so many requests to one endpoint would be moved correctly.
For example, the first message sending:
public void onReady(ReadyEvent event) {
TextChannel channel = event.getJDA().getTextChannelById("123");
for (int i = 1; i <= 100; i++) {
channel.sendMessage("Message: " + i).queue();
}
}
This will send 100 messages on startup. At this point we don't yet know the hash for this route, so we put them all in uninit+POST/channels/{channel.id}/messages:channel_id=123.
The bucket iterates the requests in sync and gets the first response. This response provides the hash for this route, and we create a bucket for it.
Once the response is handled we continue with the next request in the uninit bucket and notice the new bucket. We then move all related requests to this bucket.
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.dv8tion.jda.api.requests.RestRateLimiter
RestRateLimiter.GlobalRateLimit, RestRateLimiter.RateLimitConfig, RestRateLimiter.Work -
Field Summary
Fields inherited from interface net.dv8tion.jda.api.requests.RestRateLimiter
GLOBAL_HEADER, HASH_HEADER, LIMIT_HEADER, REMAINING_HEADER, RESET_AFTER_HEADER, RESET_HEADER, RETRY_AFTER_HEADER, SCOPE_HEADER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintCancel all currently queued requests, which are not marked aspriority.voidenqueue(RestRateLimiter.Work task) Enqueue a new request.booleanWhether the queue has stopped accepting new requests.voidIndication to stop accepting new requests.
-
Constructor Details
-
SequentialRestRateLimiter
-
-
Method Details
-
enqueue
Description copied from interface:RestRateLimiterEnqueue a new request.Use
RestRateLimiter.Work.getRoute()to determine the correct bucket.- Specified by:
enqueuein interfaceRestRateLimiter- Parameters:
task- TheRestRateLimiter.Workto enqueue
-
stop
Description copied from interface:RestRateLimiterIndication to stop accepting new requests.- Specified by:
stopin interfaceRestRateLimiter- Parameters:
shutdown- Whether to also cancel previously queued requestcallback- Function to call once all requests are completed, used for final cleanup
-
isStopped
public boolean isStopped()Description copied from interface:RestRateLimiterWhether the queue has stopped accepting new requests.- Specified by:
isStoppedin interfaceRestRateLimiter- Returns:
- True, if the queue is stopped
-
cancelRequests
public int cancelRequests()Description copied from interface:RestRateLimiterCancel all currently queued requests, which are not marked aspriority.- Specified by:
cancelRequestsin interfaceRestRateLimiter- Returns:
- The number of cancelled requests
-