packages feed

pool-conduit (empty) → 0.0.0

raw patch · 4 files changed

+119/−0 lines, 4 filesdep +basedep +conduitdep +resource-poolsetup-changed

Dependencies added: base, conduit, resource-pool, transformers

Files

+ Data/Conduit/Pool.hs view
@@ -0,0 +1,59 @@+-- | 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+    ) where++import qualified Data.Pool as P+import Control.Monad.Trans.Resource+import Control.Monad.IO.Class (liftIO)+import qualified Data.IORef as I++-- | The result of taking a resource.+data ManagedResource m a = ManagedResource+    { mrValue :: a -- ^ The actual resource.+    , mrReuse :: Bool -> ResourceT 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 :: ResourceT m ()+    -- ^ Release this resource, either destroying it or returning it to the+    -- pool.+    }++-- | Take a resource from the pool and register a release action.+takeResource :: ResourceIO m => P.Pool a -> ResourceT m (ManagedResource m a)+takeResource pool = do+    onRelRef <- liftIO $ I.newIORef False+    (relKey, (a, _)) <- withIO+        (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 :: ResourceIO m+                  => P.Pool a+                  -> (a -> ResourceT m Bool)+                  -> ResourceT 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
+ LICENSE view
@@ -0,0 +1,25 @@+The following license covers this documentation, and the source code, except+where otherwise indicated.++Copyright 2010, Michael Snoyman. All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+  list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+  this list of conditions and the following disclaimer in the documentation+  and/or other materials provided with the distribution.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO+EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,+OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.lhs view
@@ -0,0 +1,7 @@+#!/usr/bin/env runhaskell++> module Main where+> import Distribution.Simple++> main :: IO ()+> main = defaultMain
+ pool-conduit.cabal view
@@ -0,0 +1,28 @@+name:            pool-conduit+version:         0.0.0+license:         BSD3+license-file:    LICENSE+author:          Michael Snoyman <michael@snoyman.com>+maintainer:      Michael Snoyman <michael@snoyman.com>+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   && < 0.3+                   , conduit                  >= 0.0.2   && < 0.2++    exposed-modules: Data.Conduit.Pool++    ghc-options:     -Wall++source-repository head+  type:     git+  location: git://github.com/yesodweb/persistent.git