import FIREFOX_52_6_0esr_RELEASE from mozilla-esr52 hg repo

This commit is contained in:
Roy Tam 2018-01-19 03:59:58 +08:00
commit dcd9973243
150858 changed files with 23884658 additions and 0 deletions

View file

@ -0,0 +1,533 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib.pool;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
import ch.boye.httpclientandroidlib.concurrent.FutureCallback;
import ch.boye.httpclientandroidlib.util.Args;
import ch.boye.httpclientandroidlib.util.Asserts;
/**
* Abstract synchronous (blocking) pool of connections.
* <p/>
* Please note that this class does not maintain its own pool of execution {@link Thread}s.
* Therefore, one <b>must</b> call {@link Future#get()} or {@link Future#get(long, TimeUnit)}
* method on the {@link Future} object returned by the
* {@link #lease(Object, Object, FutureCallback)} method in order for the lease operation
* to complete.
*
* @param <T> the route type that represents the opposite endpoint of a pooled
* connection.
* @param <C> the connection type.
* @param <E> the type of the pool entry containing a pooled connection.
* @since 4.2
*/
@ThreadSafe
public abstract class AbstractConnPool<T, C, E extends PoolEntry<T, C>>
implements ConnPool<T, E>, ConnPoolControl<T> {
private final Lock lock;
private final ConnFactory<T, C> connFactory;
private final Map<T, RouteSpecificPool<T, C, E>> routeToPool;
private final Set<E> leased;
private final LinkedList<E> available;
private final LinkedList<PoolEntryFuture<E>> pending;
private final Map<T, Integer> maxPerRoute;
private volatile boolean isShutDown;
private volatile int defaultMaxPerRoute;
private volatile int maxTotal;
public AbstractConnPool(
final ConnFactory<T, C> connFactory,
final int defaultMaxPerRoute,
final int maxTotal) {
super();
this.connFactory = Args.notNull(connFactory, "Connection factory");
this.defaultMaxPerRoute = Args.notNegative(defaultMaxPerRoute, "Max per route value");
this.maxTotal = Args.notNegative(maxTotal, "Max total value");
this.lock = new ReentrantLock();
this.routeToPool = new HashMap<T, RouteSpecificPool<T, C, E>>();
this.leased = new HashSet<E>();
this.available = new LinkedList<E>();
this.pending = new LinkedList<PoolEntryFuture<E>>();
this.maxPerRoute = new HashMap<T, Integer>();
}
/**
* Creates a new entry for the given connection with the given route.
*/
protected abstract E createEntry(T route, C conn);
/**
* @since 4.3
*/
protected void onLease(final E entry) {
}
/**
* @since 4.3
*/
protected void onRelease(final E entry) {
}
public boolean isShutdown() {
return this.isShutDown;
}
/**
* Shuts down the pool.
*/
public void shutdown() throws IOException {
if (this.isShutDown) {
return ;
}
this.isShutDown = true;
this.lock.lock();
try {
for (final E entry: this.available) {
entry.close();
}
for (final E entry: this.leased) {
entry.close();
}
for (final RouteSpecificPool<T, C, E> pool: this.routeToPool.values()) {
pool.shutdown();
}
this.routeToPool.clear();
this.leased.clear();
this.available.clear();
} finally {
this.lock.unlock();
}
}
private RouteSpecificPool<T, C, E> getPool(final T route) {
RouteSpecificPool<T, C, E> pool = this.routeToPool.get(route);
if (pool == null) {
pool = new RouteSpecificPool<T, C, E>(route) {
@Override
protected E createEntry(final C conn) {
return AbstractConnPool.this.createEntry(route, conn);
}
};
this.routeToPool.put(route, pool);
}
return pool;
}
/**
* {@inheritDoc}
* <p/>
* Please note that this class does not maintain its own pool of execution
* {@link Thread}s. Therefore, one <b>must</b> call {@link Future#get()}
* or {@link Future#get(long, TimeUnit)} method on the {@link Future}
* returned by this method in order for the lease operation to complete.
*/
public Future<E> lease(final T route, final Object state, final FutureCallback<E> callback) {
Args.notNull(route, "Route");
Asserts.check(!this.isShutDown, "Connection pool shut down");
return new PoolEntryFuture<E>(this.lock, callback) {
@Override
public E getPoolEntry(
final long timeout,
final TimeUnit tunit)
throws InterruptedException, TimeoutException, IOException {
final E entry = getPoolEntryBlocking(route, state, timeout, tunit, this);
onLease(entry);
return entry;
}
};
}
/**
* Attempts to lease a connection for the given route and with the given
* state from the pool.
* <p/>
* Please note that this class does not maintain its own pool of execution
* {@link Thread}s. Therefore, one <b>must</b> call {@link Future#get()}
* or {@link Future#get(long, TimeUnit)} method on the {@link Future}
* returned by this method in order for the lease operation to complete.
*
* @param route route of the connection.
* @param state arbitrary object that represents a particular state
* (usually a security principal or a unique token identifying
* the user whose credentials have been used while establishing the connection).
* May be <code>null</code>.
* @return future for a leased pool entry.
*/
public Future<E> lease(final T route, final Object state) {
return lease(route, state, null);
}
private E getPoolEntryBlocking(
final T route, final Object state,
final long timeout, final TimeUnit tunit,
final PoolEntryFuture<E> future)
throws IOException, InterruptedException, TimeoutException {
Date deadline = null;
if (timeout > 0) {
deadline = new Date
(System.currentTimeMillis() + tunit.toMillis(timeout));
}
this.lock.lock();
try {
final RouteSpecificPool<T, C, E> pool = getPool(route);
E entry = null;
while (entry == null) {
Asserts.check(!this.isShutDown, "Connection pool shut down");
for (;;) {
entry = pool.getFree(state);
if (entry == null) {
break;
}
if (entry.isClosed() || entry.isExpired(System.currentTimeMillis())) {
entry.close();
this.available.remove(entry);
pool.free(entry, false);
} else {
break;
}
}
if (entry != null) {
this.available.remove(entry);
this.leased.add(entry);
return entry;
}
// New connection is needed
final int maxPerRoute = getMax(route);
// Shrink the pool prior to allocating a new connection
final int excess = Math.max(0, pool.getAllocatedCount() + 1 - maxPerRoute);
if (excess > 0) {
for (int i = 0; i < excess; i++) {
final E lastUsed = pool.getLastUsed();
if (lastUsed == null) {
break;
}
lastUsed.close();
this.available.remove(lastUsed);
pool.remove(lastUsed);
}
}
if (pool.getAllocatedCount() < maxPerRoute) {
final int totalUsed = this.leased.size();
final int freeCapacity = Math.max(this.maxTotal - totalUsed, 0);
if (freeCapacity > 0) {
final int totalAvailable = this.available.size();
if (totalAvailable > freeCapacity - 1) {
if (!this.available.isEmpty()) {
final E lastUsed = this.available.removeLast();
lastUsed.close();
final RouteSpecificPool<T, C, E> otherpool = getPool(lastUsed.getRoute());
otherpool.remove(lastUsed);
}
}
final C conn = this.connFactory.create(route);
entry = pool.add(conn);
this.leased.add(entry);
return entry;
}
}
boolean success = false;
try {
pool.queue(future);
this.pending.add(future);
success = future.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.
pool.unqueue(future);
this.pending.remove(future);
}
// check for spurious wakeup vs. timeout
if (!success && (deadline != null) &&
(deadline.getTime() <= System.currentTimeMillis())) {
break;
}
}
throw new TimeoutException("Timeout waiting for connection");
} finally {
this.lock.unlock();
}
}
public void release(final E entry, final boolean reusable) {
this.lock.lock();
try {
if (this.leased.remove(entry)) {
final RouteSpecificPool<T, C, E> pool = getPool(entry.getRoute());
pool.free(entry, reusable);
if (reusable && !this.isShutDown) {
this.available.addFirst(entry);
onRelease(entry);
} else {
entry.close();
}
PoolEntryFuture<E> future = pool.nextPending();
if (future != null) {
this.pending.remove(future);
} else {
future = this.pending.poll();
}
if (future != null) {
future.wakeup();
}
}
} finally {
this.lock.unlock();
}
}
private int getMax(final T route) {
final Integer v = this.maxPerRoute.get(route);
if (v != null) {
return v.intValue();
} else {
return this.defaultMaxPerRoute;
}
}
public void setMaxTotal(final int max) {
Args.notNegative(max, "Max value");
this.lock.lock();
try {
this.maxTotal = max;
} finally {
this.lock.unlock();
}
}
public int getMaxTotal() {
this.lock.lock();
try {
return this.maxTotal;
} finally {
this.lock.unlock();
}
}
public void setDefaultMaxPerRoute(final int max) {
Args.notNegative(max, "Max per route value");
this.lock.lock();
try {
this.defaultMaxPerRoute = max;
} finally {
this.lock.unlock();
}
}
public int getDefaultMaxPerRoute() {
this.lock.lock();
try {
return this.defaultMaxPerRoute;
} finally {
this.lock.unlock();
}
}
public void setMaxPerRoute(final T route, final int max) {
Args.notNull(route, "Route");
Args.notNegative(max, "Max per route value");
this.lock.lock();
try {
this.maxPerRoute.put(route, Integer.valueOf(max));
} finally {
this.lock.unlock();
}
}
public int getMaxPerRoute(final T route) {
Args.notNull(route, "Route");
this.lock.lock();
try {
return getMax(route);
} finally {
this.lock.unlock();
}
}
public PoolStats getTotalStats() {
this.lock.lock();
try {
return new PoolStats(
this.leased.size(),
this.pending.size(),
this.available.size(),
this.maxTotal);
} finally {
this.lock.unlock();
}
}
public PoolStats getStats(final T route) {
Args.notNull(route, "Route");
this.lock.lock();
try {
final RouteSpecificPool<T, C, E> pool = getPool(route);
return new PoolStats(
pool.getLeasedCount(),
pool.getPendingCount(),
pool.getAvailableCount(),
getMax(route));
} finally {
this.lock.unlock();
}
}
/**
* Enumerates all available connections.
*
* @since 4.3
*/
protected void enumAvailable(final PoolEntryCallback<T, C> callback) {
this.lock.lock();
try {
final Iterator<E> it = this.available.iterator();
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();
}
}
purgePoolMap();
} finally {
this.lock.unlock();
}
}
/**
* Enumerates all leased connections.
*
* @since 4.3
*/
protected void enumLeased(final PoolEntryCallback<T, C> callback) {
this.lock.lock();
try {
final Iterator<E> it = this.leased.iterator();
while (it.hasNext()) {
final E entry = it.next();
callback.process(entry);
}
} finally {
this.lock.unlock();
}
}
private void purgePoolMap() {
final Iterator<Map.Entry<T, RouteSpecificPool<T, C, E>>> it = this.routeToPool.entrySet().iterator();
while (it.hasNext()) {
final Map.Entry<T, RouteSpecificPool<T, C, E>> entry = it.next();
final RouteSpecificPool<T, C, E> pool = entry.getValue();
if (pool.getPendingCount() + pool.getAllocatedCount() == 0) {
it.remove();
}
}
}
/**
* Closes connections that have been idle longer than the given period
* of time and evicts them from the pool.
*
* @param idletime maximum idle time.
* @param tunit time unit.
*/
public void closeIdle(final long idletime, final TimeUnit tunit) {
Args.notNull(tunit, "Time unit");
long time = tunit.toMillis(idletime);
if (time < 0) {
time = 0;
}
final long deadline = System.currentTimeMillis() - time;
enumAvailable(new PoolEntryCallback<T, C>() {
public void process(final PoolEntry<T, C> entry) {
if (entry.getUpdated() <= deadline) {
entry.close();
}
}
});
}
/**
* Closes expired connections and evicts them from the pool.
*/
public void closeExpired() {
final long now = System.currentTimeMillis();
enumAvailable(new PoolEntryCallback<T, C>() {
public void process(final PoolEntry<T, C> entry) {
if (entry.isExpired(now)) {
entry.close();
}
}
});
}
@Override
public String toString() {
final StringBuilder buffer = new StringBuilder();
buffer.append("[leased: ");
buffer.append(this.leased);
buffer.append("][available: ");
buffer.append(this.available);
buffer.append("][pending: ");
buffer.append(this.pending);
buffer.append("]");
return buffer.toString();
}
}

