Data.Pool.Internal

resource-pool-0.4.0.0: A high-performance striped resource pooling implementation

Safe Haskell | None
Language | Haskell2010

Data.Pool.Internal

Description

Internal implementation details for Data.Pool.

This module is intended for internal use only, and may change without warning in subsequent releases.

Synopsis

- data Pool a = Pool {
  - poolConfig :: !(PoolConfig a)
  - localPools :: !(SmallArray (LocalPool a))
  - reaperRef :: !(IORef ())
  }
- data LocalPool a = LocalPool {
  - stripeId :: !Int
  - stripeVar :: !(MVar (Stripe a))
  - cleanerRef :: !(IORef ())
  }
- data Stripe a = Stripe {
  - available :: !Int
  - cache :: ![Entry a]
  - queue :: !(Queue a)
  - queueR :: !(Queue a)
  }
- data Entry a = Entry {
  - entry :: a
  - lastUsed :: !Double
  }
- data Queue a
  - = Queue !(MVar (Maybe a)) (Queue a)
  - | Empty
- data PoolConfig a = PoolConfig {
  - createResource :: !(IO a)
  - freeResource :: !(a -> IO ())
  - poolCacheTTL :: !Double
  - poolMaxResources :: !Int
  - poolNumStripes :: !(Maybe Int)
  }
- defaultPoolConfig :: IO a -> (a -> IO ()) -> Double -> Int -> PoolConfig a
- setNumStripes :: Maybe Int -> PoolConfig a -> PoolConfig a
- newPool :: PoolConfig a -> IO (Pool a)
- destroyResource :: Pool a -> LocalPool a -> a -> IO ()
- putResource :: LocalPool a -> a -> IO ()
- destroyAllResources :: Pool a -> IO ()
- getLocalPool :: SmallArray (LocalPool a) -> IO (LocalPool a)
- waitForResource :: MVar (Stripe a) -> MVar (Maybe a) -> IO (Maybe a)
- restoreSize :: MVar (Stripe a) -> IO ()
- cleanStripe :: (Entry a -> Bool) -> (a -> IO ()) -> MVar (Stripe a) -> IO ()
- signal :: Stripe a -> Maybe a -> IO (Stripe a)
- reverseQueue :: Queue a -> Queue a

# Documentation

data Pool a #

Striped resource pool based on Control.Concurrent.QSem.

Constructors

Pool |
Fields poolConfig :: !(PoolConfig a) localPools :: !(SmallArray (LocalPool a)) reaperRef :: !(IORef ())

data LocalPool a #

A single, local pool.

Constructors

LocalPool |
Fields stripeId :: !Int stripeVar :: !(MVar (Stripe a)) cleanerRef :: !(IORef ())

data Stripe a #

Stripe of a resource pool. If `available` is 0, the list of threads waiting for a resource (each with an associated `MVar`) is `queue ++ reverse queueR`.

Constructors

Stripe |
Fields available :: !Int cache :: ![Entry a] queue :: !(Queue a) queueR :: !(Queue a)

data Entry a #

An existing resource currently sitting in a pool.

Constructors

Entry |
Fields entry :: a lastUsed :: !Double

data Queue a #

A queue of MVarS corresponding to threads waiting for resources.

Basically a monomorphic list to save two pointer indirections.

Constructors

Queue !(MVar (Maybe a)) (Queue a) |
Empty |

data PoolConfig a #

Configuration of a `Pool`.

Constructors

PoolConfig |
Fields createResource :: !(IO a) freeResource :: !(a -> IO ()) poolCacheTTL :: !Double poolMaxResources :: !Int poolNumStripes :: !(Maybe Int)

defaultPoolConfig #

Arguments

:: IO a | The action that creates a new resource.
-> (a -> IO ()) | The action that destroys an existing resource.
-> Double | The amount of seconds for which an unused resource is kept around. The smallest acceptable value is `0.5`. Note: the elapsed time before destroying a resource may be a little longer than requested, as the collector thread wakes at 1-second intervals.
-> Int | The maximum number of resources to keep open across all stripes. The smallest acceptable value is `1`. Note: for each stripe the number of resources is divided by the number of stripes and rounded up, hence the pool might end up creating up to `N - 1` resources more in total than specified, where `N` is the number of stripes.
-> PoolConfig a |

Create a `PoolConfig` with optional parameters having default values.

For setting optional parameters have a look at:

- `setNumStripes`

Since: 0.4.0.0

setNumStripes :: Maybe Int -> PoolConfig a -> PoolConfig a #

Set the number of stripes in the pool.

If set to `Nothing` (the default value), the pool will create the amount of stripes equal to the number of capabilities. This ensures that threads never compete over access to the same stripe and results in a very good performance in a multi-threaded environment.

Since: 0.4.0.0

newPool :: PoolConfig a -> IO (Pool a) #

Create a new striped resource pool.

Note: although the runtime system will destroy all idle resources when the pool is garbage collected, it's recommended to manually call `destroyAllResources` when you're done with the pool so that the resources are freed up as soon as possible.

destroyResource :: Pool a -> LocalPool a -> a -> IO () #

Destroy a resource.

Note that this will ignore any exceptions in the destroy function.

putResource :: LocalPool a -> a -> IO () #

Return a resource to the given `LocalPool`.

destroyAllResources :: Pool a -> IO () #

Destroy all resources in all stripes in the pool.

Note that this will ignore any exceptions in the destroy function.

This function is useful when you detect that all resources in the pool are broken. For example after a database has been restarted all connections opened before the restart will be broken. In that case it's better to close those connections so that `takeResource` won't take a broken connection from the pool but will open a new connection instead.

Another use-case for this function is that when you know you are done with the pool you can destroy all idle resources immediately instead of waiting on the garbage collector to destroy them, thus freeing up those resources sooner.

getLocalPool :: SmallArray (LocalPool a) -> IO (LocalPool a) #

Get a local pool.

waitForResource :: MVar (Stripe a) -> MVar (Maybe a) -> IO (Maybe a) #

Wait for the resource to be put into a given `MVar`.

restoreSize :: MVar (Stripe a) -> IO () #

If an exception is received while a resource is being created, restore the original size of the stripe.

cleanStripe :: (Entry a -> Bool) -> (a -> IO ()) -> MVar (Stripe a) -> IO () #

Free resource entries in the stripes that fulfil a given condition.

signal :: Stripe a -> Maybe a -> IO (Stripe a) #

reverseQueue :: Queue a -> Queue a #