com.twitter.common.net.pool
Class DynamicPool<H,T,E>

java.lang.Object
  extended by com.twitter.common.net.pool.DynamicPool<H,T,E>
Type Parameters:
H - The type that contains metadata information about hosts, such as liveness and address.
T - The raw connection type that is being pooled.
E - The type that identifies the endpoint of the pool, such as an address.
All Implemented Interfaces:
ObjectPool<Connection<T,E>>

public class DynamicPool<H,T,E>
extends Object
implements ObjectPool<Connection<T,E>>

An ObjectPool that maintains a set of connections for a set of service endpoints defined by a ServerSet.


Field Summary
 
Fields inherited from interface com.twitter.common.net.pool.ObjectPool
NO_TIMEOUT
 
Constructor Summary
DynamicPool(DynamicHostSet<H> hostSet, Function<E,ObjectPool<Connection<T,E>>> endpointPoolFactory, LoadBalancer<E> loadBalancer, Closure<Collection<E>> onBackendsChosen, Amount<Long,Time> restoreInterval, Function<H,E> endpointExtractor, Predicate<H> livenessChecker)
          Creates a new ServerSetConnectionPool and blocks on an initial read and constructions of pools for the given serverSet.
 
Method Summary
 void close()
          Disallows further gets from this pool, "closes" all idle objects and any outstanding objects when they are released.
 Connection<T,E> get()
          Gets a resource potentially blocking for as long as it takes to either create a new one or wait for one to be released.
 Connection<T,E> get(Amount<Long,Time> timeout)
          Gets a resource; timing out if there are none available and it takes longer than specified to create a new one or wait for one to be released.
 void release(Connection<T,E> connection)
          Releases a resource obtained from this pool back into the pool of available resources.
 void remove(Connection<T,E> connection)
          Removes a resource obtained from this pool from its available resources.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DynamicPool

public DynamicPool(DynamicHostSet<H> hostSet,
                   Function<E,ObjectPool<Connection<T,E>>> endpointPoolFactory,
                   LoadBalancer<E> loadBalancer,
                   Closure<Collection<E>> onBackendsChosen,
                   Amount<Long,Time> restoreInterval,
                   Function<H,E> endpointExtractor,
                   Predicate<H> livenessChecker)
            throws DynamicHostSet.MonitorException
Creates a new ServerSetConnectionPool and blocks on an initial read and constructions of pools for the given serverSet.

Parameters:
hostSet - the dynamic set of available servers to pool connections for
endpointPoolFactory - a factory that can generate a connection pool for an endpoint
loadBalancer - Load balancer to manage request flow.
onBackendsChosen - A callback to notify of chosen backends.
restoreInterval - the interval after connection errors start occurring for a target to begin checking to see if it has come back to a healthy state
endpointExtractor - Function that transforms a service instance into an endpoint instance.
livenessChecker - Filter that will determine whether a host indicates itself as available.
Throws:
DynamicHostSet.MonitorException - if there is a problem monitoring the host set
Method Detail

get

public Connection<T,E> get()
                    throws ResourceExhaustedException,
                           TimeoutException
Description copied from interface: ObjectPool
Gets a resource potentially blocking for as long as it takes to either create a new one or wait for one to be released. Callers must release the connection when they are done with it.

Specified by:
get in interface ObjectPool<Connection<T,E>>
Returns:
a resource for exclusive use by the caller
Throws:
ResourceExhaustedException - if no resource could be obtained because this pool was exhausted
TimeoutException - if we timed out while trying to fetch a resource

get

public Connection<T,E> get(Amount<Long,Time> timeout)
                    throws ResourceExhaustedException,
                           TimeoutException
Description copied from interface: ObjectPool
Gets a resource; timing out if there are none available and it takes longer than specified to create a new one or wait for one to be released. Callers must (Object) release the connection when they are done with it.

Specified by:
get in interface ObjectPool<Connection<T,E>>
Parameters:
timeout - the maximum amount of time to wait
Returns:
a resource for exclusive use by the caller
Throws:
ResourceExhaustedException - if no resource could be obtained because this pool was exhausted
TimeoutException - if the specified timeout was reached before a resource became available

release

public void release(Connection<T,E> connection)
Description copied from interface: ObjectPool
Releases a resource obtained from this pool back into the pool of available resources. It is an error to release a resource not obtained from this pool.

Specified by:
release in interface ObjectPool<Connection<T,E>>
Parameters:
connection - Resource to release.

remove

public void remove(Connection<T,E> connection)
Description copied from interface: ObjectPool
Removes a resource obtained from this pool from its available resources. It is an error to remove a resource not obtained from this pool.

Specified by:
remove in interface ObjectPool<Connection<T,E>>
Parameters:
connection - Resource to remove.

close

public void close()
Description copied from interface: ObjectPool
Disallows further gets from this pool, "closes" all idle objects and any outstanding objects when they are released.

Specified by:
close in interface ObjectPool<Connection<T,E>>