Skip to content

Commit

Permalink
⚡ Close the request channel before endExchange in AuthenticationCallH…
Browse files Browse the repository at this point in the history
…andler to prevent delays when handling requests with large data payloads
  • Loading branch information
ujibang committed Apr 4, 2023
1 parent 7c91adc commit 4072bbd
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import io.undertow.attribute.ExchangeAttributes;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
Expand Down Expand Up @@ -98,7 +100,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
CORSHandler.injectAccessControlAllowHeaders(exchange);
// set status code and end exchange
exchange.setStatusCode(HttpStatus.SC_TOO_MANY_REQUESTS);
exchange.endExchange();
fastEndExchange(exchange);
} else if (sc.authenticate() && (!sc.isAuthenticationRequired() || sc.isAuthenticated())) {
// 1 authentication is always attempted
// 2 requests fails if and only if authentication fails
Expand All @@ -116,8 +118,26 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
updateFailedAuthMetrics(exchange);
// set status code and end exchange
exchange.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
exchange.endExchange();
fastEndExchange(exchange);
}
}

/**
* shutdowns the request channel and ends the exchange
*
* Closing the request channel prevents delays when handling requests
* with large data payloads.
*
* @param exchange
* @throws IOException
*/
private void fastEndExchange(HttpServerExchange exchange) throws IOException {
var requestChannel = exchange.getRequestChannel();
if (requestChannel != null) {
requestChannel.shutdownReads();
}

exchange.endExchange();
}

/**
Expand Down

0 comments on commit 4072bbd

Please sign in to comment.