hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r547966 - /jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/
Date Sat, 16 Jun 2007 19:26:22 GMT
Author: olegk
Date: Sat Jun 16 12:26:21 2007
New Revision: 547966

URL: http://svn.apache.org/viewvc?view=rev&rev=547966
Log:
* Attempt to close connection gracefully whenever possible (when not dealing with a transport
I/O error)
* #closeConnection and #shutdownConnection methods now accept the Throwable that caused the
connection to be dropped as an optional input parameter. These methods can be overridden in
super classes to implement some custom connection shutdown logic.

Modified:
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpClientHandlerBase.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpHandlerBase.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpServiceHandlerBase.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpClientHandler.java
    jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpServiceHandler.java

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java?view=diff&rev=547966&r1=547965&r2=547966
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpClientHandler.java
Sat Jun 16 12:26:21 2007
@@ -163,12 +163,12 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
         } catch (HttpException ex) {
-            shutdownConnection(conn);
+            closeConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalProtocolException(ex, conn);
             }
@@ -192,12 +192,12 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
         } catch (HttpException ex) {
-            shutdownConnection(conn);
+            closeConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalProtocolException(ex, conn);
             }
@@ -225,7 +225,7 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
@@ -266,12 +266,12 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
         } catch (HttpException ex) {
-            shutdownConnection(conn);
+            closeConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalProtocolException(ex, conn);
             }
@@ -290,13 +290,13 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
         }
         
-        shutdownConnection(conn);
+        closeConnection(conn, null);
         if (this.eventListener != null) {
             this.eventListener.connectionTimeout(conn);
         }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java?view=diff&rev=547966&r1=547965&r2=547966
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/BufferingHttpServiceHandler.java
Sat Jun 16 12:26:21 2007
@@ -172,12 +172,12 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
         } catch (HttpException ex) {
-            shutdownConnection(conn);
+            closeConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalProtocolException(ex, conn);
             }
@@ -202,12 +202,12 @@
             sendResponse(conn, response);
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
         } catch (HttpException ex) {
-            shutdownConnection(conn);
+            closeConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalProtocolException(ex, conn);
             }
@@ -242,12 +242,12 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
         } catch (HttpException ex) {
-            shutdownConnection(conn);
+            closeConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalProtocolException(ex, conn);
             }
@@ -282,7 +282,7 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpClientHandlerBase.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpClientHandlerBase.java?view=diff&rev=547966&r1=547965&r2=547966
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpClientHandlerBase.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpClientHandlerBase.java
Sat Jun 16 12:26:21 2007
@@ -66,14 +66,14 @@
     }
 
     public void exception(final NHttpClientConnection conn, final HttpException ex) {
-        shutdownConnection(conn);
+        closeConnection(conn, ex);
         if (this.eventListener != null) {
             this.eventListener.fatalProtocolException(ex, conn);
         }
     }
 
     public void exception(final NHttpClientConnection conn, final IOException ex) {
-        shutdownConnection(conn);
+        shutdownConnection(conn, ex);
         if (this.eventListener != null) {
             this.eventListener.fatalIOException(ex, conn);
         }

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpHandlerBase.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpHandlerBase.java?view=diff&rev=547966&r1=547965&r2=547966
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpHandlerBase.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpHandlerBase.java
Sat Jun 16 12:26:21 2007
@@ -85,7 +85,20 @@
         this.eventListener = eventListener;
     }
 
-    protected void shutdownConnection(final HttpConnection conn) {
+    protected void closeConnection(final HttpConnection conn, final Throwable cause) {
+        try {
+            // Try to close it nicely
+            conn.close();
+        } catch (IOException ex) {
+            try {
+                // Just shut the damn thing down
+                conn.shutdown();
+            } catch (IOException ignore) {
+            }
+        }
+    }
+    
+    protected void shutdownConnection(final HttpConnection conn, final Throwable cause) {
         try {
             conn.shutdown();
         } catch (IOException ignore) {

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpServiceHandlerBase.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpServiceHandlerBase.java?view=diff&rev=547966&r1=547965&r2=547966
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpServiceHandlerBase.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/NHttpServiceHandlerBase.java
Sat Jun 16 12:26:21 2007
@@ -83,7 +83,7 @@
     }
 
     public void exception(final NHttpServerConnection conn, final IOException ex) {
-        shutdownConnection(conn);
+        shutdownConnection(conn, ex);
         
         if (this.eventListener != null) {
             this.eventListener.fatalIOException(ex, conn);
@@ -91,7 +91,7 @@
     }
 
     public void timeout(final NHttpServerConnection conn) {
-        shutdownConnection(conn);
+        closeConnection(conn, null);
 
         if (this.eventListener != null) {
             this.eventListener.connectionTimeout(conn);

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpClientHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpClientHandler.java?view=diff&rev=547966&r1=547965&r2=547966
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpClientHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpClientHandler.java
Sat Jun 16 12:26:21 2007
@@ -49,6 +49,7 @@
 import org.apache.http.nio.ContentEncoder;
 import org.apache.http.nio.IOControl;
 import org.apache.http.nio.NHttpClientConnection;
+import org.apache.http.nio.NHttpConnection;
 import org.apache.http.nio.entity.ContentBufferEntity;
 import org.apache.http.nio.entity.ContentOutputStream;
 import org.apache.http.nio.params.HttpNIOParams;
@@ -193,12 +194,12 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
         } catch (HttpException ex) {
-            shutdownConnection(conn);
+            closeConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalProtocolException(ex, conn);
             }
@@ -229,7 +230,7 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
@@ -294,12 +295,12 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
         } catch (HttpException ex) {
-            shutdownConnection(conn);
+            closeConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalProtocolException(ex, conn);
             }
@@ -331,7 +332,7 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
@@ -354,13 +355,13 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
         }
         
-        shutdownConnection(conn);
+        closeConnection(conn, null);
         if (this.eventListener != null) {
             this.eventListener.connectionTimeout(conn);
         }
@@ -410,7 +411,7 @@
                         outstream.close();
                         
                     } catch (IOException ex) {
-                        shutdownConnection(conn);
+                        shutdownConnection(conn, ex);
                         if (eventListener != null) {
                             eventListener.fatalIOException(ex, conn);
                         }
@@ -458,7 +459,7 @@
                     }
                     
                 } catch (IOException ex) {
-                    shutdownConnection(conn);
+                    shutdownConnection(conn, ex);
                     if (eventListener != null) {
                         eventListener.fatalIOException(ex, conn);
                     }
@@ -467,6 +468,18 @@
             
         });
         
