Skip to content

Commit

Permalink
Let interactions and webhook not consume global ratelimits
Browse files Browse the repository at this point in the history
  • Loading branch information
bassner committed Mar 14, 2022
1 parent f858856 commit c9c4371
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public CompletableFuture<Void> sendInitialResponse(InteractionBase interaction)
return new RestRequest<Void>(interaction.getApi(),
RestMethod.POST, RestEndpoint.INTERACTION_RESPONSE)
.setUrlParameters(interaction.getIdAsString(), interaction.getToken())
.consumeGlobalRatelimit(false)
.includeAuthorizationHeader(false)
.setBody(topBody)
.execute(result -> null);
}
Expand All @@ -52,14 +54,18 @@ public CompletableFuture<Void> deleteInitialResponse(InteractionBase interaction
return new RestRequest<Void>(interaction.getApi(),
RestMethod.DELETE, RestEndpoint.ORIGINAL_INTERACTION_RESPONSE)
.setUrlParameters(Long.toUnsignedString(interaction.getApplicationId()), interaction.getToken())
.consumeGlobalRatelimit(false)
.includeAuthorizationHeader(false)
.execute(result -> null);
}

@Override
public CompletableFuture<Message> editOriginalResponse(InteractionBase interaction) {
RestRequest<Message> request = new RestRequest<Message>(interaction.getApi(),
RestMethod.PATCH, RestEndpoint.ORIGINAL_INTERACTION_RESPONSE)
.setUrlParameters(Long.toUnsignedString(interaction.getApplicationId()), interaction.getToken());
.setUrlParameters(Long.toUnsignedString(interaction.getApplicationId()), interaction.getToken())
.consumeGlobalRatelimit(false)
.includeAuthorizationHeader(false);

return executeResponse(request);
}
Expand All @@ -68,7 +74,9 @@ public CompletableFuture<Message> editOriginalResponse(InteractionBase interacti
public CompletableFuture<Message> sendFollowupMessage(InteractionBase interaction) {
RestRequest<Message> request = new RestRequest<Message>(interaction.getApi(),
RestMethod.POST, RestEndpoint.WEBHOOK_SEND)
.setUrlParameters(Long.toUnsignedString(interaction.getApplicationId()), interaction.getToken());
.setUrlParameters(Long.toUnsignedString(interaction.getApplicationId()), interaction.getToken())
.consumeGlobalRatelimit(false)
.includeAuthorizationHeader(false);

return executeResponse(request);
}
Expand All @@ -85,6 +93,8 @@ public CompletableFuture<Void> updateOriginalMessage(InteractionBase interaction
return new RestRequest<Void>(interaction.getApi(),
RestMethod.POST, RestEndpoint.INTERACTION_RESPONSE)
.setUrlParameters(interaction.getIdAsString(), interaction.getToken())
.consumeGlobalRatelimit(false)
.includeAuthorizationHeader(false)
.setBody(topBody)
.execute(result -> null);
}
Expand All @@ -95,6 +105,8 @@ public CompletableFuture<Void> deleteFollowupMessage(InteractionBase interaction
RestEndpoint.WEBHOOK_MESSAGE)
.setUrlParameters(Long.toUnsignedString(interaction.getApplicationId()),
interaction.getToken(), messageId)
.consumeGlobalRatelimit(false)
.includeAuthorizationHeader(false)
.execute(result -> null);
}

Expand All @@ -103,7 +115,9 @@ public CompletableFuture<Message> editFollowupMessage(InteractionBase interactio
RestRequest<Message> request = new RestRequest<Message>(interaction.getApi(), RestMethod.PATCH,
RestEndpoint.WEBHOOK_MESSAGE)
.setUrlParameters(Long.toUnsignedString(interaction.getApplicationId()),
interaction.getToken(), messageId);
interaction.getToken(), messageId)
.consumeGlobalRatelimit(false)
.includeAuthorizationHeader(false);

