hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1054988 - in /httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio: conn/ClientConnAdaptor.java conn/HttpPoolEntry.java conn/HttpSessionPool.java conn/PoolingClientConnectionManager.java pool/PoolEntry.java
Date Tue, 04 Jan 2011 11:55:47 GMT
Author: olegk
Date: Tue Jan  4 11:55:47 2011
New Revision: 1054988

URL: http://svn.apache.org/viewvc?rev=1054988&view=rev
Log:
Improved connection keep-alive code

Modified:
    httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/ClientConnAdaptor.java
    httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/HttpPoolEntry.java
    httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/HttpSessionPool.java
    httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/PoolingClientConnectionManager.java
    httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/pool/PoolEntry.java

Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/ClientConnAdaptor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/ClientConnAdaptor.java?rev=1054988&r1=1054987&r2=1054988&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/ClientConnAdaptor.java
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/ClientConnAdaptor.java
Tue Jan  4 11:55:47 2011
@@ -368,7 +368,7 @@ class ClientConnAdaptor implements Manag
         if (tunit == null) {
             throw new IllegalArgumentException("Time unit may not be null");
         }
-        this.entry.setExpiry(tunit.toMillis(duration));
+        this.entry.updateExpiry(duration, tunit);
     }
 
     @Override

Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/HttpPoolEntry.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/HttpPoolEntry.java?rev=1054988&r1=1054987&r2=1054988&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/HttpPoolEntry.java
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/HttpPoolEntry.java
Tue Jan  4 11:55:47 2011
@@ -26,6 +26,9 @@
  */
 package org.apache.http.impl.nio.conn;
 
+import java.util.Date;
+import java.util.concurrent.TimeUnit;
+
 import org.apache.commons.logging.Log;
 import org.apache.http.conn.routing.HttpRoute;
 import org.apache.http.conn.routing.RouteTracker;
@@ -37,8 +40,9 @@ class HttpPoolEntry extends PoolEntry<Ht
     private final Log log;
     private final RouteTracker tracker;
 
-    HttpPoolEntry(final Log log, final HttpRoute route, final IOSession session) {
-        super(route, session);
+    HttpPoolEntry(final Log log, final HttpRoute route, final IOSession session,
+            final long timeToLive, final TimeUnit tunit) {
+        super(route, session, timeToLive, tunit);
         this.log = log;
         this.tracker = new RouteTracker(route);
     }
@@ -74,7 +78,7 @@ class HttpPoolEntry extends PoolEntry<Ht
     public boolean isExpired(long now) {
         boolean expired = super.isExpired(now);
         if (expired && this.log.isDebugEnabled()) {
-            this.log.debug("Connection expired: " + this);
+            this.log.debug("Connection " + this + " expired @ " + new Date(getExpiry()));
         }
         return expired;
     }

Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/HttpSessionPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/HttpSessionPool.java?rev=1054988&r1=1054987&r2=1054988&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/HttpSessionPool.java
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/HttpSessionPool.java
Tue Jan  4 11:55:47 2011
@@ -49,7 +49,7 @@ class HttpSessionPool extends SessionPoo
             final SchemeRegistry schemeRegistry,
             long timeToLive, final TimeUnit tunit) {
         super(ioreactor,
-                new InternalEntryFactory(log, tunit.toMillis(timeToLive)),
+                new InternalEntryFactory(log, timeToLive, tunit),
                 new InternalRouteResolver(schemeRegistry),
                 20, 50);
     }
@@ -87,22 +87,17 @@ class HttpSessionPool extends SessionPoo
 
         private final Log log;
         private final long connTimeToLive;
+        private final TimeUnit tunit;
 
