From d84b0fababc439d5f8ccad33edea68a62b8082f6 Mon Sep 17 00:00:00 2001 From: dhoard Date: Thu, 21 Mar 2024 00:15:28 -0400 Subject: [PATCH] Added try/finally to close the HttpExchange Signed-off-by: dhoard --- .../metrics/exporter/httpserver/DefaultHandler.java | 13 ++++++++----- .../metrics/exporter/httpserver/HealthyHandler.java | 13 ++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/DefaultHandler.java b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/DefaultHandler.java index e8a54e0cf..0e6342a34 100644 --- a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/DefaultHandler.java +++ b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/DefaultHandler.java @@ -58,10 +58,13 @@ public DefaultHandler() { @Override public void handle(HttpExchange exchange) throws IOException { - exchange.getResponseHeaders().set("Content-Type", contentType); - exchange.getResponseHeaders().set("Content-Length", Integer.toString(responseBytes.length)); - exchange.sendResponseHeaders(200, responseBytes.length); - exchange.getResponseBody().write(responseBytes); - exchange.close(); + try { + exchange.getResponseHeaders().set("Content-Type", contentType); + exchange.getResponseHeaders().set("Content-Length", Integer.toString(responseBytes.length)); + exchange.sendResponseHeaders(200, responseBytes.length); + exchange.getResponseBody().write(responseBytes); + } finally { + exchange.close(); + } } } diff --git a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HealthyHandler.java b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HealthyHandler.java index 03b647d99..4fbdb9426 100644 --- a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HealthyHandler.java +++ b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HealthyHandler.java @@ -22,10 +22,13 @@ public HealthyHandler() { @Override public void handle(HttpExchange exchange) throws IOException { - exchange.getResponseHeaders().set("Content-Type", contentType); - exchange.getResponseHeaders().set("Content-Length", Integer.toString(responseBytes.length)); - exchange.sendResponseHeaders(200, responseBytes.length); - exchange.getResponseBody().write(responseBytes); - exchange.close(); + try { + exchange.getResponseHeaders().set("Content-Type", contentType); + exchange.getResponseHeaders().set("Content-Length", Integer.toString(responseBytes.length)); + exchange.sendResponseHeaders(200, responseBytes.length); + exchange.getResponseBody().write(responseBytes); + } finally { + exchange.close(); + } } }