+    }
+    
+    protected void shutdownConnection(final NHttpConnection conn, final Throwable cause)
{
+        HttpContext context = conn.getContext();
+
+        ClientConnState connState = (ClientConnState) context.getAttribute(CONN_STATE);
+        
+        super.shutdownConnection(conn, cause);
+        
+        if (connState != null) {
+            connState.shutdown();
+        }
     }
     
     static class ClientConnState {

Modified: jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpServiceHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpServiceHandler.java?view=diff&rev=547966&r1=547965&r2=547966
==============================================================================
--- jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpServiceHandler.java
(original)
+++ jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/protocol/ThrottlingHttpServiceHandler.java
Sat Jun 16 12:26:21 2007
@@ -127,12 +127,6 @@
     }
 
     public void closed(final NHttpServerConnection conn) {
-        HttpContext context = conn.getContext();
-        
-        ServerConnState connState = (ServerConnState) context.getAttribute(CONN_STATE);
-
-        connState.shutdown();
-        
         if (this.eventListener != null) {
             this.eventListener.connectionClosed(conn);
         }
@@ -162,17 +156,16 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (eventListener != null) {
                 eventListener.fatalIOException(ex, conn);
             }
         } catch (HttpException ex) {
-            shutdownConnection(conn);
+            closeConnection(conn, ex);
             if (eventListener != null) {
                 eventListener.fatalProtocolException(ex, conn);
             }
         }
-
     }
 
     public void requestReceived(final NHttpServerConnection conn) {
@@ -205,12 +198,12 @@
                         handleRequest(connState, conn);
                         
                     } catch (IOException ex) {
-                        shutdownConnection(conn);
+                        shutdownConnection(conn, ex);
                         if (eventListener != null) {
                             eventListener.fatalIOException(ex, conn);
                         }
                     } catch (HttpException ex) {
-                        shutdownConnection(conn);
+                        shutdownConnection(conn, ex);
                         if (eventListener != null) {
                             eventListener.fatalProtocolException(ex, conn);
                         }
@@ -245,7 +238,7 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
@@ -289,12 +282,12 @@
             }
 
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (eventListener != null) {
                 eventListener.fatalIOException(ex, conn);
             }
         } catch (HttpException ex) {
-            shutdownConnection(conn);
+            closeConnection(conn, ex);
             if (eventListener != null) {
                 eventListener.fatalProtocolException(ex, conn);
             }
@@ -331,27 +324,13 @@
             }
             
         } catch (IOException ex) {
-            shutdownConnection(conn);
+            shutdownConnection(conn, ex);
             if (this.eventListener != null) {
                 this.eventListener.fatalIOException(ex, conn);
             }
         }
     }
  
-    private void shutdownConnection(final NHttpConnection conn) {
-        HttpContext context = conn.getContext();
-
-        ServerConnState connState = (ServerConnState) context.getAttribute(CONN_STATE);
-        
-        try {
-            conn.shutdown();
-        } catch (IOException ignore) {
-        }
-        if (connState != null) {
-            connState.shutdown();
-        }
-    }
-    
     private void waitForOutputState(
             final ServerConnState connState, 
             int expectedState) throws InterruptedIOException {
@@ -509,6 +488,18 @@
             entity.writeTo(outstream);
             outstream.flush();
             outstream.close();
+        }
+    }
+    
+    protected void shutdownConnection(final NHttpConnection conn, final Throwable cause)
{
+        HttpContext context = conn.getContext();
+
+        ServerConnState connState = (ServerConnState) context.getAttribute(CONN_STATE);
+        
+        super.shutdownConnection(conn, cause);
+        
+        if (connState != null) {
+            connState.shutdown();
         }
     }
     



Mime
View raw message