View file

@ -0,0 +1,44 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib.pool;
import java.io.IOException;
/**
* Factory for poolable blocking connections.
*
* @param <T> the route type that represents the opposite endpoint of a pooled
* connection.
* @param <C> the connection type.
* @since 4.2
*/
public interface ConnFactory<T, C> {
C create(T route) throws IOException;
}

View file

@ -0,0 +1,68 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib.pool;
import java.util.concurrent.Future;
import ch.boye.httpclientandroidlib.concurrent.FutureCallback;
/**
* <tt>ConnPool</tt> represents a shared pool connections can be leased from
* and released back to.
*
* @param <T> the route type that represents the opposite endpoint of a pooled
* connection.
* @param <E> the type of the pool entry containing a pooled connection.
* @since 4.2
*/
public interface ConnPool<T, E> {
/**
* Attempts to lease a connection for the given route and with the given
* state from the pool.
*
* @param route route of the connection.
* @param state arbitrary object that represents a particular state
* (usually a security principal or a unique token identifying
* the user whose credentials have been used while establishing the connection).
* May be <code>null</code>.
* @param callback operation completion callback.
*
* @return future for a leased pool entry.
*/
Future<E> lease(final T route, final Object state, final FutureCallback<E> callback);
/**
* Releases the pool entry back to the pool.
*
* @param entry pool entry leased from the pool
* @param reusable flag indicating whether or not the released connection
* is in a consistent state and is safe for further use.
*/
void release(E entry, boolean reusable);
}