return executeResponse(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,9 @@ protected CompletableFuture<Message> send(String webhookId, String webhookToken,
RestRequest<Message> request =
new RestRequest<Message>(api, RestMethod.POST, RestEndpoint.WEBHOOK_SEND)
.addQueryParameter("wait", Boolean.toString(wait))
.setUrlParameters(webhookId, webhookToken);
.setUrlParameters(webhookId, webhookToken)
.consumeGlobalRatelimit(false)
.includeAuthorizationHeader(false);
CompletableFuture<Message> future = new CompletableFuture<>();
if (!attachments.isEmpty() || embeds.stream().anyMatch(EmbedBuilder::requiresAttachments)) {
// We access files etc. so this should be async
Expand Down Expand Up @@ -753,4 +755,4 @@ protected void prepareComponents(ObjectNode body, boolean evenIfEmpty) {
body.set("components", componentsNode);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public CompletableFuture<Void> respondWithChoices(List<SlashCommandOptionChoice>
return new RestRequest<Void>(getApi(),
RestMethod.POST, RestEndpoint.INTERACTION_RESPONSE)
.setUrlParameters(getIdAsString(), getToken())
.consumeGlobalRatelimit(false)
.includeAuthorizationHeader(false)
.setBody(topBody)
.execute(result -> null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public CompletableFuture<InteractionOriginalResponseUpdater> respondLater(boolea
return new RestRequest<InteractionOriginalResponseUpdater>(this.api,
RestMethod.POST, RestEndpoint.INTERACTION_RESPONSE)
.setUrlParameters(getIdAsString(), token)
.consumeGlobalRatelimit(false)
.includeAuthorizationHeader(false)
.setBody(ephemeral ? RESPOND_LATER_EPHEMERAL_BODY : RESPOND_LATER_BODY)
.execute(result -> new InteractionOriginalResponseUpdaterImpl(this));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public CompletableFuture<Void> acknowledge() {
return new RestRequest<Void>(getApi(),
RestMethod.POST, RestEndpoint.INTERACTION_RESPONSE)
.setUrlParameters(getIdAsString(), getToken())
.consumeGlobalRatelimit(false)
.includeAuthorizationHeader(false)
.setBody(UPDATE_LATER_BODY)
.execute(result -> null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class RestRequest<T> {
private final RestEndpoint endpoint;

private volatile boolean includeAuthorizationHeader = true;
private volatile boolean consumeGlobalRatelimit = true;
private volatile String[] urlParameters = new String[0];
private final Map<String, String> queryParameters = new HashMap<>();
private final Map<String, String> headers = new HashMap<>();
Expand Down Expand Up @@ -260,6 +261,17 @@ public RestRequest<T> includeAuthorizationHeader(boolean includeAuthorizationHea
return this;
}

/**
* Sets if this request should respect the global ratelimit.
*
* @param consumeGlobalRatelimit Whether the request should respect the global ratelimit.
* @return The current instance in order to chain call methods.
*/
public RestRequest<T> consumeGlobalRatelimit(boolean consumeGlobalRatelimit) {
this.consumeGlobalRatelimit = consumeGlobalRatelimit;
return this;
}

/**
* Executes the request. This will automatically retry if we hit a ratelimit.
*
Expand Down Expand Up @@ -313,13 +325,15 @@ public RestRequestInformation asRestRequestInformation() {
* @throws Exception If something went wrong while executing the request.
*/
public RestRequestResult executeBlocking() throws Exception {
api.getGlobalRatelimiter().ifPresent(ratelimiter -> {
try {
ratelimiter.requestQuota();
} catch (InterruptedException e) {
logger.warn("Encountered unexpected ratelimiter interrupt", e);
}
});
if (consumeGlobalRatelimit) {
api.getGlobalRatelimiter().ifPresent(ratelimiter -> {
try {
ratelimiter.requestQuota();
} catch (InterruptedException e) {
logger.warn("Encountered unexpected ratelimiter interrupt", e);
}
});
}
Request.Builder requestBuilder = new Request.Builder();
HttpUrl.Builder httpUrlBuilder = endpoint.getOkHttpUrl(urlParameters).newBuilder();
queryParameters.forEach(httpUrlBuilder::addQueryParameter);
Expand Down

0 comments on commit c9c4371

Please sign in to comment.