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
-
Method Summary
Modifier and TypeMethodDescriptionint
Cancel all currently queued requests, which are not marked aspriority
.void
enqueue
(RestRateLimiter.Work task) Enqueue a new request.boolean
Whether the queue has stopped accepting new requests.void
Indication to stop accepting new requests.
-
Constructor Details
-
SequentialRestRateLimiter
-
-
Method Details
-
enqueue
Description copied from interface:RestRateLimiter
Enqueue a new request.Use
RestRateLimiter.Work.getRoute()
to determine the correct bucket.- Specified by:
enqueue
in interfaceRestRateLimiter
- Parameters:
task
- TheRestRateLimiter.Work
to enqueue
-
stop
Description copied from interface:RestRateLimiter
Indication to stop accepting new requests.- Specified by:
stop
in 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:RestRateLimiter
Whether the queue has stopped accepting new requests.- Specified by:
isStopped
in interfaceRestRateLimiter
- Returns:
- True, if the queue is stopped
-
cancelRequests
public int cancelRequests()Description copied from interface:RestRateLimiter
Cancel all currently queued requests, which are not marked aspriority
.- Specified by:
cancelRequests
in interfaceRestRateLimiter
- Returns:
- The number of cancelled requests
-