pool-conduit-0.1.2/0000755000000000000000000000000012137377100012315 5ustar0000000000000000pool-conduit-0.1.2/pool-conduit.cabal0000644000000000000000000000175612137377100015726 0ustar0000000000000000name: pool-conduit version: 0.1.2 license: MIT license-file: LICENSE author: Michael Snoyman maintainer: Michael Snoyman synopsis: Resource pool allocations via ResourceT. description: Allocate resources from a pool, guaranteeing resource handling via the ResourceT transformer. category: Database, Yesod, Conduit stability: Stable cabal-version: >= 1.8 build-type: Simple homepage: http://www.yesodweb.com/book/persistent library build-depends: base >= 4 && < 5 , resource-pool >= 0.2.1 && < 0.3 , transformers >= 0.2.1 , resourcet >= 0.3 && < 0.5 , monad-control exposed-modules: Data.Conduit.Pool ghc-options: -Wall source-repository head type: git location: git://github.com/yesodweb/persistent.git pool-conduit-0.1.2/LICENSE0000644000000000000000000000207512137377100013326 0ustar0000000000000000Copyright (c) 2012 Michael Snoyman, http://www.yesodweb.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pool-conduit-0.1.2/Setup.lhs0000644000000000000000000000016212137377100014124 0ustar0000000000000000#!/usr/bin/env runhaskell > module Main where > import Distribution.Simple > main :: IO () > main = defaultMain pool-conduit-0.1.2/Data/0000755000000000000000000000000012137377100013166 5ustar0000000000000000pool-conduit-0.1.2/Data/Conduit/0000755000000000000000000000000012137377100014573 5ustar0000000000000000pool-conduit-0.1.2/Data/Conduit/Pool.hs0000644000000000000000000000660212137377100016044 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-} -- | Allocate resources from a pool, guaranteeing resource handling via the -- ResourceT transformer. module Data.Conduit.Pool ( ManagedResource (..) , takeResource , takeResourceCheck , P.Pool , P.createPool , P.withResource , withResourceTimeout , withResourceT ) where import qualified Data.Pool as P import Control.Monad (liftM) import Control.Monad.Trans.Resource import Control.Monad.IO.Class (liftIO) import qualified Data.IORef as I import Control.Exception (onException, mask) import System.Timeout (timeout) import Control.Monad.Trans.Control (control) -- | The result of taking a resource. data ManagedResource m a = ManagedResource { mrValue :: a -- ^ The actual resource. , mrReuse :: Bool -> m () -- ^ Let's you specify whether the resource should be returned to the pool -- (via 'P.putResource') or destroyed (via 'P.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. } -- | Like 'P.withResource', but uses 'MonadResource' instead of 'MonadBaseControl'. -- -- Since 0.1.1 withResourceT :: MonadResource m => P.Pool a -> (a -> m b) -> m b withResourceT pool f = do mr <- takeResource pool b <- f $ mrValue mr mrReuse mr True mrRelease mr return b -- | Like 'P.withResource', but times out the operation if resource -- allocation does not complete within the given timeout period. -- -- Since 0.1.2 withResourceTimeout :: #if MIN_VERSION_monad_control(0,3,0) (MonadBaseControl IO m) #else (MonadControlIO m) #endif => Int -- ^ Timeout period in microseconds -> P.Pool a -> (a -> m b) -> m (Maybe b) {-# SPECIALIZE withResourceTimeout :: Int -> P.Pool a -> (a -> IO b) -> IO (Maybe b) #-} withResourceTimeout ms pool act = control $ \runInIO -> mask $ \restore -> do mres <- timeout ms $ P.takeResource pool case mres of Nothing -> runInIO $ return Nothing Just (resource, local) -> do ret <- restore (runInIO (liftM Just $ act resource)) `onException` P.destroyResource pool local resource P.putResource local resource return ret #if __GLASGOW_HASKELL__ >= 700 {-# INLINABLE withResourceTimeout #-} #endif -- | Take a resource from the pool and register a release action. takeResource :: MonadResource m => P.Pool a -> m (ManagedResource m a) takeResource pool = do onRelRef <- liftIO $ I.newIORef False (relKey, (a, _)) <- allocate (P.takeResource pool) (\(a, local) -> do onRel <- I.readIORef onRelRef if onRel then P.putResource local a else P.destroyResource pool local a) return ManagedResource { mrValue = a , mrReuse = liftIO . I.writeIORef onRelRef , mrRelease = release relKey } -- | Same as 'takeResource', but apply some action to check if a resource is -- still valid. takeResourceCheck :: MonadResource m => P.Pool a -> (a -> m Bool) -> m (ManagedResource m a) takeResourceCheck pool check = do mr <- takeResource pool isValid <- check $ mrValue mr if isValid then return mr else do mrRelease mr takeResourceCheck pool check