Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Change the default number of stripes to 1.
* Do not exceed the maximum number of resources if the number of stripes does
not divide it.
* Add support for assigning a label to the pool.

# resource-pool-0.4.0.0 (2023-01-16)
* Require `poolMaxResources` to be not smaller than the number of stripes.
Expand Down
1 change: 1 addition & 0 deletions resource-pool.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ library
, hashable >= 1.1.0.0
, primitive >= 0.7
, stm
, text
, time

ghc-options: -Wall
Expand Down
3 changes: 3 additions & 0 deletions src/Data/Pool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Data.Pool
, PoolConfig
, defaultPoolConfig
, setNumStripes
, setPoolLabel

-- * Resource management
, withResource
Expand All @@ -27,6 +28,7 @@ module Data.Pool
import Control.Concurrent.STM
import Control.Exception
import Control.Monad
import Data.Text qualified as T
import Data.Time (NominalDiffTime)

import Data.Pool.Internal
Expand Down Expand Up @@ -116,6 +118,7 @@ createPool create free numStripes idleTime maxResources =
, poolCacheTTL = realToFrac idleTime
, poolMaxResources = numStripes * maxResources
, poolNumStripes = Just numStripes
, pcLabel = T.empty
}

----------------------------------------
Expand Down
19 changes: 17 additions & 2 deletions src/Data/Pool/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import Data.Hashable (hash)
import Data.IORef
import Data.List qualified as L
import Data.Primitive.SmallArray
import Data.Text qualified as T
import GHC.Clock (getMonotonicTime)
import GHC.Conc (unsafeIOToSTM)
import GHC.Conc (labelThread, unsafeIOToSTM)

-- | Striped resource pool based on "Control.Concurrent.QSem".
data Pool a = Pool
Expand Down Expand Up @@ -60,6 +61,7 @@ data PoolConfig a = PoolConfig
, poolCacheTTL :: !Double
, poolMaxResources :: !Int
, poolNumStripes :: !(Maybe Int)
, pcLabel :: !T.Text
}

-- | Create a 'PoolConfig' with optional parameters having default values.
Expand Down Expand Up @@ -94,6 +96,7 @@ defaultPoolConfig create free cacheTTL maxResources =
, poolCacheTTL = cacheTTL
, poolMaxResources = maxResources
, poolNumStripes = Just 1
, pcLabel = T.empty
}

-- | Set the number of stripes (sub-pools) in the pool.
Expand All @@ -111,6 +114,15 @@ defaultPoolConfig create free cacheTTL maxResources =
setNumStripes :: Maybe Int -> PoolConfig a -> PoolConfig a
setNumStripes numStripes pc = pc {poolNumStripes = numStripes}

-- | Assign a label to the pool.
--
-- The label will appear in a label of the collector thread as well as
-- t'Data.Pool.Introspection.Resource'.
--
-- @since 0.5.0.0
setPoolLabel :: T.Text -> PoolConfig a -> PoolConfig a
setPoolLabel label pc = pc {pcLabel = label}

-- | Create a new striped resource pool.
--
-- /Note:/ although the runtime system will destroy all idle resources when the
Expand Down Expand Up @@ -149,7 +161,10 @@ newPool pc = do
}
mask_ $ do
ref <- newIORef ()
collectorA <- forkIOWithUnmask $ \unmask -> unmask $ collector pools
collectorA <- forkIOWithUnmask $ \unmask -> unmask $ do
tid <- myThreadId
labelThread tid $ "resource-pool: collector (" ++ T.unpack (pcLabel pc) ++ ")"
collector pools
void . mkWeakIORef ref $ do
-- When the pool goes out of scope, stop the collector. Resources existing
-- in stripes will be taken care by their cleaners.
Expand Down
7 changes: 7 additions & 0 deletions src/Data/Pool/Introspection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Data.Pool.Introspection
, PoolConfig
, defaultPoolConfig
, setNumStripes
, setPoolLabel

-- * Resource management
, Resource (..)
Expand All @@ -25,6 +26,7 @@ module Data.Pool.Introspection
import Control.Concurrent.STM
import Control.Exception
import Control.Monad
import Data.Text qualified as T
import GHC.Clock (getMonotonicTime)
import GHC.Generics (Generic)

Expand All @@ -33,6 +35,7 @@ import Data.Pool.Internal
-- | A resource taken from the pool along with additional information.
data Resource a = Resource
{ resource :: a
, poolLabel :: !T.Text
, stripeNumber :: !Int
, availableResources :: !Int
, acquisition :: !Acquisition
Expand Down Expand Up @@ -75,6 +78,7 @@ takeResource pool = mask_ $ do
let res =
Resource
{ resource = a
, poolLabel = pcLabel $ poolConfig pool
, stripeNumber = stripeId lp
, availableResources = 0
, acquisition = Delayed
Expand All @@ -89,6 +93,7 @@ takeResource pool = mask_ $ do
let res =
Resource
{ resource = a
, poolLabel = pcLabel $ poolConfig pool
, stripeNumber = stripeId lp
, availableResources = 0
, acquisition = Delayed
Expand Down Expand Up @@ -143,6 +148,7 @@ takeAvailableResource pool t1 lp stripe = case cache stripe of
let res =
Resource
{ resource = a
, poolLabel = pcLabel $ poolConfig pool
, stripeNumber = stripeId lp
, availableResources = newAvailable
, acquisition = Immediate
Expand All @@ -158,6 +164,7 @@ takeAvailableResource pool t1 lp stripe = case cache stripe of
let res =
Resource
{ resource = a
, poolLabel = pcLabel $ poolConfig pool
, stripeNumber = stripeId lp
, availableResources = newAvailable
, acquisition = Immediate
Expand Down