pool-conduit-0.1.0.2: Resource pool allocations via ResourceT.

Safe HaskellSafe-Infered

Data.Conduit.Pool

Description

Allocate resources from a pool, guaranteeing resource handling via the ResourceT transformer.

Synopsis

Documentation

data ManagedResource m a Source

The result of taking a resource.

Constructors

ManagedResource 

Fields

mrValue :: a

The actual resource.

mrReuse :: Bool -> m ()

Let's you specify whether the resource should be returned to the pool (via putResource) or destroyed (via destroyResource) on release. This defaults to destruction, in case of exceptions.

mrRelease :: m ()

Release this resource, either destroying it or returning it to the pool.

takeResource :: MonadResource m => Pool a -> m (ManagedResource m a)Source

Take a resource from the pool and register a release action.

takeResourceCheck :: MonadResource m => Pool a -> (a -> m Bool) -> m (ManagedResource m a)Source

Same as takeResource, but apply some action to check if a resource is still valid.

data Pool a

Instances

Show (Pool a) 

createPool

Arguments

:: IO a

Action that creates a new resource.

-> (a -> IO ())

Action that destroys an existing resource.

-> Int

Stripe count. The number of distinct sub-pools to maintain. The smallest acceptable value is 1.

-> NominalDiffTime

Amount of time for which an unused resource is kept open. The smallest acceptable value is 0.5 seconds.

The elapsed time before destroying a resource may be a little longer than requested, as the reaper thread wakes at 1-second intervals.

-> Int

Maximum number of resources to keep open per stripe. The smallest acceptable value is 1.

Requests for resources will block if this limit is reached on a single stripe, even if other stripes have idle resources available.

-> IO (Pool a) 

withResource :: MonadBaseControl IO m => Pool a -> (a -> m b) -> m b

Temporarily take a resource from a 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.