Copyright | (c) Dong Han 2017 |
---|---|
License | BSD |
Maintainer | winterland1989@gmail.com |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
This module also implements Gabriel Gonzalez'd idea on Resource
applicative:
http://www.haskellforall.com/2013/06/the-resource-applicative.html. The Applicative
and Monad
instance is
especially useful when you want safely combine multiple resources.
A high performance resource pool based on STM is also provided.
Synopsis
- newtype Resource a = Resource {}
- initResource :: IO a -> (a -> IO ()) -> Resource a
- initResource_ :: IO a -> IO () -> Resource a
- withResource :: (MonadMask m, MonadIO m, HasCallStack) => Resource a -> (a -> m b) -> m b
- withResource' :: (MonadMask m, MonadIO m, HasCallStack) => Resource a -> (a -> m () -> m b) -> m b
- data Pool a
- data PoolState
- initPool :: Resource a -> Int -> Int -> Resource (Pool a)
- withResourceInPool :: (MonadMask m, MonadIO m, HasCallStack) => Pool a -> (a -> m b) -> m b
- poolStat :: Pool a -> IO PoolState
- poolInUse :: Pool a -> IO Int
- liftIO :: MonadIO m => IO a -> m a
Resource management
A Resource
is an IO
action which acquires some resource of type a and
also returns a finalizer of type IO () that releases the resource.
The only safe way to use a Resource
is withResource
and withResource'
,
You should not use the acquire
field directly, unless you want to implement your own
resource management. In the later case, you should mask_
acquire
since
some resource initializations may assume async exceptions are masked.
MonadIO
instance is provided so that you can lift IO
computation inside
Resource
, this is convenient for propagating Resource
around since many
IO
computations carry finalizers.
A convention in Z-IO is that functions returning a Resource
should be
named in initXXX
format, users are strongly recommended to follow this convention.
There're two additional guarantees we made in Z-IO:
- All resources in Z-IO can track its own liveness, throw
ResourceVanished
exception usingthrowECLOSED
orthrowECLOSEDSTM
when used after resource is closed. - All resources' clean up action in Z-IO is idempotent.
Library authors providing initXXX
are also encouraged to provide these guarantees.
initResource :: IO a -> (a -> IO ()) -> Resource a Source #
Create Resource
from create and release action.
Note, resource
doesn't open resource itself, resource is created when you use
with
/ with'
.
withResource :: (MonadMask m, MonadIO m, HasCallStack) => Resource a -> (a -> m b) -> m b Source #
Create a new resource and run some computation, resource is guarantee to be closed.
Be care don't leak the resource through computation return value, because after the computation finishes, the resource is closed already.
withResource' :: (MonadMask m, MonadIO m, HasCallStack) => Resource a -> (a -> m () -> m b) -> m b Source #
Create a new resource and run some computation, resource is guarantee to be closed.
The difference from with
is that the computation will receive an extra
close action, which can be used to close the resource early before the whole
computation finished, the close action can be called multiple times,
only the first call will clean up the resource.
Resource pool
A high performance resource pool based on STM.
We choose to not divide pool into strips due to the difficults in resource balancing. If there
is a high contention on resource (see statPool
), just increase the maximum number of resources
can be opened.
:: Resource a | |
-> Int | maximum number of resources can be opened |
-> Int | amount of time after which an unused resource can be released (in seconds). |
-> Resource (Pool a) |
Initialize a resource pool with given Resource
Like other initXXX functions, this function won't open a resource pool until you use withResource
.
And this resource pool follow the same resource management pattern like other resources.
withResourceInPool :: (MonadMask m, MonadIO m, HasCallStack) => Pool a -> (a -> m b) -> m b Source #
Open resource inside a given resource pool and do some computation.
This function is thread safe, concurrently usage will be guaranteed to get different resource. If exception happens, resource will be closed(not return to pool).
poolStat :: Pool a -> IO PoolState Source #
Get a resource pool's PoolState
This function is useful when debug, under load lots of PoolEmpty
may indicate
contention on resources, i.e. the limit on maximum number of resources can be opened
should be adjusted to a higher number. On the otherhand, lots of PoolScanning
may indicate there're too much free resources.
poolInUse :: Pool a -> IO Int Source #
Get how many resource is being used within a resource pool.
This function is useful when debug, under load in use number alway reaches limit may indicate contention on resources, i.e. the limit on maximum number of resources can be opened should be adjusted to a higher number.