Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: WindowedLimit allow non-updating threads to proceed when updating #205

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;

public class WindowedLimit implements Limit {
Expand Down Expand Up @@ -103,7 +104,7 @@ public WindowedLimit build(Limit delegate) {

private final long minRttThreshold;

private final Object lock = new Object();
private final ReentrantLock lock = new ReentrantLock();

private final SampleWindowFactory sampleWindowFactory;

Expand All @@ -129,7 +130,7 @@ public void notifyOnChange(Consumer<Integer> consumer) {

@Override
public void onSample(long startTime, long rtt, int inflight, boolean didDrop) {
long endTime = startTime + rtt;
final long endTime = startTime + rtt;

if (rtt < minRttThreshold) {
return;
Expand All @@ -138,15 +139,25 @@ public void onSample(long startTime, long rtt, int inflight, boolean didDrop) {
sample.updateAndGet(current -> current.addSample(rtt, inflight, didDrop));

if (endTime > nextUpdateTime) {
synchronized (lock) {
// Double check under the lock
if (endTime > nextUpdateTime) {
SampleWindow current = sample.getAndSet(sampleWindowFactory.newInstance());
nextUpdateTime = endTime + Math.min(Math.max(current.getCandidateRttNanos() * 2, minWindowTime), maxWindowTime);

if (isWindowReady(current)) {
delegate.onSample(startTime, current.getTrackedRttNanos(), current.getMaxInFlight(), current.didDrop());
// Only allow one thread at propagate the sample to the delegate.
// Other threads are free to continue. They will continue to accumulate
// against 'sample'.
boolean haveLock = lock.tryLock();
if (haveLock) {
try {
// Double check under the lock, in case of the flow:
// A : check end time , lock , , set nextUpdateTime , unlock ,
// B : check end time , lock , [[nextUpdateTime has changed!]]
if (endTime > nextUpdateTime) {
SampleWindow current = sample.getAndSet(sampleWindowFactory.newInstance());
nextUpdateTime = endTime + Math.min(Math.max(current.getCandidateRttNanos() * 2, minWindowTime), maxWindowTime);

if (isWindowReady(current)) {
delegate.onSample(startTime, current.getTrackedRttNanos(), current.getMaxInFlight(), current.didDrop());
}
}
} finally {
lock.unlock();
}
}
}
Expand Down
Loading