Package com.palantir.common.pooling
Class AbstractPoolingContainer<T>
- java.lang.Object
-
- com.palantir.common.pooling.AbstractPoolingContainer<T>
-
- All Implemented Interfaces:
PoolingContainer<T>
public abstract class AbstractPoolingContainer<T> extends Object implements PoolingContainer<T>
This class will pool resources up to the passedLimit. It will never block and instead will always allocate a new resource if there are none in the pool.
-
-
Field Summary
Fields Modifier and Type Field Description protected AtomicLong
allocatedResources
-
Constructor Summary
Constructors Constructor Description AbstractPoolingContainer(int itemsToPool)
Pool up to itemsToPool resources.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
cleanupForDiscard(T discardedResource)
This will be called after a resource is done being used but the pool is full and it will be discardedprotected void
cleanupForReturnToPool(T resourceToReturn)
This will be called after a resource is done being used and will be returned to the pool so it maybe used again.protected abstract T
createNewPooledResource()
This should create a new resource.protected void
discardCurrentPool()
This method is used when we still want to continue to pool, but we think that the current pooled resources are no longer good to use.long
getAllocatedResources()
int
getMaxPoolSize()
protected T
getResource()
This should always be followed by a try/finallyprotected void
returnResource(T resource)
This method should only be called in the finally block of a try/finally<V> V
runWithPooledResource(com.google.common.base.Function<T,V> f)
This will run the provided function with a pooled resource.<V,K extends Exception>
VrunWithPooledResource(FunctionCheckedException<T,V,K> f)
void
shutdownPooling()
This method will callcleanupForDiscard(Object)
on everything that is in the pool and will make the pool of size zero.
-
-
-
Field Detail
-
allocatedResources
protected final AtomicLong allocatedResources
-
-
Constructor Detail
-
AbstractPoolingContainer
public AbstractPoolingContainer(int itemsToPool)
Pool up to itemsToPool resources. If the internal pool is already full, then additional ones will be thrown away.If items is
Integer.MAX_VALUE
, then we will use an unbounded queue withConcurrentLinkedQueue
which should have better performance because it is lock-free- Parameters:
itemsToPool
- must be at least 1
-
-
Method Detail
-
getAllocatedResources
public long getAllocatedResources()
-
getMaxPoolSize
public int getMaxPoolSize()
-
createNewPooledResource
@Nonnull protected abstract T createNewPooledResource()
This should create a new resource. It should be non-null.
-
cleanupForDiscard
protected void cleanupForDiscard(T discardedResource)
This will be called after a resource is done being used but the pool is full and it will be discardedThis should not throw because it is called in finally blocks.
-
cleanupForReturnToPool
protected void cleanupForReturnToPool(T resourceToReturn)
This will be called after a resource is done being used and will be returned to the pool so it maybe used again.cleanupForDiscard(Object)
may be called for this resource after this returns if the pool was already full orshutdownPooling()
has been called.This should not throw because it is called in finally blocks.
-
runWithPooledResource
public <V> V runWithPooledResource(com.google.common.base.Function<T,V> f)
This will run the provided function with a pooled resource.createNewPooledResource()
will be called if it cannot get one from the pool. This function is not allowed to hold onto this resource after it completes. If it does, there will be undefined behavior.- Specified by:
runWithPooledResource
in interfacePoolingContainer<T>
- Returns:
- whatever the passed function returns
-
runWithPooledResource
public <V,K extends Exception> V runWithPooledResource(FunctionCheckedException<T,V,K> f) throws K extends Exception
- Specified by:
runWithPooledResource
in interfacePoolingContainer<T>
- Throws:
K extends Exception
-
getResource
protected final T getResource()
This should always be followed by a try/finally
-
returnResource
protected final void returnResource(T resource)
This method should only be called in the finally block of a try/finally
-
shutdownPooling
public void shutdownPooling()
This method will callcleanupForDiscard(Object)
on everything that is in the pool and will make the pool of size zero. Once this method is called using this pool will just result increateNewPooledResource()
being called for every use andcleanupForDiscard(Object)
being called after it is used.- Specified by:
shutdownPooling
in interfacePoolingContainer<T>
-
discardCurrentPool
protected void discardCurrentPool()
This method is used when we still want to continue to pool, but we think that the current pooled resources are no longer good to use.
-
-