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 2013/06/05 10:54:15 UTC
svn commit: r1489746 - in /httpcomponents/httpcore/trunk:
httpcore-nio/src/main/java/org/apache/http/nio/pool/
httpcore-nio/src/test/java/org/apache/http/nio/pool/
httpcore/src/main/java/org/apache/http/pool/
httpcore/src/test/java/org/apache/http/pool/
Author: olegk
Date: Wed Jun 5 08:54:15 2013
New Revision: 1489746
URL: http://svn.apache.org/r1489746
Log:
HTTPCORE-341: Added methods to enumerate all available and leased connections in connection pool
Added:
httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/PoolEntryCallback.java (with props)
Modified:
httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/AbstractNIOConnPool.java
httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java
httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/AbstractNIOConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/AbstractNIOConnPool.java?rev=1489746&r1=1489745&r2=1489746&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/AbstractNIOConnPool.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/AbstractNIOConnPool.java Wed Jun 5 08:54:15 2013
@@ -51,6 +51,7 @@ import org.apache.http.nio.reactor.Sessi
import org.apache.http.pool.ConnPool;
import org.apache.http.pool.ConnPoolControl;
import org.apache.http.pool.PoolEntry;
+import org.apache.http.pool.PoolEntryCallback;
import org.apache.http.pool.PoolStats;
import org.apache.http.util.Args;
import org.apache.http.util.Asserts;
@@ -595,6 +596,47 @@ public abstract class AbstractNIOConnPoo
}
}
+ /**
+ * Enumerates all available connections.
+ *
+ * @since 4.3
+ */
+ protected void enumAvailable(final PoolEntryCallback<T, C> callback) {
+ this.lock.lock();
+ try {
+ enumEntries(this.available.iterator(), callback);
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ /**
+ * Enumerates all leased connections.
+ *
+ * @since 4.3
+ */
+ protected void enumLeased(final PoolEntryCallback<T, C> callback) {
+ this.lock.lock();
+ try {
+ enumEntries(this.leased.iterator(), callback);
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ protected void enumEntries(final Iterator<E> it, final PoolEntryCallback<T, C> callback) {
+ while (it.hasNext()) {
+ final E entry = it.next();
+ callback.process(entry);
+ if (entry.isClosed()) {
+ final RouteSpecificPool<T, C, E> pool = getPool(entry.getRoute());
+ pool.remove(entry);
+ it.remove();
+ }
+ }
+ processPendingRequests();
+ }
+
public void closeIdle(final long idletime, final TimeUnit tunit) {
Args.notNull(tunit, "Time unit");
long time = tunit.toMillis(idletime);
@@ -602,42 +644,30 @@ public abstract class AbstractNIOConnPoo
time = 0;
}
final long deadline = System.currentTimeMillis() - time;
- this.lock.lock();
- try {
- final Iterator<E> it = this.available.iterator();
- while (it.hasNext()) {
- final E entry = it.next();
+ enumAvailable(new PoolEntryCallback<T, C>() {
+
+ @Override
+ public void process(final PoolEntry<T, C> entry) {
if (entry.getUpdated() <= deadline) {
entry.close();
- final RouteSpecificPool<T, C, E> pool = getPool(entry.getRoute());
- pool.remove(entry);
- it.remove();
}
}
- processPendingRequests();
- } finally {
- this.lock.unlock();
- }
+
+ });
}
public void closeExpired() {
final long now = System.currentTimeMillis();
- this.lock.lock();
- try {
- final Iterator<E> it = this.available.iterator();
- while (it.hasNext()) {
- final E entry = it.next();
+ enumAvailable(new PoolEntryCallback<T, C>() {
+
+ @Override
+ public void process(final PoolEntry<T, C> entry) {
if (entry.isExpired(now)) {
entry.close();
- final RouteSpecificPool<T, C, E> pool = getPool(entry.getRoute());
- pool.remove(entry);
- it.remove();
}
}
- processPendingRequests();
- } finally {
- this.lock.unlock();
- }
+
+ });
}
@Override
Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java?rev=1489746&r1=1489745&r2=1489746&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java (original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java Wed Jun 5 08:54:15 2013
@@ -50,18 +50,24 @@ public class TestNIOConnPool {
static class LocalPoolEntry extends PoolEntry<String, IOSession> {
+ private boolean closed;
+
public LocalPoolEntry(final String route, final IOSession conn) {
super(null, route, conn);
}
@Override
public void close() {
+ if (this.closed) {
+ return;
+ }
+ this.closed = true;
getConnection().close();
}
@Override
public boolean isClosed() {
- return getConnection().isClosed();
+ return this.closed;
}
}
Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java?rev=1489746&r1=1489745&r2=1489746&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/AbstractConnPool.java Wed Jun 5 08:54:15 2013
@@ -413,6 +413,46 @@ public abstract class AbstractConnPool<T
}
/**
+ * Enumerates all available connections.
+ *
+ * @since 4.3
+ */
+ protected void enumAvailable(final PoolEntryCallback<T, C> callback) {
+ this.lock.lock();
+ try {
+ enumEntries(this.available.iterator(), callback);
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ /**
+ * Enumerates all leased connections.
+ *
+ * @since 4.3
+ */
+ protected void enumLeased(final PoolEntryCallback<T, C> callback) {
+ this.lock.lock();
+ try {
+ enumEntries(this.leased.iterator(), callback);
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ private void enumEntries(final Iterator<E> it, final PoolEntryCallback<T, C> callback) {
+ while (it.hasNext()) {
+ final E entry = it.next();
+ callback.process(entry);
+ if (entry.isClosed()) {
+ final RouteSpecificPool<T, C, E> pool = getPool(entry.getRoute());
+ pool.remove(entry);
+ it.remove();
+ }
+ }
+ }
+
+ /**
* Closes connections that have been idle longer than the given period
* of time and evicts them from the pool.
*
@@ -426,22 +466,16 @@ public abstract class AbstractConnPool<T
time = 0;
}
final long deadline = System.currentTimeMillis() - time;
- this.lock.lock();
- try {
- final Iterator<E> it = this.available.iterator();
- while (it.hasNext()) {
- final E entry = it.next();
+ enumAvailable(new PoolEntryCallback<T, C>() {
+
+ @Override
+ public void process(final PoolEntry<T, C> entry) {
if (entry.getUpdated() <= deadline) {
entry.close();
- final RouteSpecificPool<T, C, E> pool = getPool(entry.getRoute());
- pool.remove(entry);
- it.remove();
- notifyPending(pool);
}
}
- } finally {
- this.lock.unlock();
- }
+
+ });
}
/**
@@ -449,22 +483,16 @@ public abstract class AbstractConnPool<T
*/
public void closeExpired() {
final long now = System.currentTimeMillis();
- this.lock.lock();
- try {
- final Iterator<E> it = this.available.iterator();
- while (it.hasNext()) {
- final E entry = it.next();
+ enumAvailable(new PoolEntryCallback<T, C>() {
+
+ @Override
+ public void process(final PoolEntry<T, C> entry) {
if (entry.isExpired(now)) {
entry.close();
- final RouteSpecificPool<T, C, E> pool = getPool(entry.getRoute());
- pool.remove(entry);
- it.remove();
- notifyPending(pool);
}
}
- } finally {
- this.lock.unlock();
- }
+
+ });
}
@Override
Added: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/PoolEntryCallback.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/PoolEntryCallback.java?rev=1489746&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/PoolEntryCallback.java (added)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/PoolEntryCallback.java Wed Jun 5 08:54:15 2013
@@ -0,0 +1,43 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.pool;
+
+import java.io.IOException;
+
+/**
+ * Pool entry callabck.
+ *
+ * @param <T> the route type that represents the opposite endpoint of a pooled
+ * connection.
+ * @param <C> the connection type.
+ * @since 4.3
+ */
+public interface PoolEntryCallback<T, C> {
+
+ void process(PoolEntry<T, C> entry);
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/PoolEntryCallback.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/PoolEntryCallback.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/pool/PoolEntryCallback.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java?rev=1489746&r1=1489745&r2=1489746&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/pool/TestConnPool.java Wed Jun 5 08:54:15 2013
@@ -46,12 +46,18 @@ public class TestConnPool {
static class LocalPoolEntry extends PoolEntry<String, HttpConnection> {
+ private boolean closed;
+
public LocalPoolEntry(final String route, final HttpConnection conn) {
super(null, route, conn);
}
@Override
public void close() {
+ if (this.closed) {
+ return;
+ }
+ this.closed = true;
try {
getConnection().close();
} catch (final IOException ignore) {
@@ -60,7 +66,7 @@ public class TestConnPool {
@Override
public boolean isClosed() {
- return !getConnection().isOpen();
+ return this.closed;
}
}