HORIZON HASKELLDocslts/ghc-10.0.x8174fa42026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8174fa4 · 2026-10-11

Moduleresource-pool-0.5.1.0Haskell2010

Data.Pool

A high-performance pooling abstraction for managing flexibly-sized collections of resources such as database connections.

  • 3 types
  • 12 values

Pool

3 declarations
valuenewPool :: 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.

Configuration

valuedefaultPoolConfig
  1. :: IO a

    The action that creates a new resource.

  2. -> (a -> IO ())

    The action that destroys an existing resource.

  3. -> Double

    The number of seconds for which an unused resource is kept around. The smallest acceptable value is 0.5.

  4. -> Int

    The maximum number of resources to keep open across all stripes. The smallest acceptable value is 1 per stripe.

    Note: if the number of stripes does not divide the number of resources, some of the stripes will have 1 more resource available than the others.

  5. -> PoolConfig a
#

Create a PoolConfig with optional parameters having default values.

For setting optional parameters have a look at:

Set the number of stripes (sub-pools) in the pool.

If not explicitly set, the default number of stripes is 1, which should be good for typical use (when in doubt, profile your application first).

If set to Nothing, the pool will create the number of stripes equal to the number of capabilities.

Note: usage of multiple stripes reduces contention, but can also result in suboptimal use of resources since stripes are separated from each other.

Resource management

7 declarations
valuewithResource :: Pool a -> (a -> IO r) -> IO r
#

Take a resource from the pool, perform an action with it and return it to the pool afterwards.

  • If the pool has an idle resource available, it is used immediately.

  • Otherwise, if the maximum number of resources has not yet been reached, a new resource is created and used.

  • If the maximum number of resources has been reached, this function blocks until a resource becomes available.

If the action throws an exception of any type, the resource is destroyed and not returned to the pool.

It probably goes without saying that you should never manually destroy a pooled resource, as doing so will almost certainly cause a subsequent user (who expects the resource to be valid) to throw an exception.

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

Destroy a resource.

Note: since version 0.5.0.0 exceptions thrown by the destroy function are no longer ignored.

valuedestroyAllResources :: 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.

Compatibility with 0.2

1 declaration