You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2019/09/06 08:16:33 UTC

[httpcomponents-core] 01/01: HTTPCORE-596: connection pools to reduce socket timeout to 1 second when closing managed connections

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch HTTPCORE-596
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit 3964b1254f0f18223ec5dfa6a7ce92e2af6fb24d
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Fri Sep 6 10:15:52 2019 +0200

    HTTPCORE-596: connection pools to reduce socket timeout to 1 second when closing managed connections
---
 .../org/apache/http/impl/nio/pool/BasicNIOPoolEntry.java    | 13 +++++++++++--
 .../main/java/org/apache/http/impl/pool/BasicPoolEntry.java | 13 +++++++++++--
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/httpcore-nio/src/main/java/org/apache/http/impl/nio/pool/BasicNIOPoolEntry.java b/httpcore-nio/src/main/java/org/apache/http/impl/nio/pool/BasicNIOPoolEntry.java
index a80a420..16e8c00 100644
--- a/httpcore-nio/src/main/java/org/apache/http/impl/nio/pool/BasicNIOPoolEntry.java
+++ b/httpcore-nio/src/main/java/org/apache/http/impl/nio/pool/BasicNIOPoolEntry.java
@@ -29,8 +29,8 @@ package org.apache.http.impl.nio.pool;
 import java.io.IOException;
 
 import org.apache.http.HttpHost;
-import org.apache.http.annotation.ThreadingBehavior;
 import org.apache.http.annotation.Contract;
+import org.apache.http.annotation.ThreadingBehavior;
 import org.apache.http.nio.NHttpClientConnection;
 import org.apache.http.pool.PoolEntry;
 
@@ -54,7 +54,16 @@ public class BasicNIOPoolEntry extends PoolEntry<HttpHost, NHttpClientConnection
     @Override
     public void close() {
         try {
-            getConnection().close();
+            final NHttpClientConnection connection = getConnection();
+            try {
+                final int socketTimeout = connection.getSocketTimeout();
+                if (socketTimeout <= 0 || socketTimeout > 1000) {
+                    connection.setSocketTimeout(1000);
+                }
+                connection.close();
+            } catch (final IOException ex) {
+                connection.shutdown();
+            }
         } catch (final IOException ignore) {
         }
     }
diff --git a/httpcore/src/main/java/org/apache/http/impl/pool/BasicPoolEntry.java b/httpcore/src/main/java/org/apache/http/impl/pool/BasicPoolEntry.java
index f31c6cf..80aca62 100644
--- a/httpcore/src/main/java/org/apache/http/impl/pool/BasicPoolEntry.java
+++ b/httpcore/src/main/java/org/apache/http/impl/pool/BasicPoolEntry.java
@@ -30,8 +30,8 @@ import java.io.IOException;
 
 import org.apache.http.HttpClientConnection;
 import org.apache.http.HttpHost;
-import org.apache.http.annotation.ThreadingBehavior;
 import org.apache.http.annotation.Contract;
+import org.apache.http.annotation.ThreadingBehavior;
 import org.apache.http.pool.PoolEntry;
 
 /**
@@ -52,7 +52,16 @@ public class BasicPoolEntry extends PoolEntry<HttpHost, HttpClientConnection> {
     @Override
     public void close() {
         try {
-            this.getConnection().close();
+            final HttpClientConnection connection = getConnection();
+            try {
+                final int socketTimeout = connection.getSocketTimeout();
+                if (socketTimeout <= 0 || socketTimeout > 1000) {
+                    connection.setSocketTimeout(1000);
+                }
+                connection.close();
+            } catch (final IOException ex) {
+                connection.shutdown();
+            }
         } catch (final IOException ignore) {
         }
     }