|
| 1 | +package net.hypixel.api.reactor; |
| 2 | + |
| 3 | +import io.netty.handler.codec.http.HttpResponseStatus; |
| 4 | +import net.hypixel.api.http.HypixelHttpClient; |
| 5 | +import net.hypixel.api.http.HypixelHttpResponse; |
| 6 | +import reactor.core.Disposable; |
| 7 | +import reactor.core.publisher.Flux; |
| 8 | +import reactor.core.publisher.Mono; |
| 9 | +import reactor.core.publisher.MonoSink; |
| 10 | +import reactor.core.scheduler.Schedulers; |
| 11 | +import reactor.netty.http.client.HttpClient; |
| 12 | +import reactor.netty.http.client.HttpClientResponse; |
| 13 | +import reactor.util.function.Tuple2; |
| 14 | + |
| 15 | +import java.time.Duration; |
| 16 | +import java.util.UUID; |
| 17 | +import java.util.concurrent.ArrayBlockingQueue; |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 21 | + |
| 22 | +public class ReactorHttpClient implements HypixelHttpClient { |
| 23 | + private final HttpClient httpClient; |
| 24 | + private final UUID apiKey; |
| 25 | + |
| 26 | + // Marker to reset the request counter and release waiting threads |
| 27 | + private final AtomicBoolean firstRequestReturned = new AtomicBoolean(false); |
| 28 | + // Marker to only schedule a reset clock once on error 429 |
| 29 | + private final AtomicBoolean overflowStartedNewClock = new AtomicBoolean(false); |
| 30 | + |
| 31 | + // Callbacks that will trigger their corresponding requests |
| 32 | + private final ArrayBlockingQueue<RequestCallback> blockingQueue; |
| 33 | + |
| 34 | + // For shutting down the flux that emits request callbacks |
| 35 | + private final Disposable requestCallbackFluxDisposable; |
| 36 | + |
| 37 | + private final Object lock = new Object(); |
| 38 | + |
| 39 | + /* |
| 40 | + * How many requests we can send before reaching the limit |
| 41 | + * Starts as 1 so the first request returns and resets this value before allowing other requests to be sent. |
| 42 | + */ |
| 43 | + private int actionsLeftThisMinute = 1; |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructs a new instance of this client using the specified API key. |
| 47 | + * |
| 48 | + * @param apiKey the key associated with this connection |
| 49 | + * @param minDelayBetweenRequests minimum time between sending requests (in ms) default is 8 |
| 50 | + * @param bufferCapacity fixed size of blockingQueue |
| 51 | + */ |
| 52 | + public ReactorHttpClient(UUID apiKey, long minDelayBetweenRequests, int bufferCapacity) { |
| 53 | + this.apiKey = apiKey; |
| 54 | + this.httpClient = HttpClient.create().secure(); |
| 55 | + this.blockingQueue = new ArrayBlockingQueue<>(bufferCapacity); |
| 56 | + |
| 57 | + this.requestCallbackFluxDisposable = Flux.<RequestCallback>generate((synchronousSink) -> { |
| 58 | + try { |
| 59 | + RequestCallback callback = blockingQueue.take(); |
| 60 | + // prune skipped/completed requests to avoid counting them |
| 61 | + while (callback.isCanceled()) { |
| 62 | + callback = blockingQueue.take(); |
| 63 | + } |
| 64 | + |
| 65 | + synchronized (lock) { |
| 66 | + while (this.actionsLeftThisMinute <= 0) { |
| 67 | + lock.wait(); |
| 68 | + } |
| 69 | + |
| 70 | + actionsLeftThisMinute--; |
| 71 | + } |
| 72 | + synchronousSink.next(callback); |
| 73 | + } catch (InterruptedException e) { |
| 74 | + throw new AssertionError("This should not have been possible", e); |
| 75 | + } |
| 76 | + }).subscribeOn(Schedulers.boundedElastic()).delayElements(Duration.ofMillis(minDelayBetweenRequests), Schedulers.boundedElastic()).subscribe(RequestCallback::sendRequest); |
| 77 | + } |
| 78 | + |
| 79 | + public ReactorHttpClient(UUID apiKey, long minDelayBetweenRequests) |
| 80 | + { |
| 81 | + this(apiKey, minDelayBetweenRequests, 500); |
| 82 | + } |
| 83 | + |
| 84 | + public ReactorHttpClient(UUID apiKey, int bufferCapacity) |
| 85 | + { |
| 86 | + this(apiKey, 8, bufferCapacity); |
| 87 | + } |
| 88 | + |
| 89 | + public ReactorHttpClient(UUID apiKey) |
| 90 | + { |
| 91 | + this(apiKey, 8, 500); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Canceling the returned future will result in canceling the request if possible |
| 96 | + */ |
| 97 | + @Override |
| 98 | + public CompletableFuture<HypixelHttpResponse> makeRequest(String url) { |
| 99 | + return toHypixelResponseFuture(makeRequest(url, false)); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Canceling the returned future will result in canceling the request if possible |
| 104 | + */ |
| 105 | + @Override |
| 106 | + public CompletableFuture<HypixelHttpResponse> makeAuthenticatedRequest(String url) { |
| 107 | + return toHypixelResponseFuture(makeRequest(url, true)); |
| 108 | + } |
| 109 | + |
| 110 | + private static CompletableFuture<HypixelHttpResponse> toHypixelResponseFuture(Mono<Tuple2<String, Integer>> result) { |
| 111 | + return result.map(tuple -> new HypixelHttpResponse(tuple.getT2(), tuple.getT1())) |
| 112 | + .toFuture(); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void shutdown() { |
| 117 | + this.requestCallbackFluxDisposable.dispose(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Makes a request to the Hypixel api and returns a {@link Mono<Tuple2<String, Integer>>} containing |
| 122 | + * the response body and status code, canceling this mono will prevent the request from being sent if possible |
| 123 | + * @param path full url |
| 124 | + * @param isAuthenticated whether to enable authentication or not |
| 125 | + */ |
| 126 | + public Mono<Tuple2<String, Integer>> makeRequest(String path, boolean isAuthenticated) { |
| 127 | + return Mono.<Tuple2<String, Integer>>create(sink -> { |
| 128 | + RequestCallback callback = new RequestCallback(path, sink, isAuthenticated, this); |
| 129 | + |
| 130 | + try { |
| 131 | + this.blockingQueue.put(callback); |
| 132 | + } catch (InterruptedException e) { |
| 133 | + sink.error(e); |
| 134 | + throw new AssertionError("Queue insertion interrupted. This should not have been possible", e); |
| 135 | + } |
| 136 | + }).subscribeOn(Schedulers.boundedElastic()); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Reads response status and retries error 429 (too many requests) |
| 141 | + * The first request after every limit reset will be used to schedule the next limit reset |
| 142 | + * |
| 143 | + * @param response the {@link HttpClientResponse} from our request |
| 144 | + * @param requestCallback the callback controlling our request |
| 145 | + * @return whether to return the request body or wait for a retry |
| 146 | + */ |
| 147 | + private ResponseHandlingResult handleResponse(HttpClientResponse response, RequestCallback requestCallback) throws InterruptedException { |
| 148 | + if (response.status() == HttpResponseStatus.TOO_MANY_REQUESTS) { |
| 149 | + int timeRemaining = Math.max(1, response.responseHeaders().getInt("ratelimit-reset", 10)); |
| 150 | + |
| 151 | + if (this.overflowStartedNewClock.compareAndSet(false, true)) { |
| 152 | + synchronized (lock) { |
| 153 | + this.actionsLeftThisMinute = 0; |
| 154 | + } |
| 155 | + resetForFirstRequest(timeRemaining); |
| 156 | + } |
| 157 | + |
| 158 | + // execute this last to prevent a possible exception from messing up our clock synchronization |
| 159 | + this.blockingQueue.put(requestCallback); |
| 160 | + return new ResponseHandlingResult(false, response.status().code()); |
| 161 | + } |
| 162 | + |
| 163 | + if (this.firstRequestReturned.compareAndSet(false, true)) { |
| 164 | + int timeRemaining = Math.max(1, response.responseHeaders().getInt("ratelimit-reset", 10)); |
| 165 | + int requestsRemaining = response.responseHeaders().getInt("ratelimit-remaining", 110); |
| 166 | + |
| 167 | + synchronized (lock) { |
| 168 | + this.actionsLeftThisMinute = requestsRemaining; |
| 169 | + lock.notifyAll(); |
| 170 | + } |
| 171 | + |
| 172 | + resetForFirstRequest(timeRemaining); |
| 173 | + } |
| 174 | + return new ResponseHandlingResult(true, response.status().code()); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Wakes up all waiting threads in the specified amount of seconds |
| 179 | + * (Adds two seconds to account for sync errors in the server). |
| 180 | + * |
| 181 | + * @param timeRemaining how much time is left until the next reset |
| 182 | + */ |
| 183 | + private void resetForFirstRequest(int timeRemaining) { |
| 184 | + Schedulers.parallel().schedule(() -> { |
| 185 | + this.firstRequestReturned.set(false); |
| 186 | + this.overflowStartedNewClock.set(false); |
| 187 | + synchronized (lock) { |
| 188 | + this.actionsLeftThisMinute = 1; |
| 189 | + lock.notifyAll(); |
| 190 | + } |
| 191 | + }, timeRemaining + 2, TimeUnit.SECONDS); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Controls a request |
| 196 | + */ |
| 197 | + private static class RequestCallback { |
| 198 | + private final String url; |
| 199 | + private final MonoSink<Tuple2<String, Integer>> monoSink; |
| 200 | + private final ReactorHttpClient requestRateLimiter; |
| 201 | + private final boolean isAuthenticated; |
| 202 | + private boolean isCanceled = false; |
| 203 | + |
| 204 | + private RequestCallback(String url, MonoSink<Tuple2<String, Integer>> monoSink, boolean isAuthenticated, ReactorHttpClient requestRateLimiter) { |
| 205 | + this.url = url; |
| 206 | + this.monoSink = monoSink; |
| 207 | + this.requestRateLimiter = requestRateLimiter; |
| 208 | + this.isAuthenticated = isAuthenticated; |
| 209 | + |
| 210 | + this.monoSink.onCancel(() -> { |
| 211 | + synchronized (this) { |
| 212 | + this.isCanceled = true; |
| 213 | + } |
| 214 | + }); |
| 215 | + } |
| 216 | + |
| 217 | + public boolean isCanceled() { |
| 218 | + return this.isCanceled; |
| 219 | + } |
| 220 | + |
| 221 | + private void sendRequest() { |
| 222 | + synchronized (this) { |
| 223 | + if (isCanceled) { |
| 224 | + synchronized (this.requestRateLimiter.lock) { |
| 225 | + this.requestRateLimiter.actionsLeftThisMinute++; |
| 226 | + this.requestRateLimiter.lock.notifyAll(); |
| 227 | + } |
| 228 | + return; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + (this.isAuthenticated ? requestRateLimiter.httpClient.headers(headers -> headers.add("API-Key", requestRateLimiter.apiKey.toString())) : requestRateLimiter.httpClient).get() |
| 233 | + .uri(url) |
| 234 | + .responseSingle((response, body) -> { |
| 235 | + try { |
| 236 | + ResponseHandlingResult result = requestRateLimiter.handleResponse(response, this); |
| 237 | + |
| 238 | + if (result.allowToPass) { |
| 239 | + return body.asString().zipWith(Mono.just(result.statusCode)); |
| 240 | + } |
| 241 | + return Mono.empty(); |
| 242 | + } catch (InterruptedException e) { |
| 243 | + monoSink.error(e); |
| 244 | + throw new AssertionError("ERROR: Queue insertion got interrupted, serious problem! (this should not happen!!)", e); |
| 245 | + } |
| 246 | + }).subscribe(this.monoSink::success); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * Data object |
| 252 | + */ |
| 253 | + private static class ResponseHandlingResult { |
| 254 | + public final boolean allowToPass; |
| 255 | + public final int statusCode; |
| 256 | + |
| 257 | + public ResponseHandlingResult(boolean allowToPass, int statusCode) { |
| 258 | + this.allowToPass = allowToPass; |
| 259 | + this.statusCode = statusCode; |
| 260 | + } |
| 261 | + } |
| 262 | +} |
0 commit comments