Skip to content

Alter rate limiting approach to be better in multi-threaded contexts #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/main/java/is/swan/mcmarketapi/request/Client.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package is.swan.mcmarketapi.request;

import is.swan.mcmarketapi.Token;
import is.swan.mcmarketapi.request.Request.Method;
import is.swan.mcmarketapi.utils.HTTPUtil;

public class Client {

private final Token token;
private final Throttler throttler;

public Client(Token token) {
this.token = token;
this.throttler = new Throttler();
}

public Response send(Request request) {
Expand All @@ -24,18 +27,33 @@ public Response send(Request request) {
}

public Response sendOrWait(Request request) {
Response response = getResponse(request);

while (response.isRatelimited()) {
long stallFor;
while ((stallFor = this.throttler.stallFor(request.getMethod())) > 0) {
try {
Thread.sleep(response.getMillisecondsToWait());
Thread.sleep(stallFor);
} catch (InterruptedException e) {
e.printStackTrace();
}

response = getResponse(request);
}

Response response = getResponse(request);

if (response.isRatelimited()) {
if (request.getMethod() == Method.GET) {
throttler.setRead(response.getMillisecondsToWait());
} else {
throttler.setWrite(response.getMillisecondsToWait());
}

return sendOrWait(request);
}

if (request.getMethod() == Method.GET) {
throttler.resetRead();
} else {
throttler.resetWrite();
}

if (response.getError() != null) {
return response;
}
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/is/swan/mcmarketapi/request/Throttler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package is.swan.mcmarketapi.request;

import java.util.concurrent.atomic.AtomicLong;

import is.swan.mcmarketapi.request.Request.Method;

public class Throttler {
private final AtomicLong readLastRetry = new AtomicLong(0);
private final AtomicLong readLastRequest = new AtomicLong(System.currentTimeMillis());

private final AtomicLong writeLastRetry = new AtomicLong(0);
private final AtomicLong writeLastRequest = new AtomicLong(System.currentTimeMillis());

public long stallFor(Method method) {
long time = System.currentTimeMillis();

if (method == Method.GET) {
return Throttler.stalForHelper(this.readLastRetry, this.readLastRequest, time);
} else {
return Throttler.stalForHelper(this.writeLastRetry, this.writeLastRequest, time);
}
}

private static long stalForHelper(AtomicLong aLastRetry, AtomicLong aLastRequest, long time){
long lastRetry = aLastRetry.getAcquire();
long lastRequest = aLastRequest.getAcquire();

if (lastRetry > 0 && (time - lastRequest) < lastRetry) {
return lastRetry - (time - lastRequest);
} else {
return 0;
}
}

public void setRead(long value) {
readLastRetry.setRelease(value);
readLastRequest.setRelease(System.currentTimeMillis());
}

public void setWrite(long value) {
writeLastRetry.setRelease(value);
writeLastRequest.setRelease(System.currentTimeMillis());
}

public void resetRead() {
readLastRetry.setRelease(0);
readLastRequest.setRelease(System.currentTimeMillis());
}

public void resetWrite() {
writeLastRetry.setRelease(0);
writeLastRequest.setRelease(System.currentTimeMillis());
}
}