Created
September 21, 2023 08:37
-
-
Save romanbsd/b92b12fd7c5d75650b56bc1094ad7f6b to your computer and use it in GitHub Desktop.
asynchronously reserve permission
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyVerticle extends AbstractVerticle { | |
/* Try to acquire permits asynchronously */ | |
private <T> Supplier<CompletionStage<T>> decorateCompletionStage( | |
RateLimiter rateLimiter, | |
Supplier<CompletionStage<T>> supplier) { | |
return () -> { | |
final CompletableFuture<T> promise = new CompletableFuture<>(); | |
final BiConsumer<T, Throwable> biConsumer = (result, throwable) -> { | |
if (throwable != null) { | |
rateLimiter.onError(throwable); | |
promise.completeExceptionally(throwable); | |
} else { | |
rateLimiter.onResult(result); | |
promise.complete(result); | |
} | |
}; | |
try { | |
long waitNano = rateLimiter.reservePermission(); | |
if (waitNano == 0) { | |
supplier.get().whenComplete(biConsumer); | |
} else if (waitNano > 0) { | |
long waitMs = waitNano / 1000000; | |
LOG.warn("Waiting {}ms", waitMs); | |
vertx.setTimer(waitMs, u -> supplier.get().whenComplete(biConsumer)); | |
} else { | |
promise.completeExceptionally(RequestNotPermitted.createRequestNotPermitted(rateLimiter)); | |
} | |
} catch (Exception exception) { | |
rateLimiter.onError(exception); | |
promise.completeExceptionally(exception); | |
} | |
return promise; | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment