diff --git a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/TargetHandler.java b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/TargetHandler.java index ca951dff35..859a43988b 100644 --- a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/TargetHandler.java +++ b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/TargetHandler.java @@ -770,7 +770,7 @@ public void closed(NHttpClientConnection conn) { metrics.disconnected(); TargetContext.updateState(conn, ProtocolState.CLOSED); - targetConfiguration.getConnections().shutdownConnection(conn, isFault); + targetConfiguration.getConnections().closeConnection(conn, isFault); } @@ -871,7 +871,7 @@ public void timeout(NHttpClientConnection conn) { } TargetContext.updateState(conn, ProtocolState.CLOSED); - targetConfiguration.getConnections().shutdownConnection(conn, true); + targetConfiguration.getConnections().closeConnection(conn, true); } private boolean isResponseHaveBodyExpected( diff --git a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/connections/TargetConnections.java b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/connections/TargetConnections.java index e69b569602..773b711874 100644 --- a/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/connections/TargetConnections.java +++ b/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/passthru/connections/TargetConnections.java @@ -177,6 +177,33 @@ public void shutdownConnection(NHttpClientConnection conn, boolean isError) { } } + /** + * Close a connection gracefully. + * + * @param conn the connection that needs to be closed. + * @param isError whether an error is causing the close of the connection. + * When an error is causing a close of a connection we should + * not release the associated buffers into the pool. + */ + public void closeConnection(NHttpClientConnection conn, boolean isError) { + HostConnections pool = (HostConnections) conn.getContext().getAttribute( + PassThroughConstants.CONNECTION_POOL); + + TargetContext.get(conn).reset(isError); + + if (pool != null) { + pool.forget(conn); + } else { + // we shouldn't get here + log.fatal("Connection without a pool. Something wrong. Need to fix."); + } + + try { + conn.close(); + } catch (IOException ignored) { + } + } + /** * Release an active connection to the pool *