-        InternalEntryFactory(final Log log, final long connTimeToLive) {
+        InternalEntryFactory(final Log log, final long connTimeToLive, final TimeUnit tunit)
{
             super();
             this.log = log;
             this.connTimeToLive = connTimeToLive;
+            this.tunit = tunit;
         }
 
         public HttpPoolEntry createEntry(final HttpRoute route, final IOSession session)
{
-            HttpPoolEntry entry = new HttpPoolEntry(this.log, route, session);
-            long now = System.currentTimeMillis();
-            entry.setCreated(now);
-            entry.setUpdated(now);
-            if (this.connTimeToLive > 0) {
-                entry.setDeadline(now + this.connTimeToLive);
-            }
-            return entry;
+            return new HttpPoolEntry(this.log, route, session, this.connTimeToLive, this.tunit);
         }
 
     };

Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/PoolingClientConnectionManager.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/PoolingClientConnectionManager.java?rev=1054988&r1=1054987&r2=1054988&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/PoolingClientConnectionManager.java
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/conn/PoolingClientConnectionManager.java
Tue Jan  4 11:55:47 2011
@@ -129,7 +129,7 @@ public class PoolingClientConnectionMana
         ClientConnAdaptor adaptor = (ClientConnAdaptor) conn;
         ClientConnectionManager manager = adaptor.getManager();
         if (manager != null && manager != this) {
-            throw new IllegalArgumentException("connection not obtained from this manager");
+            throw new IllegalArgumentException("Connection not obtained from this manager");
         }
         HttpPoolEntry entry = adaptor.getEntry();
         if (this.log.isDebugEnabled()) {
@@ -144,19 +144,18 @@ public class PoolingClientConnectionMana
 
         boolean reusable = adaptor.isReusable();
         if (reusable) {
-            entry.setExpiry(tunit.toMillis(validDuration));
+            entry.updateExpiry(validDuration, tunit);
             if (this.log.isDebugEnabled()) {
-                entry.setExpiry(tunit.toMillis(validDuration));
                 String s;
-                if (validDuration >= 0) {
-                    s = validDuration + " " + tunit;
+                if (validDuration > 0) {
+                    s = "for " + validDuration + " " + tunit;
                 } else {
-                    s = "ever";
+                    s = "indefinitely";
                 }
                 HttpRoute route = entry.getPlannedRoute();
                 Object state = entry.getState();
                 this.log.debug("Pooling connection" +
-                        " [" + route + "][" + state + "]; keep alive for " + s);
+                        " [" + route + "][" + state + "]; keep alive " + s);
             }
         }
         this.pool.release(entry, reusable);

Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/pool/PoolEntry.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/pool/PoolEntry.java?rev=1054988&r1=1054987&r2=1054988&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/pool/PoolEntry.java
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/pool/PoolEntry.java
Tue Jan  4 11:55:47 2011
@@ -26,6 +26,7 @@
  */
 package org.apache.http.impl.nio.pool;
 
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.http.nio.reactor.IOSession;
@@ -37,17 +38,34 @@ public abstract class PoolEntry<T> {
     private final long id;
     private final T route;
     private final IOSession session;
+    private final long created;
+    private final long validUnit;
+
     private Object state;
-    private long created;
     private long updated;
-    private long deadline;
     private long expiry;
 
-    public PoolEntry(final T route, final IOSession session) {
+    public PoolEntry(final T route, final IOSession session,
+            final long timeToLive, final TimeUnit tunit) {
         super();
+        if (route == null) {
+            throw new IllegalArgumentException("Route may not be null");
+        }
+        if (session == null) {
+            throw new IllegalArgumentException("I/O session may not be null");
+        }
+        if (tunit == null) {
+            throw new IllegalArgumentException("Time unit may not be null");
+        }
         this.route = route;
         this.session = session;
         this.id = COUNTER.incrementAndGet();
+        this.created = System.currentTimeMillis();
+        if (timeToLive > 0) {
+            this.validUnit = this.created + tunit.toMillis(timeToLive);
+        } else {
+            this.validUnit = Long.MAX_VALUE;
+        }
     }
 
     protected T getRoute() {
@@ -58,54 +76,46 @@ public abstract class PoolEntry<T> {
         return this.session;
     }
 
-    protected Object getState() {
-        return this.state;
-    }
-
-    protected void setState(final Object state) {
-        this.state = state;
-    }
-
     public long getCreated() {
         return this.created;
     }
 
-    public void setCreated(long created) {
-        this.created = created;
+    public long getValidUnit() {
+        return this.validUnit;
     }
 
-    public long getUpdated() {
-        return this.updated;
-    }
-
-    public void setUpdated(long updated) {
-        this.updated = updated;
+    protected Object getState() {
+        return this.state;
     }
 
-    public long getDeadline() {
-        return this.deadline;
+    protected void setState(final Object state) {
+        this.state = state;
     }
 
-    public void setDeadline(long deadline) {
-        this.deadline = deadline;
+    public long getUpdated() {
+        return this.updated;
     }
 
     public long getExpiry() {
         return this.expiry;
     }
 
-    public void setExpiry(long expiry) {
-        this.expiry = expiry;
-    }
-
-    public boolean isExpired(long now) {
-        if (this.deadline > 0 && this.deadline < now) {
-            return true;
+    public void updateExpiry(final long time, final TimeUnit tunit) {
+        if (tunit == null) {
+            throw new IllegalArgumentException("Time unit may not be null");
         }
-        if (this.expiry > 0 && this.expiry < now) {
-            return true;
+        this.updated = System.currentTimeMillis();
+        long newExpiry;
+        if (time > 0) {
+            newExpiry = this.updated + tunit.toMillis(time);
+        } else {
+            newExpiry = Long.MAX_VALUE;
         }
-        return false;
+        this.expiry = Math.min(newExpiry, this.validUnit);
+    }
+
+    public boolean isExpired(final long now) {
+        return now >= this.expiry;
     }
 
     @Override



Mime
View raw message