hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rola...@apache.org
Subject svn commit: r607424 - in /httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm: ConnPoolByRoute.java WaitingThread.java
Date Sat, 29 Dec 2007 15:02:49 GMT
Author: rolandw
Date: Sat Dec 29 07:02:49 2007
New Revision: 607424

URL: http://svn.apache.org/viewvc?rev=607424&view=rev
Log:
HTTPCLIENT-725: use deadline instead of adjusted timeout interval

Modified:
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/WaitingThread.java

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java?rev=607424&r1=607423&r2=607424&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java
Sat Dec 29 07:02:49 2007
@@ -30,6 +30,7 @@
 
 package org.apache.http.impl.conn.tsccm;
 
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Queue;
@@ -171,19 +172,18 @@
         int maxTotalConnections = HttpConnectionManagerParams
             .getMaxTotalConnections(this.params);
         
-        BasicPoolEntry entry = null;
+        Date deadline = null;
+        if (timeout > 0) {
+            deadline = new Date(System.currentTimeMillis() + timeout);
+        }
 
+        BasicPoolEntry entry = null;
         try {
             poolLock.lock();
 
             RouteSpecificPool rospl = getRoutePool(route, true);
             WaitingThread waitingThread = null;
 
-            boolean useTimeout = (timeout > 0);
-            long timeToWait = timeout;
-            long startWait = 0;
-            long endWait = 0;
-
             while (entry == null) {
 
                 if (isShutDown) {
@@ -213,34 +213,28 @@
                     entry = createEntry(rospl, operator);
 
                 } else {
-                    // TODO: keep track of which routes have waiting threads,
-                    // so they avoid being sacrificed before necessary
+
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Need to wait for connection. " + route);
+                    }
+
+                    if (waitingThread == null) {
+                        //@@@ use factory method?
+                        waitingThread = new WaitingThread
+                            (poolLock.newCondition(), rospl);
+                    }
 
                     boolean success = false;
                     try {
-                        if (useTimeout && timeToWait <= 0) {
-                            throw new ConnectionPoolTimeoutException
-                                ("Timeout waiting for connection");
-                        }
-   
-                        if (LOG.isDebugEnabled()) {
-                            LOG.debug("Need to wait for connection. " + route);
-                        }
-   
-                        if (waitingThread == null) {
-                            //@@@ use factory method?
-                            waitingThread = new WaitingThread
-                                (poolLock.newCondition(), rospl);
-                        }
-
-                        if (useTimeout) {
-                            startWait = System.currentTimeMillis();
-                        }
-
                         rospl.queueThread(waitingThread);
                         waitingThreads.add(waitingThread);
-                        success = waitingThread.await(timeToWait); //@@@, TimeUnit.MILLISECONDS);
or deadline
+                        success = waitingThread.await(deadline);
+
                     } finally {
+                        // In case of 'success', we were woken up by the
+                        // connection pool and should now have a connection
+                        // waiting for us, or else we're shutting down.
+                        // Just continue in the loop, both cases are checked.
                         if (!success) {
                             // Either we timed out, experienced a
                             // "spurious wakeup", or were interrupted by
@@ -249,15 +243,13 @@
                             rospl.removeThread(waitingThread);
                             waitingThreads.remove(waitingThread);
                         }
-                        // In case of 'success', we were woken up by the
-                        // connection pool and should now have a connection
-                        // waiting for us, or else we're shutting down.
-                        // Just continue in the loop, both cases are checked.
+                    }
 
-                        if (useTimeout) {
-                            endWait = System.currentTimeMillis();
-                            timeToWait -= (endWait - startWait);
-                        }
+                    // check for spurious wakeup vs. timeout
+                    if (!success && (deadline != null) &&
+                        (deadline.getTime() <= System.currentTimeMillis())) {
+                        throw new ConnectionPoolTimeoutException
+                            ("Timeout waiting for connection");
                     }
                 }
             } // while no entry

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/WaitingThread.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/WaitingThread.java?rev=607424&r1=607423&r2=607424&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/WaitingThread.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/WaitingThread.java
Sat Dec 29 07:02:49 2007
@@ -31,6 +31,7 @@
 package org.apache.http.impl.conn.tsccm;
 
 
+import java.util.Date;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.locks.Condition;
 
@@ -79,31 +80,62 @@
 
 
     /**
+     * Obtains the condition.
+     *
+     * @return  the condition on which to wait, never <code>null</code>
+     */
+    public final Condition getCondition() {
+        // not synchronized
+        return this.cond;
+    }
+
+
+    /**
+     * Obtains the pool, if there is one.
+     *
+     * @return  the pool on which a thread is or was waiting,
+     *          or <code>null</code>
+     */
+    public final RouteSpecificPool getPool() {
+        // not synchronized
+        return this.pool;
+    }
+
+
+    /**
+     * Obtains the thread, if there is one.
+     *
+     * @return  the thread which is waiting, or <code>null</code>
+     */
+    public final Thread getThread() {
+        // not synchronized
+        return this.waiter;
+    }
+
+
+    /**
      * Blocks the calling thread.
      * This method returns when the thread is notified or interrupted,
      * if a timeout occurrs, or if there is a spurious wakeup.
      * <br/>
      * This method assumes external synchronization.
      *
-     * @param timeout   the timeout in milliseconds, or 0 for no timeout
+     * @param deadline  when to time out, or <code>null</code> for no timeout
      *
      * @return  <code>true</code> if the condition was satisfied,
      *          <code>false</code> in case of a timeout.
      *          Typically, a call to {@link #wakeup} is used to indicate
-     *          that the condition was satisfied. Since the condition can
-     *          be accessed from outside, this cannot be guaranteed though.
+     *          that the condition was satisfied. Since the condition is
+     *          accessible outside, this cannot be guaranteed though.
      *
      * @throws InterruptedException     if the waiting thread was interrupted
      *
      * @see #wakeup
      */
-    public boolean await(long timeout)
+    public boolean await(Date deadline)
         throws InterruptedException {
 
-        //@@@ check timeout for negative, or assume overflow?
-        //@@@ for now, leave the check to the condition
-
-        // This is only a sanity check. We cannot not synchronize here,
+        // This is only a sanity check. We cannot synchronize here,
         // the lock would not be released on calling cond.await() below.
         if (this.waiter != null) {
             throw new IllegalStateException
@@ -116,7 +148,12 @@
 
         boolean success = false;
         try {
-            success = this.cond.await(timeout, TimeUnit.MILLISECONDS);
+            if (deadline != null) {
+                success = this.cond.awaitUntil(deadline);
+            } else {
+                this.cond.await();
+                success = true;
+            }
         } finally {
             this.waiter = null;
         }
@@ -142,40 +179,6 @@
         // One condition might be shared by several WaitingThread instances.
         // It probably isn't, but just in case: wake all, not just one.
         this.cond.signalAll();
-    }
-
-
-    /**
-     * Obtains the condition.
-     *
-     * @return  the condition on which to wait, never <code>null</code>
-     */
-    public final Condition getCondition() {
-        // not synchronized
-        return this.cond;
-    }
-
-
-    /**
-     * Obtains the pool, if there is one.
-     *
-     * @return  the pool on which a thread is or was waiting,
-     *          or <code>null</code>
-     */
-    public final RouteSpecificPool getPool() {
-        // not synchronized
-        return this.pool;
-    }
-
-
-    /**
-     * Obtains the thread, if there is one.
-     *
-     * @return  the thread which is waiting, or <code>null</code>
-     */
-    public final Thread getThread() {
-        // not synchronized
-        return this.waiter;
     }
 
 



Mime
View raw message