From c0e1eb545af41ee2b5580cc5525c028e0d5c19de Mon Sep 17 00:00:00 2001 From: Violeta Georgieva Date: Thu, 12 Dec 2024 10:36:32 +0200 Subject: [PATCH] Ensure the request counter is updated once when the request is received --- .../reactor/netty/channel/ChannelOperations.java | 3 ++- .../java/reactor/netty/http/HttpOperations.java | 5 ++++- .../reactor/netty/http/server/HttpServerTests.java | 14 ++++++++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/reactor-netty-core/src/main/java/reactor/netty/channel/ChannelOperations.java b/reactor-netty-core/src/main/java/reactor/netty/channel/ChannelOperations.java index 8ae1cfca3..9603508be 100644 --- a/reactor-netty-core/src/main/java/reactor/netty/channel/ChannelOperations.java +++ b/reactor-netty-core/src/main/java/reactor/netty/channel/ChannelOperations.java @@ -605,8 +605,9 @@ public Context currentContext() { @Override public String asShortText() { + String shortId = this.shortId; if (shortId == null) { - shortId = initShortId(); + this.shortId = shortId = initShortId(); } return shortId; diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/HttpOperations.java b/reactor-netty-http/src/main/java/reactor/netty/http/HttpOperations.java index 0df049088..8e09dc72c 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/HttpOperations.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/HttpOperations.java @@ -89,6 +89,9 @@ protected HttpOperations(HttpOperations replaced) { protected HttpOperations(Connection connection, ConnectionObserver listener, HttpMessageLogFactory httpMessageLogFactory) { super(connection, listener); this.httpMessageLogFactory = httpMessageLogFactory; + if (connection instanceof AtomicLong) { + ((AtomicLong) connection).incrementAndGet(); + } } /** @@ -372,7 +375,7 @@ protected final boolean markSentHeaderAndBody(Object... objectsToRelease) { protected final String initShortId() { Connection connection = connection(); if (connection instanceof AtomicLong) { - return connection.channel().id().asShortText() + '-' + ((AtomicLong) connection).incrementAndGet(); + return connection.channel().id().asShortText() + '-' + ((AtomicLong) connection).get(); } return super.initShortId(); } diff --git a/reactor-netty-http/src/test/java/reactor/netty/http/server/HttpServerTests.java b/reactor-netty-http/src/test/java/reactor/netty/http/server/HttpServerTests.java index 8e1a47975..666e51aa5 100644 --- a/reactor-netty-http/src/test/java/reactor/netty/http/server/HttpServerTests.java +++ b/reactor-netty-http/src/test/java/reactor/netty/http/server/HttpServerTests.java @@ -43,6 +43,7 @@ import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiFunction; import java.util.function.Function; @@ -3129,7 +3130,12 @@ void testRemoveRoutes() { @ParameterizedTest @ValueSource(ints = {-1, 1, 2}) void testMaxKeepAliveRequests(int maxKeepAliveRequests) { - HttpServer server = createServer().handle((req, res) -> res.sendString(Mono.just("testMaxKeepAliveRequests"))); + AtomicLong requestId = new AtomicLong(); + HttpServer server = createServer().handle((req, res) -> { + String id = req.requestId(); + requestId.set(Integer.parseInt(id.substring(id.lastIndexOf('-') + 1))); + return res.sendString(Mono.just("testMaxKeepAliveRequests")); + }); assertThat(server.configuration().maxKeepAliveRequests()).isEqualTo(-1); server = server.maxKeepAliveRequests(maxKeepAliveRequests); @@ -3154,17 +3160,17 @@ void testMaxKeepAliveRequests(int maxKeepAliveRequests) { "testMaxKeepAliveRequests".equals(l.get(1).getT1()); if (maxKeepAliveRequests == -1) { - return result && + return result && requestId.get() == 2 && "persistent".equals(l.get(0).getT2()) && "persistent".equals(l.get(1).getT2()); } else if (maxKeepAliveRequests == 1) { - return result && + return result && requestId.get() == 1 && "close".equals(l.get(0).getT2()) && "close".equals(l.get(1).getT2()); } else if (maxKeepAliveRequests == 2) { - return result && + return result && requestId.get() == 2 && "persistent".equals(l.get(0).getT2()) && "close".equals(l.get(1).getT2()); }