View file

@ -0,0 +1,56 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib.pool;
/**
* Interface to control runtime properties of a {@link ConnPool} such as
* maximum total number of connections or maximum connections per route
* allowed.
*
* @param <T> the route type that represents the opposite endpoint of a pooled
* connection.
* @since 4.2
*/
public interface ConnPoolControl<T> {
void setMaxTotal(int max);
int getMaxTotal();
void setDefaultMaxPerRoute(int max);
int getDefaultMaxPerRoute();
void setMaxPerRoute(final T route, int max);
int getMaxPerRoute(final T route);
PoolStats getTotalStats();
PoolStats getStats(final T route);
}

View file

@ -0,0 +1,183 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib.pool;
import java.util.concurrent.TimeUnit;
import ch.boye.httpclientandroidlib.annotation.GuardedBy;
import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
import ch.boye.httpclientandroidlib.util.Args;
/**
* Pool entry containing a pool connection object along with its route.
* <p/>
* The connection contained by the pool entry may have an expiration time which
* can be either set upon construction time or updated with
* the {@link #updateExpiry(long, TimeUnit)}.
* <p/>
* Pool entry may also have an object associated with it that represents
* a connection state (usually a security principal or a unique token identifying
* the user whose credentials have been used while establishing the connection).
*
* @param <T> the route type that represents the opposite endpoint of a pooled
* connection.
* @param <C> the connection type.
* @since 4.2
*/
@ThreadSafe
public abstract class PoolEntry<T, C> {
private final String id;
private final T route;
private final C conn;
private final long created;
private final long validUnit;
@GuardedBy("this")
private long updated;
@GuardedBy("this")
private long expiry;
private volatile Object state;
/**
* Creates new <tt>PoolEntry</tt> instance.
*
* @param id unique identifier of the pool entry. May be <code>null</code>.
* @param route route to the opposite endpoint.
* @param conn the connection.
* @param timeToLive maximum time to live. May be zero if the connection
* does not have an expiry deadline.
* @param tunit time unit.
*/
public PoolEntry(final String id, final T route, final C conn,
final long timeToLive, final TimeUnit tunit) {
super();
Args.notNull(route, "Route");
Args.notNull(conn, "Connection");
Args.notNull(tunit, "Time unit");
this.id = id;
this.route = route;
this.conn = conn;
this.created = System.currentTimeMillis();
if (timeToLive > 0) {
this.validUnit = this.created + tunit.toMillis(timeToLive);
} else {
this.validUnit = Long.MAX_VALUE;
}
this.expiry = this.validUnit;
}
/**
* Creates new <tt>PoolEntry</tt> instance without an expiry deadline.
*
* @param id unique identifier of the pool entry. May be <code>null</code>.
* @param route route to the opposite endpoint.
* @param conn the connection.
*/
public PoolEntry(final String id, final T route, final C conn) {
this(id, route, conn, 0, TimeUnit.MILLISECONDS);
}
public String getId() {
return this.id;
}
public T getRoute() {
return this.route;
}
public C getConnection() {
return this.conn;
}
public long getCreated() {
return this.created;
}
public long getValidUnit() {
return this.validUnit;
}
public Object getState() {
return this.state;
}
public void setState(final Object state) {
this.state = state;
}
public synchronized long getUpdated() {
return this.updated;
}
public synchronized long getExpiry() {
return this.expiry;
}
public synchronized void updateExpiry(final long time, final TimeUnit tunit) {
Args.notNull(tunit, "Time unit");
this.updated = System.currentTimeMillis();
final long newExpiry;
if (time > 0) {
newExpiry = this.updated + tunit.toMillis(time);
} else {
newExpiry = Long.MAX_VALUE;
}
this.expiry = Math.min(newExpiry, this.validUnit);
}
public synchronized boolean isExpired(final long now) {
return now >= this.expiry;
}
/**
* Invalidates the pool entry and closes the pooled connection associated
* with it.
*/
public abstract void close();
/**
* Returns <code>true</code> if the pool entry has been invalidated.
*/
public abstract boolean isClosed();
@Override
public String toString() {
final StringBuilder buffer = new StringBuilder();
buffer.append("[id:");
buffer.append(this.id);
buffer.append("][route:");
buffer.append(this.route);
buffer.append("][state:");
buffer.append(this.state);
buffer.append("]");
return buffer.toString();
}
}

