From 73bb10ae44237e7d665d3e473ae38887cf71e5a4 Mon Sep 17 00:00:00 2001 From: Wetitpig Date: Mon, 4 Dec 2023 15:23:47 +0800 Subject: [PATCH] closeHandler has to be registered before connection close Signed-off-by: Wetitpig --- .../api/handlers/MaxConnectionsHandler.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/handlers/MaxConnectionsHandler.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/handlers/MaxConnectionsHandler.java index 42c4158c4e95..b4298e4a42f9 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/handlers/MaxConnectionsHandler.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/handlers/MaxConnectionsHandler.java @@ -29,6 +29,14 @@ private MaxConnectionsHandler() {} public static Handler handler( final AtomicInteger activeConnectionsCount, final int maxActiveConnections) { return connection -> { + connection.closeHandler( + c -> + LOG.debug( + "Connection closed from {} to {}. Total of active connections: {}/{}", + connection.remoteAddress(), + connection.localAddress(), + activeConnectionsCount.decrementAndGet(), + maxActiveConnections)); if (activeConnectionsCount.get() >= maxActiveConnections) { // disallow new connections to prevent DoS LOG.warn( @@ -45,14 +53,6 @@ public static Handler handler( activeConnectionsCount.incrementAndGet(), maxActiveConnections); } - connection.closeHandler( - c -> - LOG.debug( - "Connection closed from {} to {}. Total of active connections: {}/{}", - connection.remoteAddress(), - connection.localAddress(), - activeConnectionsCount.decrementAndGet(), - maxActiveConnections)); }; } }