You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ro...@apache.org on 2007/12/14 20:05:03 UTC

svn commit: r604259 - in /jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm: ConnPoolByRoute.java RouteSpecificPool.java

Author: rolandw
Date: Fri Dec 14 11:05:02 2007
New Revision: 604259

URL: http://svn.apache.org/viewvc?rev=604259&view=rev
Log:
cleanup and accessors in RouteSpecificPool

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

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java?rev=604259&r1=604258&r2=604259&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ConnPoolByRoute.java Fri Dec 14 11:05:02 2007
@@ -240,7 +240,7 @@
                         startWait = System.currentTimeMillis();
                     }
 
-                    rospl.waitingThreads.addLast(waitingThread);
+                    rospl.queueThread(waitingThread);
                     waitingThreads.add(waitingThread);
                     wait(timeToWait);
 
@@ -259,8 +259,8 @@
                         // Either we timed out, experienced a
                         // "spurious wakeup", or were interrupted by an
                         // external thread.  Regardless we need to 
-                            // cleanup for ourselves in the wait queue.
-                        rospl.waitingThreads.remove(waitingThread);
+                        // cleanup for ourselves in the wait queue.
+                        rospl.removeThread(waitingThread);
                         waitingThreads.remove(waitingThread);
                     }
 
@@ -455,12 +455,12 @@
         // it from all wait queues before interrupting.
         WaitingThread waitingThread = null;
 
-        if ((rospl != null) && !rospl.waitingThreads.isEmpty()) {
+        if ((rospl != null) && rospl.hasThread()) {
             if (LOG.isDebugEnabled()) {
                 LOG.debug("Notifying thread waiting on pool. "
                           + rospl.getRoute());
             }
-            waitingThread = (WaitingThread) rospl.waitingThreads.removeFirst();
+            waitingThread = rospl.dequeueThread();
             waitingThreads.remove(waitingThread);
 
         } else if (!waitingThreads.isEmpty()) {
@@ -468,7 +468,7 @@
                 LOG.debug("Notifying thread waiting on any pool.");
             }
             waitingThread = waitingThreads.remove();
-            waitingThread.pool.waitingThreads.remove(waitingThread);
+            waitingThread.pool.removeThread(waitingThread);
 
         } else if (LOG.isDebugEnabled()) {
             LOG.debug("Notifying no-one, there are no waiting threads");

Modified: jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RouteSpecificPool.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RouteSpecificPool.java?rev=604259&r1=604258&r2=604259&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RouteSpecificPool.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/RouteSpecificPool.java Fri Dec 14 11:05:02 2007
@@ -30,6 +30,7 @@
 
 package org.apache.http.impl.conn.tsccm;
 
+import java.util.Queue;
 import java.util.LinkedList;
 
 import org.apache.http.conn.HttpRoute;
@@ -41,31 +42,32 @@
  */
 public class RouteSpecificPool {
 
-    //@@@ change attribute visibility to protected once it is ensured
-    //@@@ that there is no direct attribute access within this package
-
     /** The route this pool is for. */
-    private final HttpRoute route;
+    protected final HttpRoute route;
 
-    /** The list of free entries. */
-    private LinkedList<BasicPoolEntry> freeEntries;
+    /**
+     * The list of free entries.
+     * This list is managed LIFO, to increase idle times and
+     * allow for closing connections that are not really needed.
+     */
+    protected LinkedList<BasicPoolEntry> freeEntries;
 
     /** The list of threads waiting for this pool. */
-    /*private@@@ currently still default*/ LinkedList<Object> waitingThreads;
+    protected Queue<ConnPoolByRoute.WaitingThread> waitingThreads;
 
     /** The number of created entries. */
-    private int numEntries;
+    protected int numEntries;
 
 
     /**
      * Creates a new route-specific pool.
      *
      * @param r     the route for which to pool
-         */
+     */
     public RouteSpecificPool(HttpRoute r) {
         this.route = r;
         this.freeEntries = new LinkedList<BasicPoolEntry>();
-        this.waitingThreads = new LinkedList<Object>();
+        this.waitingThreads = new LinkedList<ConnPoolByRoute.WaitingThread>();
         this.numEntries = 0;
     }
 
@@ -194,6 +196,57 @@
                 ("There is no entry that could be dropped.");
         }
         numEntries--;
+    }
+
+
+    /**
+     * Adds a waiting thread.
+     * This pool makes no attempt to match waiting threads with pool entries.
+     * It is the caller's responsibility to check that there is no entry
+     * before adding a waiting thread.
+     *
+     * @param wt        the waiting thread
+     */
+    public void queueThread(ConnPoolByRoute.WaitingThread wt) {
+        if (wt == null) {
+            throw new IllegalArgumentException
+                ("Waiting thread must not be null.");
+        }
+        this.waitingThreads.add(wt);
+    }
+
+
+    /**
+     * Checks whether there is a waiting thread in this pool.
+     *
+     * @return  <code>true</code> if there is a waiting thread,
+     *          <code>false</code> otherwise
+     */
+    public boolean hasThread() {
+        return !this.waitingThreads.isEmpty();
+    }
+
+
+    /**
+     * Obtains and removes a waiting thread.
+     *
+     * @return  a waiting thread, or <code>null</code> if there is none
+     */
+    public ConnPoolByRoute.WaitingThread dequeueThread() {
+        return this.waitingThreads.poll();
+    }
+
+
+    /**
+     * Removes a waiting thread, if it is queued.
+     *
+     * @param wt        the waiting thread
+     */
+    public void removeThread(ConnPoolByRoute.WaitingThread wt) {
+        if (wt == null)
+            return;
+
+        this.waitingThreads.remove(wt);
     }