diff --git a/src/io/github/sammers/pla/Main.java b/src/io/github/sammers/pla/Main.java index 32107b05..fb6e6b47 100644 --- a/src/io/github/sammers/pla/Main.java +++ b/src/io/github/sammers/pla/Main.java @@ -8,6 +8,7 @@ import io.github.sammers.pla.logic.CharacterCache; import io.github.sammers.pla.logic.Ladder; import io.github.sammers.pla.logic.Refs; +import io.prometheus.metrics.core.metrics.Gauge; import io.prometheus.metrics.exporter.httpserver.HTTPServer; import io.prometheus.metrics.instrumentation.jvm.JvmMetrics; import io.reactivex.Scheduler; @@ -81,7 +82,12 @@ public static void main(String[] args) throws IOException { final Refs refs = new Refs(); final Map cutoffsMap = new ConcurrentHashMap<>(); final ExtCharacterSearcher extSearch = new ExtCharacterSearcher(webClient); - final BlizzardAPI blizzardAPI = new BlizzardAPI(clientId, clientSecret, webClient, refs, characterCache, cutoffsMap); + Gauge permitsMetric = Gauge.builder() + .name("RateLimiterPermits") + .help("How many permits are left in the rate limiter") + .labelNames("name") + .register(); + final BlizzardAPI blizzardAPI = new BlizzardAPI(permitsMetric, clientId, clientSecret, webClient, refs, characterCache, cutoffsMap); DB db = new DB(mongoClient); Ladder ladder = new Ladder(webClient, db, blizzardAPI, characterCache, refs, cutoffsMap); ladder.start(); diff --git a/src/io/github/sammers/pla/blizzard/BlizzardAPI.java b/src/io/github/sammers/pla/blizzard/BlizzardAPI.java index 2a8c07d0..4dc58737 100644 --- a/src/io/github/sammers/pla/blizzard/BlizzardAPI.java +++ b/src/io/github/sammers/pla/blizzard/BlizzardAPI.java @@ -5,6 +5,7 @@ import io.github.sammers.pla.logic.CharacterCache; import io.github.sammers.pla.logic.RateLimiter; import io.github.sammers.pla.logic.Refs; +import io.prometheus.metrics.core.metrics.Gauge; import io.reactivex.Completable; import io.reactivex.Maybe; import io.reactivex.Single; @@ -46,17 +47,18 @@ public class BlizzardAPI { private final Map cutoffs; private final String clientId; private final AtomicReference token = new AtomicReference<>(); - private final RateLimiter rateLimiter = new RateLimiter("100 per sec", 100, TimeUnit.SECONDS, 1000, - Optional.of(new RateLimiter("36000 per hr", 36000, TimeUnit.HOURS, 1000, Optional.empty(), Main.VTHREAD_SCHEDULER)), - Main.VTHREAD_SCHEDULER); + private final RateLimiter rateLimiter; - public BlizzardAPI(String clientId, String clientSecret, WebClient webClient, Refs refs, CharacterCache characterCache, Map cutoffs) { + public BlizzardAPI(Gauge permits, String clientId, String clientSecret, WebClient webClient, Refs refs, CharacterCache characterCache, Map cutoffs) { this.clientId = clientId; this.clientSecret = clientSecret; this.webClient = webClient; this.refs = refs; this.characterCache = characterCache; this.cutoffs = cutoffs; + rateLimiter = new RateLimiter("100 per sec", permits, 100, TimeUnit.SECONDS, 1000, + Optional.of(new RateLimiter("36000 per hr", permits, 36000, TimeUnit.HOURS, 1000, Optional.empty(), Main.VTHREAD_SCHEDULER)), + Main.VTHREAD_SCHEDULER); } public Completable rpsToken() { diff --git a/src/io/github/sammers/pla/logic/RateLimiter.java b/src/io/github/sammers/pla/logic/RateLimiter.java index db94a288..57d7c893 100644 --- a/src/io/github/sammers/pla/logic/RateLimiter.java +++ b/src/io/github/sammers/pla/logic/RateLimiter.java @@ -19,13 +19,10 @@ public class RateLimiter { private final ConcurrentLinkedQueue requestQes = new ConcurrentLinkedQueue<>(); private final int maxRequestsTotal; private final Optional parent; - private final Gauge permitsMetric = Gauge.builder() - .name("RateLimiterPermits") - .help("How many permits are left in the rate limiter") - .labelNames("name") - .register(); + private final Gauge permitsMetric; - public RateLimiter(String name, int permits, TimeUnit per, int maxRequestsTotal, Optional parent, Scheduler scheduler) { + public RateLimiter(String name, Gauge permitsMetric, int permits, TimeUnit per, int maxRequestsTotal, Optional parent, Scheduler scheduler) { + this.permitsMetric = permitsMetric; this.maxRequestsTotal = maxRequestsTotal; this.parent = parent; long now = System.currentTimeMillis(); diff --git a/test/io/github/sammers/pla/logic/RateLimiterTest.java b/test/io/github/sammers/pla/logic/RateLimiterTest.java index 9c44a57d..e5719f33 100644 --- a/test/io/github/sammers/pla/logic/RateLimiterTest.java +++ b/test/io/github/sammers/pla/logic/RateLimiterTest.java @@ -1,5 +1,6 @@ package io.github.sammers.pla.logic; +import io.prometheus.metrics.core.metrics.Gauge; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import io.reactivex.Completable; @@ -13,13 +14,18 @@ import java.util.ArrayList; public class RateLimiterTest { - + public static final Scheduler VTHREAD_EXECUTOR = io.reactivex.schedulers.Schedulers.from(Executors.newVirtualThreadPerTaskExecutor()); + public static final Gauge permitsMetric = Gauge.builder() + .name("RateLimiterPermits") + .help("How many permits are left in the rate limiter") + .labelNames("name") + .register(); @Test public void basic() { Long start = System.currentTimeMillis(); - RateLimiter woWAPIRateLimiter = new RateLimiter(2, TimeUnit.SECONDS, 100, Optional.empty(), VTHREAD_EXECUTOR); + RateLimiter woWAPIRateLimiter = new RateLimiter("Test", permitsMetric, 2, TimeUnit.SECONDS, 100, Optional.empty(), VTHREAD_EXECUTOR); Completable.merge( List.of( woWAPIRateLimiter.request(), @@ -40,7 +46,7 @@ public void basic() { @Test public void basicWithHr() { Long start = System.currentTimeMillis(); - RateLimiter woWAPIRateLimiter = new RateLimiter(36000, TimeUnit.HOURS, 100, Optional.empty(), VTHREAD_EXECUTOR); + RateLimiter woWAPIRateLimiter = new RateLimiter("Test", permitsMetric, 36000, TimeUnit.HOURS, 100, Optional.empty(), VTHREAD_EXECUTOR); Completable.merge( List.of( woWAPIRateLimiter.request(), @@ -61,9 +67,9 @@ public void basicWithHr() { @Test public void complexBlizzardLike() { Long start = System.currentTimeMillis(); - RateLimiter woWAPIRateLimiter = new RateLimiter(10, TimeUnit.SECONDS, 100, - Optional.of(new RateLimiter(10, TimeUnit.SECONDS, 100, Optional.empty(), VTHREAD_EXECUTOR)), - VTHREAD_EXECUTOR); + RateLimiter woWAPIRateLimiter = new RateLimiter("Test", permitsMetric, 10, TimeUnit.SECONDS, 100, + Optional.of(new RateLimiter("Test", permitsMetric, 10, TimeUnit.SECONDS, 100, Optional.empty(), VTHREAD_EXECUTOR)), + VTHREAD_EXECUTOR); List x = new ArrayList<>(); for (int i = 0; i < 10; i++) { x.add(woWAPIRateLimiter.request());