resource-pool-0.2.3.2/0000755000000000000000000000000012443772326012652 5ustar0000000000000000resource-pool-0.2.3.2/LICENSE0000644000000000000000000000266712443772326013672 0ustar0000000000000000Copyright (c) 2011, MailRank, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. 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. 3. Neither the name of the author nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``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 AUTHORS OR CONTRIBUTORS 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. resource-pool-0.2.3.2/Setup.lhs0000644000000000000000000000011412443772326014456 0ustar0000000000000000#!/usr/bin/env runhaskell > import Distribution.Simple > main = defaultMain resource-pool-0.2.3.2/resource-pool.cabal0000644000000000000000000000253712443772326016443 0ustar0000000000000000name: resource-pool version: 0.2.3.2 synopsis: A high-performance striped resource pooling implementation description: A high-performance striped pooling abstraction for managing flexibly-sized collections of resources such as database connections. homepage: http://github.com/bos/pool license: BSD3 license-file: LICENSE author: Bryan O'Sullivan maintainer: Bryan O'Sullivan , Bas van Dijk copyright: Copyright 2011 MailRank, Inc. category: Data, Database, Network build-type: Simple extra-source-files: README.markdown cabal-version: >=1.8 flag developer description: operate in developer mode default: False manual: True library exposed-modules: Data.Pool build-depends: base >= 4.4 && < 5, hashable, monad-control >= 0.2.0.1, transformers, transformers-base >= 0.4, stm >= 2.3, time, vector >= 0.7 if flag(developer) ghc-options: -Werror ghc-prof-options: -auto-all cpp-options: -DASSERTS -DDEBUG ghc-options: -Wall source-repository head type: git location: http://github.com/bos/pool source-repository head type: mercurial location: http://bitbucket.org/bos/pool resource-pool-0.2.3.2/README.markdown0000644000000000000000000000130112443772326015346 0ustar0000000000000000# Welcome to pool pool is a fast Haskell library for managing medium-lifetime pooled resources, such as database connections. # Join in! We are happy to receive bug reports, fixes, documentation enhancements, and other improvements. Please report bugs via the [github issue tracker](http://github.com/bos/pool/issues). Master [git repository](http://github.com/bos/pool): * `git clone git://github.com/bos/pool.git` There's also a [Mercurial mirror](http://bitbucket.org/bos/pool): * `hg clone http://bitbucket.org/bos/pool` (You can create and contribute changes using either git or Mercurial.) Authors ------- This library is written and maintained by Bryan O'Sullivan, . resource-pool-0.2.3.2/Data/0000755000000000000000000000000012443772326013523 5ustar0000000000000000resource-pool-0.2.3.2/Data/Pool.hs0000644000000000000000000003500412443772326014772 0ustar0000000000000000{-# LANGUAGE CPP, NamedFieldPuns, RecordWildCards, ScopedTypeVariables, RankNTypes, DeriveDataTypeable #-} #if MIN_VERSION_monad_control(0,3,0) {-# LANGUAGE FlexibleContexts #-} #endif #if !MIN_VERSION_base(4,3,0) {-# LANGUAGE RankNTypes #-} #endif -- | -- Module: Data.Pool -- Copyright: (c) 2011 MailRank, Inc. -- License: BSD3 -- Maintainer: Bryan O'Sullivan , -- Bas van Dijk -- Stability: experimental -- Portability: portable -- -- A high-performance striped pooling abstraction for managing -- flexibly-sized collections of resources such as database -- connections. -- -- \"Striped\" means that a single 'Pool' consists of several -- sub-pools, each managed independently. A single stripe is fine for -- many applications, and probably what you should choose by default. -- More stripes will lead to reduced contention in high-performance -- multicore applications, at a trade-off of causing the maximum -- number of simultaneous resources in use to grow. module Data.Pool ( Pool(idleTime, maxResources, numStripes) , LocalPool , createPool , withResource , takeResource , tryWithResource , tryTakeResource , destroyResource , putResource , destroyAllResources ) where import Control.Applicative ((<$>)) import Control.Concurrent (ThreadId, forkIOWithUnmask, killThread, myThreadId, threadDelay) import Control.Concurrent.STM import Control.Exception (SomeException, onException, mask_) import Control.Monad (forM_, forever, join, liftM3, unless, when) import Data.Hashable (hash) import Data.IORef (IORef, newIORef, mkWeakIORef) import Data.List (partition) import Data.Time.Clock (NominalDiffTime, UTCTime, diffUTCTime, getCurrentTime) import Data.Typeable (Typeable) import GHC.Conc.Sync (labelThread) import qualified Control.Exception as E import qualified Data.Vector as V #if MIN_VERSION_monad_control(0,3,0) import Control.Monad.Trans.Control (MonadBaseControl, control) import Control.Monad.Base (liftBase) #else import Control.Monad.IO.Control (MonadControlIO, controlIO) import Control.Monad.IO.Class (liftIO) #define control controlIO #define liftBase liftIO #endif #if MIN_VERSION_base(4,3,0) import Control.Exception (mask) #else -- Don't do any async exception protection for older GHCs. mask :: ((forall a. IO a -> IO a) -> IO b) -> IO b mask f = f id #endif -- | A single resource pool entry. data Entry a = Entry { entry :: a , lastUse :: UTCTime -- ^ Time of last return. } -- | A single striped pool. data LocalPool a = LocalPool { inUse :: TVar Int -- ^ Count of open entries (both idle and in use). , entries :: TVar [Entry a] -- ^ Idle entries. , lfin :: IORef () -- ^ empty value used to attach a finalizer to (internal) } deriving (Typeable) data Pool a = Pool { create :: IO a -- ^ Action for creating a new entry to add to the pool. , destroy :: a -> IO () -- ^ Action for destroying an entry that is now done with. , numStripes :: Int -- ^ The number of stripes (distinct sub-pools) to maintain. -- The smallest acceptable value is 1. , idleTime :: NominalDiffTime -- ^ Amount of time for which an unused resource is kept alive. -- The smallest acceptable value is 0.5 seconds. -- -- The elapsed time before closing may be a little longer than -- requested, as the reaper thread wakes at 1-second intervals. , maxResources :: Int -- ^ Maximum number of resources to maintain 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. , localPools :: V.Vector (LocalPool a) -- ^ Per-capability resource pools. , fin :: IORef () -- ^ empty value used to attach a finalizer to (internal) } deriving (Typeable) instance Show (Pool a) where show Pool{..} = "Pool {numStripes = " ++ show numStripes ++ ", " ++ "idleTime = " ++ show idleTime ++ ", " ++ "maxResources = " ++ show maxResources ++ "}" -- | Create a striped resource pool. -- -- Although the garbage collector will destroy all idle resources when -- the pool is garbage collected it's recommended to manually -- 'destroyAllResources' when you're done with the pool so that the -- resources are freed up as soon as possible. createPool :: IO a -- ^ Action that creates a new resource. -> (a -> IO ()) -- ^ Action that destroys an existing resource. -> Int -- ^ The number of stripes (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) createPool create destroy numStripes idleTime maxResources = do when (numStripes < 1) $ modError "pool " $ "invalid stripe count " ++ show numStripes when (idleTime < 0.5) $ modError "pool " $ "invalid idle time " ++ show idleTime when (maxResources < 1) $ modError "pool " $ "invalid maximum resource count " ++ show maxResources localPools <- V.replicateM numStripes $ liftM3 LocalPool (newTVarIO 0) (newTVarIO []) (newIORef ()) reaperId <- forkIOLabeledWithUnmask "resource-pool: reaper" $ \unmask -> unmask $ reaper destroy idleTime localPools fin <- newIORef () let p = Pool { create , destroy , numStripes , idleTime , maxResources , localPools , fin } mkWeakIORef fin (killThread reaperId) >> V.mapM_ (\lp -> mkWeakIORef (lfin lp) (purgeLocalPool destroy lp)) localPools return p -- TODO: Propose 'forkIOLabeledWithUnmask' for the base library. -- | Sparks off a new thread using 'forkIOWithUnmask' to run the given -- IO computation, but first labels the thread with the given label -- (using 'labelThread'). -- -- The implementation makes sure that asynchronous exceptions are -- masked until the given computation is executed. This ensures the -- thread will always be labeled which guarantees you can always -- easily find it in the GHC event log. -- -- Like 'forkIOWithUnmask', the given computation is given a function -- to unmask asynchronous exceptions. See the documentation of that -- function for the motivation of this. -- -- Returns the 'ThreadId' of the newly created thread. forkIOLabeledWithUnmask :: String -> ((forall a. IO a -> IO a) -> IO ()) -> IO ThreadId forkIOLabeledWithUnmask label m = mask_ $ forkIOWithUnmask $ \unmask -> do tid <- myThreadId labelThread tid label m unmask -- | Periodically go through all pools, closing any resources that -- have been left idle for too long. reaper :: (a -> IO ()) -> NominalDiffTime -> V.Vector (LocalPool a) -> IO () reaper destroy idleTime pools = forever $ do threadDelay (1 * 1000000) now <- getCurrentTime let isStale Entry{..} = now `diffUTCTime` lastUse > idleTime V.forM_ pools $ \LocalPool{..} -> do resources <- atomically $ do (stale,fresh) <- partition isStale <$> readTVar entries unless (null stale) $ do writeTVar entries fresh modifyTVar_ inUse (subtract (length stale)) return (map entry stale) forM_ resources $ \resource -> do destroy resource `E.catch` \(_::SomeException) -> return () -- | Destroy all idle resources of the given 'LocalPool' and remove them from -- the pool. purgeLocalPool :: (a -> IO ()) -> LocalPool a -> IO () purgeLocalPool destroy LocalPool{..} = do resources <- atomically $ do idle <- swapTVar entries [] modifyTVar_ inUse (subtract (length idle)) return (map entry idle) forM_ resources $ \resource -> destroy resource `E.catch` \(_::SomeException) -> return () -- | 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. withResource :: #if MIN_VERSION_monad_control(0,3,0) (MonadBaseControl IO m) #else (MonadControlIO m) #endif => Pool a -> (a -> m b) -> m b {-# SPECIALIZE withResource :: Pool a -> (a -> IO b) -> IO b #-} withResource pool act = control $ \runInIO -> mask $ \restore -> do (resource, local) <- takeResource pool ret <- restore (runInIO (act resource)) `onException` destroyResource pool local resource putResource local resource return ret #if __GLASGOW_HASKELL__ >= 700 {-# INLINABLE withResource #-} #endif -- | Take a resource from the pool, following the same results as -- 'withResource'. Note that this function should be used with caution, as -- improper exception handling can lead to leaked resources. -- -- This function returns both a resource and the @LocalPool@ it came from so -- that it may either be destroyed (via 'destroyResource') or returned to the -- pool (via 'putResource'). takeResource :: Pool a -> IO (a, LocalPool a) takeResource pool@Pool{..} = do local@LocalPool{..} <- getLocalPool pool resource <- liftBase . join . atomically $ do ents <- readTVar entries case ents of (Entry{..}:es) -> writeTVar entries es >> return (return entry) [] -> do used <- readTVar inUse when (used == maxResources) retry writeTVar inUse $! used + 1 return $ create `onException` atomically (modifyTVar_ inUse (subtract 1)) return (resource, local) #if __GLASGOW_HASKELL__ >= 700 {-# INLINABLE takeResource #-} #endif -- | Similar to 'withResource', but only performs the action if a resource could -- be taken from the pool /without blocking/. Otherwise, 'tryWithResource' -- returns immediately with 'Nothing' (ie. the action function is /not/ called). -- Conversely, if a resource can be borrowed from the pool without blocking, the -- action is performed and it's result is returned, wrapped in a 'Just'. tryWithResource :: forall m a b. #if MIN_VERSION_monad_control(0,3,0) (MonadBaseControl IO m) #else (MonadControlIO m) #endif => Pool a -> (a -> m b) -> m (Maybe b) tryWithResource pool act = control $ \runInIO -> mask $ \restore -> do res <- tryTakeResource pool case res of Just (resource, local) -> do ret <- restore (runInIO (Just <$> act resource)) `onException` destroyResource pool local resource putResource local resource return ret Nothing -> restore . runInIO $ return (Nothing :: Maybe b) #if __GLASGOW_HASKELL__ >= 700 {-# INLINABLE tryWithResource #-} #endif -- | A non-blocking version of 'takeResource'. The 'tryTakeResource' function -- returns immediately, with 'Nothing' if the pool is exhausted, or @'Just' (a, -- 'LocalPool' a)@ if a resource could be borrowed from the pool successfully. tryTakeResource :: Pool a -> IO (Maybe (a, LocalPool a)) tryTakeResource pool@Pool{..} = do local@LocalPool{..} <- getLocalPool pool resource <- liftBase . join . atomically $ do ents <- readTVar entries case ents of (Entry{..}:es) -> writeTVar entries es >> return (return . Just $ entry) [] -> do used <- readTVar inUse if used == maxResources then return (return Nothing) else do writeTVar inUse $! used + 1 return $ Just <$> create `onException` atomically (modifyTVar_ inUse (subtract 1)) return $ (flip (,) local) <$> resource #if __GLASGOW_HASKELL__ >= 700 {-# INLINABLE tryTakeResource #-} #endif -- | Get a (Thread-)'LocalPool' -- -- Internal, just to not repeat code for 'takeResource' and 'tryTakeResource' getLocalPool :: Pool a -> IO (LocalPool a) getLocalPool Pool{..} = do i <- liftBase $ ((`mod` numStripes) . hash) <$> myThreadId return $ localPools V.! i #if __GLASGOW_HASKELL__ >= 700 {-# INLINABLE getLocalPool #-} #endif -- | Destroy a resource. Note that this will ignore any exceptions in the -- destroy function. destroyResource :: Pool a -> LocalPool a -> a -> IO () destroyResource Pool{..} LocalPool{..} resource = do destroy resource `E.catch` \(_::SomeException) -> return () atomically (modifyTVar_ inUse (subtract 1)) #if __GLASGOW_HASKELL__ >= 700 {-# INLINABLE destroyResource #-} #endif -- | Return a resource to the given 'LocalPool'. putResource :: LocalPool a -> a -> IO () putResource LocalPool{..} resource = do now <- getCurrentTime atomically $ modifyTVar_ entries (Entry resource now:) #if __GLASGOW_HASKELL__ >= 700 {-# INLINABLE putResource #-} #endif -- | 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. destroyAllResources :: Pool a -> IO () destroyAllResources Pool{..} = V.forM_ localPools $ purgeLocalPool destroy modifyTVar_ :: TVar a -> (a -> a) -> STM () modifyTVar_ v f = readTVar v >>= \a -> writeTVar v $! f a modError :: String -> String -> a modError func msg = error $ "Data.Pool." ++ func ++ ": " ++ msg