Skip to content
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

446: Adhere to GitHub rate limits #690

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -24,9 +24,10 @@

import java.io.IOException;
import java.net.http.*;
import java.time.Duration;
import java.time.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.*;
import java.util.logging.Logger;

enum RestRequestCache {
@@ -62,18 +63,36 @@ public int hashCode() {
private final Map<RequestContext, HttpResponse<String>> cachedResponses = new ConcurrentHashMap<>();
private final HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
private final Logger log = Logger.getLogger("org.openjdk.skara.network");
private final Map<String, Lock> authLocks = new HashMap<>();
private final Map<String, Instant> lastUpdates = new ConcurrentHashMap<>();

HttpResponse<String> send(String authId, HttpRequest.Builder requestBuilder) throws IOException, InterruptedException {
if (authId == null) {
authId = "anonymous";
}
var unauthenticatedRequest = requestBuilder.build();
var requestContext = new RequestContext(authId, unauthenticatedRequest);
synchronized (authLocks) {
if (!authLocks.containsKey(authId)) {
authLocks.put(authId, new ReentrantLock());
}
}
var authLock = authLocks.get(authId);
if (unauthenticatedRequest.method().equals("GET")) {
var cached = cachedResponses.get(requestContext);
if (cached != null) {
var tag = cached.headers().firstValue("ETag");
tag.ifPresent(value -> requestBuilder.header("If-None-Match", value));
}
var finalRequest = requestBuilder.build();
var response = client.send(finalRequest, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> response;
try {
// Perform requests using a certain account serially
authLock.lock();
response = client.send(finalRequest, HttpResponse.BodyHandlers.ofString());
} finally {
authLock.unlock();
}
if (response.statusCode() == 304) {
log.finer("Using cached response for " + finalRequest + " (" + authId + ")");
return cached;
@@ -85,7 +104,28 @@ HttpResponse<String> send(String authId, HttpRequest.Builder requestBuilder) thr
} else {
var finalRequest = requestBuilder.build();
log.finer("Not using response cache for " + finalRequest + " (" + authId + ")");
return client.send(finalRequest, HttpResponse.BodyHandlers.ofString());
Instant lastUpdate;
try {
authLock.lock();
lastUpdate = lastUpdates.getOrDefault(authId, Instant.now().minus(Duration.ofDays(1)));
lastUpdates.put(authId, Instant.now());
} finally {
authLock.unlock();
}
// Perform at most one update per second
var requiredDelay = Duration.between(Instant.now().minus(Duration.ofSeconds(1)), lastUpdate);
if (!requiredDelay.isNegative()) {
try {
Thread.sleep(requiredDelay.toMillis());
} catch (InterruptedException ignored) {
}
}
try {
authLock.lock();
return client.send(finalRequest, HttpResponse.BodyHandlers.ofString());
} finally {
authLock.unlock();
}
}
}
}