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

WebSockets Next: improve the concurrency limiter #40619

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
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 @@ -25,7 +25,8 @@ class ConcurrencyLimiter {
ConcurrencyLimiter(WebSocketConnectionBase connection) {
this.connection = connection;
this.uncompleted = new AtomicLong();
this.queueCounter = new AtomicLong();
// Counter is only used for debugging
this.queueCounter = LOG.isDebugEnabled() ? new AtomicLong() : null;
this.queue = Queues.createMpscQueue();
}

Expand All @@ -51,7 +52,7 @@ void run(Context context, Runnable action) {
LOG.debugf("Run action: %s", connection);
action.run();
} else {
long queueIndex = queueCounter.incrementAndGet();
long queueIndex = queueCounter != null ? queueCounter.incrementAndGet() : 0l;
LOG.debugf("Action queued as %s: %s", queueIndex, connection);
queue.offer(new Action(queueIndex, action, context));
// We need to make sure that at least one completion is in flight
Expand All @@ -76,38 +77,31 @@ void failure(Throwable t) {
try {
promise.fail(t);
} finally {
if (uncompleted.decrementAndGet() == 0) {
return;
}
Action queuedAction = queue.poll();
assert queuedAction != null;
LOG.debugf("Run action %s from queue: %s", queuedAction.queueIndex, connection);
queuedAction.context.runOnContext(new Handler<Void>() {
@Override
public void handle(Void event) {
queuedAction.runnable.run();
}
});
tryNext();
}
}

void complete() {
try {
promise.complete();
} finally {
if (uncompleted.decrementAndGet() == 0) {
return;
}
Action queuedAction = queue.poll();
assert queuedAction != null;
LOG.debugf("Run action %s from queue: %s", queuedAction.queueIndex, connection);
queuedAction.context.runOnContext(new Handler<Void>() {
@Override
public void handle(Void event) {
queuedAction.runnable.run();
}
});
tryNext();
}
}

private void tryNext() {
if (uncompleted.decrementAndGet() == 0) {
return;
}
Action queuedAction = queue.poll();
assert queuedAction != null;
LOG.debugf("Run action %s from queue: %s", queuedAction.queueIndex, connection);
queuedAction.context.runOnContext(new Handler<Void>() {
@Override
public void handle(Void event) {
queuedAction.runnable.run();
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public Uni<Void> close() {
}

public Uni<Void> close(CloseReason reason) {
if (isClosed()) {
LOG.warnf("Connection already closed: %s", this);
return Uni.createFrom().voidItem();
}
return UniHelper.toUni(webSocket().close((short) reason.getCode(), reason.getMessage()));
}

Expand Down