From c8321045a74c2a212061978111d6a0d0040f68ad Mon Sep 17 00:00:00 2001 From: Patrick Strawderman Date: Sat, 14 Sep 2024 17:00:41 -0700 Subject: [PATCH] Use LongSupplier for clock in AbstractLimiter --- .../limits/limiter/AbstractLimiter.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limiter/AbstractLimiter.java b/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limiter/AbstractLimiter.java index e4cb9e0..f17fe36 100644 --- a/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limiter/AbstractLimiter.java +++ b/concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limiter/AbstractLimiter.java @@ -24,6 +24,7 @@ import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.LongSupplier; import java.util.function.Predicate; import java.util.function.Supplier; @@ -35,7 +36,7 @@ public abstract static class Builder> { private static final AtomicInteger idCounter = new AtomicInteger(); private Limit limit = VegasLimit.newDefault(); - private Supplier clock = System::nanoTime; + private LongSupplier clock = System::nanoTime; protected String name = "unnamed-" + idCounter.incrementAndGet(); protected MetricRegistry registry = EmptyMetricRegistry.INSTANCE; @@ -53,7 +54,13 @@ public BuilderT limit(Limit limit) { return self(); } + @Deprecated public BuilderT clock(Supplier clock) { + this.clock = clock::get; + return self(); + } + + public BuilderT clock(LongSupplier clock) { this.clock = clock; return self(); } @@ -91,7 +98,7 @@ protected final BuilderT bypassLimitResolverInternal(Predicate shouldBypass) } private final AtomicInteger inFlight = new AtomicInteger(); - private final Supplier clock; + private final LongSupplier clock; private final Limit limitAlgorithm; private final MetricRegistry.Counter successCounter; private final MetricRegistry.Counter droppedCounter; @@ -148,7 +155,7 @@ public void onDropped() { } protected Listener createListener() { - final long startTime = clock.get(); + final long startTime = clock.getAsLong(); final int currentInflight = inFlight.incrementAndGet(); return new Listener() { @Override @@ -156,7 +163,7 @@ public void onSuccess() { inFlight.decrementAndGet(); successCounter.increment(); - limitAlgorithm.onSample(startTime, clock.get() - startTime, currentInflight, false); + limitAlgorithm.onSample(startTime, clock.getAsLong() - startTime, currentInflight, false); } @Override @@ -170,7 +177,7 @@ public void onDropped() { inFlight.decrementAndGet(); droppedCounter.increment(); - limitAlgorithm.onSample(startTime, clock.get() - startTime, currentInflight, true); + limitAlgorithm.onSample(startTime, clock.getAsLong() - startTime, currentInflight, true); } }; }