View file

@ -0,0 +1,41 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib.pool;
/**
* 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);
}

View file

@ -0,0 +1,155 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib.pool;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
import ch.boye.httpclientandroidlib.concurrent.FutureCallback;
import ch.boye.httpclientandroidlib.util.Args;
@ThreadSafe
abstract class PoolEntryFuture<T> implements Future<T> {
private final Lock lock;
private final FutureCallback<T> callback;
private final Condition condition;
private volatile boolean cancelled;
private volatile boolean completed;
private T result;
PoolEntryFuture(final Lock lock, final FutureCallback<T> callback) {
super();
this.lock = lock;
this.condition = lock.newCondition();
this.callback = callback;
}
public boolean cancel(final boolean mayInterruptIfRunning) {
this.lock.lock();
try {
if (this.completed) {
return false;
}
this.completed = true;
this.cancelled = true;
if (this.callback != null) {
this.callback.cancelled();
}
this.condition.signalAll();
return true;
} finally {
this.lock.unlock();
}
}
public boolean isCancelled() {
return this.cancelled;
}
public boolean isDone() {
return this.completed;
}
public T get() throws InterruptedException, ExecutionException {
try {
return get(0, TimeUnit.MILLISECONDS);
} catch (final TimeoutException ex) {
throw new ExecutionException(ex);
}
}
public T get(
final long timeout,
final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
Args.notNull(unit, "Time unit");
this.lock.lock();
try {
if (this.completed) {
return this.result;
}
this.result = getPoolEntry(timeout, unit);
this.completed = true;
if (this.callback != null) {
this.callback.completed(this.result);
}
return result;
} catch (final IOException ex) {
this.completed = true;
this.result = null;
if (this.callback != null) {
this.callback.failed(ex);
}
throw new ExecutionException(ex);
} finally {
this.lock.unlock();
}
}
protected abstract T getPoolEntry(
long timeout, TimeUnit unit) throws IOException, InterruptedException, TimeoutException;
public boolean await(final Date deadline) throws InterruptedException {
this.lock.lock();
try {
if (this.cancelled) {
throw new InterruptedException("Operation interrupted");
}
final boolean success;
if (deadline != null) {
success = this.condition.awaitUntil(deadline);
} else {
this.condition.await();
success = true;
}
if (this.cancelled) {
throw new InterruptedException("Operation interrupted");
}
return success;
} finally {
this.lock.unlock();
}
}
public void wakeup() {
this.lock.lock();
try {
this.condition.signalAll();
} finally {
this.lock.unlock();
}
}
}

View file

@ -0,0 +1,114 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib.pool;
import ch.boye.httpclientandroidlib.annotation.Immutable;
/**
* Pool statistics.
* <p>
* The total number of connections in the pool is equal to {@code available} plus {@code leased}.
* </p>
*
* @since 4.2
*/
@Immutable
public class PoolStats {
private final int leased;
private final int pending;
private final int available;
private final int max;
public PoolStats(final int leased, final int pending, final int free, final int max) {
super();
this.leased = leased;
this.pending = pending;
this.available = free;
this.max = max;
}
/**
* Gets the number of persistent connections tracked by the connection manager currently being used to execute
* requests.
* <p>
* The total number of connections in the pool is equal to {@code available} plus {@code leased}.
* </p>
*
* @return the number of persistent connections.
*/
public int getLeased() {
return this.leased;
}
/**
* Gets the number of connection requests being blocked awaiting a free connection. This can happen only if there
* are more worker threads contending for fewer connections.
*
* @return the number of connection requests being blocked awaiting a free connection.
*/
public int getPending() {
return this.pending;
}
/**
* Gets the number idle persistent connections.
* <p>
* The total number of connections in the pool is equal to {@code available} plus {@code leased}.
* </p>
*
* @return number idle persistent connections.
*/
public int getAvailable() {
return this.available;
}
/**
* Gets the maximum number of allowed persistent connections.
*
* @return the maximum number of allowed persistent connections.
*/
public int getMax() {
return this.max;
}
@Override
public String toString() {
final StringBuilder buffer = new StringBuilder();
buffer.append("[leased: ");
buffer.append(this.leased);
buffer.append("; pending: ");
buffer.append(this.pending);
buffer.append("; available: ");
buffer.append(this.available);
buffer.append("; max: ");
buffer.append(this.max);
buffer.append("]");
return buffer.toString();
}
}

