Skip to content

Commit

Permalink
Use LongSupplier for clock in AbstractLimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
kilink committed Sep 15, 2024
1 parent 53eee34 commit dd1ba7f
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -35,7 +36,7 @@ public abstract static class Builder<BuilderT extends Builder<BuilderT>> {
private static final AtomicInteger idCounter = new AtomicInteger();

private Limit limit = VegasLimit.newDefault();
private Supplier<Long> clock = System::nanoTime;
private LongSupplier clock = System::nanoTime;

protected String name = "unnamed-" + idCounter.incrementAndGet();
protected MetricRegistry registry = EmptyMetricRegistry.INSTANCE;
Expand All @@ -53,7 +54,16 @@ public BuilderT limit(Limit limit) {
return self();
}

/**
* @deprecated use {@link #nanoClock(LongSupplier)}
*/
@Deprecated
public BuilderT clock(Supplier<Long> clock) {
this.clock = clock::get;
return self();
}

public BuilderT nanoClock(LongSupplier clock) {
this.clock = clock;
return self();
}
Expand Down Expand Up @@ -91,7 +101,7 @@ protected final BuilderT bypassLimitResolverInternal(Predicate<?> shouldBypass)
}

private final AtomicInteger inFlight = new AtomicInteger();
private final Supplier<Long> clock;
private final LongSupplier clock;
private final Limit limitAlgorithm;
private final MetricRegistry.Counter successCounter;
private final MetricRegistry.Counter droppedCounter;
Expand Down Expand Up @@ -148,15 +158,15 @@ 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
public void onSuccess() {
inFlight.decrementAndGet();
successCounter.increment();

limitAlgorithm.onSample(startTime, clock.get() - startTime, currentInflight, false);
limitAlgorithm.onSample(startTime, clock.getAsLong() - startTime, currentInflight, false);
}

@Override
Expand All @@ -170,7 +180,7 @@ public void onDropped() {
inFlight.decrementAndGet();
droppedCounter.increment();

limitAlgorithm.onSample(startTime, clock.get() - startTime, currentInflight, true);
limitAlgorithm.onSample(startTime, clock.getAsLong() - startTime, currentInflight, true);
}
};
}
Expand Down

0 comments on commit dd1ba7f

Please sign in to comment.