From 0d2f52a4b8d37a2432de427e12f4059dbbcbcb3e Mon Sep 17 00:00:00 2001 From: Pierre De Rop Date: Tue, 28 Jun 2022 08:15:09 +0200 Subject: [PATCH 1/2] Ensure active connections is decremented --- .../AbstractHttpServerMetricsHandler.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/AbstractHttpServerMetricsHandler.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/AbstractHttpServerMetricsHandler.java index 316f0dd033..c9123dbcff 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/AbstractHttpServerMetricsHandler.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/AbstractHttpServerMetricsHandler.java @@ -112,23 +112,23 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) if (msg instanceof LastHttpContent) { promise.addListener(future -> { - try { - ChannelOperations channelOps = ChannelOperations.get(ctx.channel()); - if (channelOps instanceof HttpServerOperations) { - HttpServerOperations ops = (HttpServerOperations) channelOps; + ChannelOperations channelOps = ChannelOperations.get(ctx.channel()); + if (channelOps instanceof HttpServerOperations) { + HttpServerOperations ops = (HttpServerOperations) channelOps; + try { recordWrite(ops, uriTagValue == null ? ops.path : uriTagValue.apply(ops.path), ops.method().name(), ops.status().codeAsText().toString()); - if (!ops.isHttp2() && ops.hostAddress() != null) { - // This metric is not applicable for HTTP/2 - // ops.hostAddress() == null when request decoding failed, in this case - // we do not report active connection, so we do not report inactive connection - recordInactiveConnection(ops); - } } - } - catch (RuntimeException e) { - log.warn("Exception caught while recording metrics.", e); - // Allow request-response exchange to continue, unaffected by metrics problem + catch (RuntimeException e) { + log.warn("Exception caught while recording metrics.", e); + // Allow request-response exchange to continue, unaffected by metrics problem + } + if (!ops.isHttp2() && ops.hostAddress() != null) { + // This metric is not applicable for HTTP/2 + // ops.hostAddress() == null when request decoding failed, in this case + // we do not report active connection, so we do not report inactive connection + recordInactiveConnection(ops); + } } dataSent = 0; From 10602aacf5e41f8d078947f36aaf42f4e9cecb9f Mon Sep 17 00:00:00 2001 From: Pierre De Rop Date: Tue, 28 Jun 2022 10:55:53 +0200 Subject: [PATCH 2/2] Catch exceptions from recordInactiveConnection callback --- .../http/server/AbstractHttpServerMetricsHandler.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/reactor-netty-http/src/main/java/reactor/netty/http/server/AbstractHttpServerMetricsHandler.java b/reactor-netty-http/src/main/java/reactor/netty/http/server/AbstractHttpServerMetricsHandler.java index c9123dbcff..43a328de1b 100644 --- a/reactor-netty-http/src/main/java/reactor/netty/http/server/AbstractHttpServerMetricsHandler.java +++ b/reactor-netty-http/src/main/java/reactor/netty/http/server/AbstractHttpServerMetricsHandler.java @@ -127,7 +127,13 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) // This metric is not applicable for HTTP/2 // ops.hostAddress() == null when request decoding failed, in this case // we do not report active connection, so we do not report inactive connection - recordInactiveConnection(ops); + try { + recordInactiveConnection(ops); + } + catch (RuntimeException e) { + log.warn("Exception caught while recording metrics.", e); + // Allow request-response exchange to continue, unaffected by metrics problem + } } }