View file

@ -0,0 +1,184 @@
/*
* ====================================================================
* 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 ch.boye.httpclientandroidlib.pool;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import ch.boye.httpclientandroidlib.annotation.NotThreadSafe;
import ch.boye.httpclientandroidlib.util.Args;
import ch.boye.httpclientandroidlib.util.Asserts;
@NotThreadSafe
abstract class RouteSpecificPool<T, C, E extends PoolEntry<T, C>> {
private final T route;
private final Set<E> leased;
private final LinkedList<E> available;
private final LinkedList<PoolEntryFuture<E>> pending;
RouteSpecificPool(final T route) {
super();
this.route = route;
this.leased = new HashSet<E>();
this.available = new LinkedList<E>();
this.pending = new LinkedList<PoolEntryFuture<E>>();
}
protected abstract E createEntry(C conn);
public final T getRoute() {
return route;
}
public int getLeasedCount() {
return this.leased.size();
}
public int getPendingCount() {
return this.pending.size();
}
public int getAvailableCount() {
return this.available.size();
}
public int getAllocatedCount() {
return this.available.size() + this.leased.size();
}
public E getFree(final Object state) {
if (!this.available.isEmpty()) {
if (state != null) {
final Iterator<E> it = this.available.iterator();
while (it.hasNext()) {
final E entry = it.next();
if (state.equals(entry.getState())) {
it.remove();
this.leased.add(entry);
return entry;
}
}
}
final Iterator<E> it = this.available.iterator();
while (it.hasNext()) {
final E entry = it.next();
if (entry.getState() == null) {
it.remove();
this.leased.add(entry);
return entry;
}
}
}
return null;
}
public E getLastUsed() {
if (!this.available.isEmpty()) {
return this.available.getLast();
} else {
return null;
}
}
public boolean remove(final E entry) {
Args.notNull(entry, "Pool entry");
if (!this.available.remove(entry)) {
if (!this.leased.remove(entry)) {
return false;
}
}
return true;
}
public void free(final E entry, final boolean reusable) {
Args.notNull(entry, "Pool entry");
final boolean found = this.leased.remove(entry);
Asserts.check(found, "Entry %s has not been leased from this pool", entry);
if (reusable) {
this.available.addFirst(entry);
}
}
public E add(final C conn) {
final E entry = createEntry(conn);
this.leased.add(entry);
return entry;
}
public void queue(final PoolEntryFuture<E> future) {
if (future == null) {
return;
}
this.pending.add(future);
}
public PoolEntryFuture<E> nextPending() {
return this.pending.poll();
}
public void unqueue(final PoolEntryFuture<E> future) {
if (future == null) {
return;
}
this.pending.remove(future);
}
public void shutdown() {
for (final PoolEntryFuture<E> future: this.pending) {
future.cancel(true);
}
this.pending.clear();
for (final E entry: this.available) {
entry.close();
}
this.available.clear();
for (final E entry: this.leased) {
entry.close();
}
this.leased.clear();
}
@Override
public String toString() {
final StringBuilder buffer = new StringBuilder();
buffer.append("[route: ");
buffer.append(this.route);
buffer.append("][leased: ");
buffer.append(this.leased.size());
buffer.append("][available: ");
buffer.append(this.available.size());
buffer.append("][pending: ");
buffer.append(this.pending.size());
buffer.append("]");
return buffer.toString();
}
}

View file

@ -0,0 +1,32 @@
/*
* ====================================================================
* 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/>.
*
*/
/**
* Client side connection pools APIs for synchronous, blocking
* communication.
*/
package ch.boye.httpclientandroidlib.pool;