Skip to content

Commit

Permalink
Rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
surajkumar committed May 29, 2024
1 parent b880043 commit 1870954
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
60 changes: 60 additions & 0 deletions api/src/main/java/com/javadiscord/jdi/RateLimit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.javadiscord.jdi;

public class RateLimit {
private String bucket;
private int limit;
private int remaining;
private long reset;
private int resetAfter;
private boolean globalRateLimit;

public RateLimit() {}

public String getBucket() {
return bucket;
}

public void setBucket(String bucket) {
this.bucket = bucket;
}

public int getLimit() {
return limit;
}

public void setLimit(int limit) {
this.limit = limit;
}

public int getRemaining() {
return remaining;
}

public void setRemaining(int remaining) {
this.remaining = remaining;
}

public long getReset() {
return reset;
}

public void setReset(long reset) {
this.reset = reset;
}

public int getResetAfter() {
return resetAfter;
}

public void setResetAfter(int resetAfter) {
this.resetAfter = resetAfter;
}

public boolean isGlobalRateLimit() {
return globalRateLimit;
}

public void setGlobalRateLimit(boolean globalRateLimit) {
this.globalRateLimit = globalRateLimit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Map;
Expand All @@ -10,6 +11,8 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;

import com.javadiscord.jdi.RateLimit;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -27,6 +30,8 @@ public class DiscordRequestDispatcher implements Runnable {
private int numberOfRequestsSent;
private long timeSinceLastRequest;

private RateLimit rateLimit = new RateLimit();

public DiscordRequestDispatcher(String botToken) {
this.botToken = botToken;
this.httpClient = HttpClient.newBuilder().build();
Expand All @@ -51,6 +56,14 @@ public void run() {
long currentTime = System.currentTimeMillis();
long elapsed = currentTime - timeSinceLastRequest;

if (rateLimit.getRemaining() == 0 && elapsed < rateLimit.getResetAfter()) {
try {
Thread.sleep(rateLimit.getResetAfter() - elapsed);

Check warning on line 61 in api/src/main/java/com/javadiscord/jdi/internal/api/DiscordRequestDispatcher.java

View workflow job for this annotation

GitHub Actions / qodana

Busy wait

Call to `Thread.sleep()` in a loop, probably busy-waiting
} catch (InterruptedException e) {
/* Ignore */
}
}

if (elapsed < 1000 && numberOfRequestsSent >= 50) {
try {
Thread.sleep(1000 - elapsed);

Check warning on line 69 in api/src/main/java/com/javadiscord/jdi/internal/api/DiscordRequestDispatcher.java

View workflow job for this annotation

GitHub Actions / qodana

Busy wait

Call to `Thread.sleep()` in a loop, probably busy-waiting
Expand Down Expand Up @@ -119,6 +132,17 @@ private void sendRequest(DiscordRequestBuilder discordRequestBuilder) {
HttpResponse<String> response =
httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());

HttpHeaders headers = response.headers();
headers.firstValue("x-ratelimit-bucket").ifPresent(val -> rateLimit.setBucket(val));
headers.firstValue("x-ratelimit-limit")
.ifPresent(val -> rateLimit.setLimit(Integer.parseInt(val)));
headers.firstValue("x-ratelimit-remaining")
.ifPresent(val -> rateLimit.setRemaining(Integer.parseInt(val)));
headers.firstValue("x-ratelimit-reset")
.ifPresent(val -> rateLimit.setReset(Long.parseLong(val)));
headers.firstValue("x-ratelimit-reset-after")
.ifPresent(val -> rateLimit.setResetAfter(Integer.parseInt(val)));

numberOfRequestsSent++;
timeSinceLastRequest = System.currentTimeMillis();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@JsonIgnoreProperties(ignoreUnknown = true)
public record Gateway(
String url,
int shards,
@JsonProperty("url") String url,
@JsonProperty("shards") int shards,
@JsonProperty("session_start_limit") SessionStartLimit sessionStartLimit
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

@JsonIgnoreProperties(ignoreUnknown = true)
public record SessionStartLimit(
int total,
int remaining,
@JsonProperty("total") int total,
@JsonProperty("remaining") int remaining,
@JsonProperty("reset_after") long resetAfter,
@JsonProperty("max_concurrency") int maxConcurrency
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public void start(ConnectionMediator connectionMediator) {
}

public void restart(ConnectionMediator connectionMediator) {

webSocketManager.restart(connectionMediator);
}

Expand Down

0 comments on commit 1870954

Please sign in to comment.