Skip to content

Commit daebd31

Browse files
authored
Merge pull request #206 from kilink/limiter-long-supplier-clock
Use LongSupplier for clock in AbstractLimiter
2 parents 53eee34 + dd1ba7f commit daebd31

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

concurrency-limits-core/src/main/java/com/netflix/concurrency/limits/limiter/AbstractLimiter.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.util.Optional;
2626
import java.util.concurrent.atomic.AtomicInteger;
27+
import java.util.function.LongSupplier;
2728
import java.util.function.Predicate;
2829
import java.util.function.Supplier;
2930

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

3738
private Limit limit = VegasLimit.newDefault();
38-
private Supplier<Long> clock = System::nanoTime;
39+
private LongSupplier clock = System::nanoTime;
3940

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

57+
/**
58+
* @deprecated use {@link #nanoClock(LongSupplier)}
59+
*/
60+
@Deprecated
5661
public BuilderT clock(Supplier<Long> clock) {
62+
this.clock = clock::get;
63+
return self();
64+
}
65+
66+
public BuilderT nanoClock(LongSupplier clock) {
5767
this.clock = clock;
5868
return self();
5969
}
@@ -91,7 +101,7 @@ protected final BuilderT bypassLimitResolverInternal(Predicate<?> shouldBypass)
91101
}
92102

93103
private final AtomicInteger inFlight = new AtomicInteger();
94-
private final Supplier<Long> clock;
104+
private final LongSupplier clock;
95105
private final Limit limitAlgorithm;
96106
private final MetricRegistry.Counter successCounter;
97107
private final MetricRegistry.Counter droppedCounter;
@@ -148,15 +158,15 @@ public void onDropped() {
148158
}
149159

150160
protected Listener createListener() {
151-
final long startTime = clock.get();
161+
final long startTime = clock.getAsLong();
152162
final int currentInflight = inFlight.incrementAndGet();
153163
return new Listener() {
154164
@Override
155165
public void onSuccess() {
156166
inFlight.decrementAndGet();
157167
successCounter.increment();
158168

159-
limitAlgorithm.onSample(startTime, clock.get() - startTime, currentInflight, false);
169+
limitAlgorithm.onSample(startTime, clock.getAsLong() - startTime, currentInflight, false);
160170
}
161171

162172
@Override
@@ -170,7 +180,7 @@ public void onDropped() {
170180
inFlight.decrementAndGet();
171181
droppedCounter.increment();
172182

173-
limitAlgorithm.onSample(startTime, clock.get() - startTime, currentInflight, true);
183+
limitAlgorithm.onSample(startTime, clock.getAsLong() - startTime, currentInflight, true);
174184
}
175185
};
176186
}

0 commit comments

Comments
 (0)