Skip to content

Commit

Permalink
The Http1xServerConnection synchronize itself to guard the internal s…
Browse files Browse the repository at this point in the history
…tate of the request/response in progress.

Such synchronization used to be useful before Vert.x 4 and the clear dispatch between connection/request code. Currently read and writes are confined in the event-loop thread and removing this synchronization has no effect on the read/writes of this state.
  • Loading branch information
vietj committed May 16, 2024
1 parent f8244af commit f37aa24
Showing 1 changed file with 19 additions and 38 deletions.
57 changes: 19 additions & 38 deletions src/main/java/io/vertx/core/http/impl/Http1xServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,7 @@ private void onContent(Object msg) {
return;
}
Buffer buffer = Buffer.buffer(VertxHandler.safeBuffer(content.content()));
Http1xServerRequest request;
synchronized (this) {
request = requestInProgress;
}
Http1xServerRequest request = requestInProgress;
request.context.execute(buffer, request::handleContent);
//TODO chunk trailers
if (content instanceof LastHttpContent) {
Expand All @@ -212,12 +209,9 @@ private void onContent(Object msg) {

private void onEnd() {
boolean close;
Http1xServerRequest request;
synchronized (this) {
request = requestInProgress;
requestInProgress = null;
close = !keepAlive && responseInProgress == null;
}
Http1xServerRequest request = requestInProgress;
requestInProgress = null;
close = !keepAlive && responseInProgress == null;
request.context.execute(request, Http1xServerRequest::handleEnd);
if (close) {
flushAndClose();
Expand Down Expand Up @@ -468,16 +462,14 @@ public void handleInterestedOpsChanged() {
writable = !isNotWritable();
ContextInternal context;
Handler<Boolean> handler;
synchronized (this) {
if (responseInProgress != null) {
context = responseInProgress.context;
handler = responseInProgress.response()::handleWritabilityChanged;
} else if (webSocket != null) {
context = webSocket.context;
handler = webSocket::handleWritabilityChanged;
} else {
return;
}
if (responseInProgress != null) {
context = responseInProgress.context;
handler = responseInProgress.response()::handleWritabilityChanged;
} else if (webSocket != null) {
context = webSocket.context;
handler = webSocket::handleWritabilityChanged;
} else {
return;
}
context.execute(writable, handler);
}
Expand All @@ -496,14 +488,9 @@ void write103EarlyHints(HttpHeaders headers, PromiseInternal<Void> promise) {
}

protected void handleClosed() {
Http1xServerRequest responseInProgress;
Http1xServerRequest requestInProgress;
ServerWebSocketImpl ws;
synchronized (this) {
ws = this.webSocket;
requestInProgress = this.requestInProgress;
responseInProgress = this.responseInProgress;
}
Http1xServerRequest responseInProgress = this.responseInProgress;
Http1xServerRequest requestInProgress = this.requestInProgress;
ServerWebSocketImpl webSocket = this.webSocket;
if (requestInProgress != null) {
requestInProgress.context.execute(v -> {
requestInProgress.handleException(HttpUtils.CONNECTION_CLOSED_EXCEPTION);
Expand All @@ -514,23 +501,17 @@ protected void handleClosed() {
responseInProgress.handleException(HttpUtils.CONNECTION_CLOSED_EXCEPTION);
});
}
if (ws != null) {
ws.context.execute(v -> ws.handleConnectionClosed());
if (webSocket != null) {
webSocket.context.execute(v -> webSocket.handleConnectionClosed());
}
super.handleClosed();
}

@Override
public void handleException(Throwable t) {
super.handleException(t);
Http1xServerRequest responseInProgress;
Http1xServerRequest requestInProgress;
synchronized (this) {
requestInProgress = this.requestInProgress;
responseInProgress = this.responseInProgress;
if (METRICS_ENABLED && metrics != null) {
requestFailed = true;
}
if (METRICS_ENABLED && metrics != null) {
requestFailed = true;
}
if (requestInProgress != null) {
requestInProgress.handleException(t);
Expand Down

0 comments on commit f37aa24

Please sign in to comment.