Skip to content

Commit ddfcb64

Browse files
committed
Add support for assigning a label to the pool
1 parent db69d3b commit ddfcb64

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Change the default number of stripes to 1.
1010
* Do not exceed the maximum number of resources if the number of stripes does
1111
not divide it.
12+
* Add support for assigning a label to the pool.
1213

1314
# resource-pool-0.4.0.0 (2023-01-16)
1415
* Require `poolMaxResources` to be not smaller than the number of stripes.

resource-pool.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ library
3636
, hashable >= 1.1.0.0
3737
, primitive >= 0.7
3838
, stm
39+
, text
3940
, time
4041

4142
ghc-options: -Wall

src/Data/Pool.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Data.Pool
1010
, PoolConfig
1111
, defaultPoolConfig
1212
, setNumStripes
13+
, setPoolLabel
1314

1415
-- * Resource management
1516
, withResource
@@ -27,6 +28,7 @@ module Data.Pool
2728
import Control.Concurrent.STM
2829
import Control.Exception
2930
import Control.Monad
31+
import Data.Text qualified as T
3032
import Data.Time (NominalDiffTime)
3133

3234
import Data.Pool.Internal
@@ -116,6 +118,7 @@ createPool create free numStripes idleTime maxResources =
116118
, poolCacheTTL = realToFrac idleTime
117119
, poolMaxResources = numStripes * maxResources
118120
, poolNumStripes = Just numStripes
121+
, pcLabel = T.empty
119122
}
120123

121124
----------------------------------------

src/Data/Pool/Internal.hs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import Data.Either
1414
import Data.Hashable (hash)
1515
import Data.IORef
1616
import Data.List qualified as L
17+
import Data.Text qualified as T
1718
import Data.Primitive.SmallArray
1819
import GHC.Clock (getMonotonicTime)
19-
import GHC.Conc (unsafeIOToSTM)
20+
import GHC.Conc (labelThread, unsafeIOToSTM)
2021

2122
-- | Striped resource pool based on "Control.Concurrent.QSem".
2223
data Pool a = Pool
@@ -60,6 +61,7 @@ data PoolConfig a = PoolConfig
6061
, poolCacheTTL :: !Double
6162
, poolMaxResources :: !Int
6263
, poolNumStripes :: !(Maybe Int)
64+
, pcLabel :: !T.Text
6365
}
6466

6567
-- | Create a 'PoolConfig' with optional parameters having default values.
@@ -94,6 +96,7 @@ defaultPoolConfig create free cacheTTL maxResources =
9496
, poolCacheTTL = cacheTTL
9597
, poolMaxResources = maxResources
9698
, poolNumStripes = Just 1
99+
, pcLabel = T.empty
97100
}
98101

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

117+
-- | Assign a label to the pool.
118+
--
119+
-- The label will appear in a label of the collector thread as well as
120+
-- t'Data.Pool.Introspection.Resource'.
121+
--
122+
-- @since 0.5.0.0
123+
setPoolLabel :: T.Text -> PoolConfig a -> PoolConfig a
124+
setPoolLabel label pc = pc {pcLabel = label}
125+
114126
-- | Create a new striped resource pool.
115127
--
116128
-- /Note:/ although the runtime system will destroy all idle resources when the
@@ -149,7 +161,10 @@ newPool pc = do
149161
}
150162
mask_ $ do
151163
ref <- newIORef ()
152-
collectorA <- forkIOWithUnmask $ \unmask -> unmask $ collector pools
164+
collectorA <- forkIOWithUnmask $ \unmask -> unmask $ do
165+
tid <- myThreadId
166+
labelThread tid $ "resource-pool: collector (" ++ T.unpack (pcLabel pc) ++ ")"
167+
collector pools
153168
void . mkWeakIORef ref $ do
154169
-- When the pool goes out of scope, stop the collector. Resources existing
155170
-- in stripes will be taken care by their cleaners.

src/Data/Pool/Introspection.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Data.Pool.Introspection
99
, PoolConfig
1010
, defaultPoolConfig
1111
, setNumStripes
12+
, setPoolLabel
1213

1314
-- * Resource management
1415
, Resource (..)
@@ -25,6 +26,7 @@ module Data.Pool.Introspection
2526
import Control.Concurrent.STM
2627
import Control.Exception
2728
import Control.Monad
29+
import Data.Text qualified as T
2830
import GHC.Clock (getMonotonicTime)
2931
import GHC.Generics (Generic)
3032

@@ -33,6 +35,7 @@ import Data.Pool.Internal
3335
-- | A resource taken from the pool along with additional information.
3436
data Resource a = Resource
3537
{ resource :: a
38+
, poolLabel :: !T.Text
3639
, stripeNumber :: !Int
3740
, availableResources :: !Int
3841
, acquisition :: !Acquisition
@@ -75,6 +78,7 @@ takeResource pool = mask_ $ do
7578
let res =
7679
Resource
7780
{ resource = a
81+
, poolLabel = pcLabel $ poolConfig pool
7882
, stripeNumber = stripeId lp
7983
, availableResources = 0
8084
, acquisition = Delayed
@@ -89,6 +93,7 @@ takeResource pool = mask_ $ do
8993
let res =
9094
Resource
9195
{ resource = a
96+
, poolLabel = pcLabel $ poolConfig pool
9297
, stripeNumber = stripeId lp
9398
, availableResources = 0
9499
, acquisition = Delayed
@@ -143,6 +148,7 @@ takeAvailableResource pool t1 lp stripe = case cache stripe of
143148
let res =
144149
Resource
145150
{ resource = a
151+
, poolLabel = pcLabel $ poolConfig pool
146152
, stripeNumber = stripeId lp
147153
, availableResources = newAvailable
148154
, acquisition = Immediate
@@ -158,6 +164,7 @@ takeAvailableResource pool t1 lp stripe = case cache stripe of
158164
let res =
159165
Resource
160166
{ resource = a
167+
, poolLabel = pcLabel $ poolConfig pool
161168
, stripeNumber = stripeId lp
162169
, availableResources = newAvailable
163170
, acquisition = Immediate

0 commit comments

Comments
 (0)