scrive / pool

A high-performance striped resource pooling implementation for Haskell
Other
18 stars 11 forks source link

Can we add timeout for resource acquiring? #10

Open goosedb opened 2 years ago

goosedb commented 2 years ago

Can we add optional timeout for resource acquiring? Not for creating but taking resource. For now pool waits for available resource infinitely that can be a reason of dead locks.

main :: IO ()
main = do
  pool <- newPool PoolConfig 
    { createResource = pure 0
    , freeResource = const (pure ())
    , poolCacheTTL = 60
    , poolMaxResources = 3
    }

  let takeAnd action = withResource pool (const action)

  putStrLn "-------------\n 1st\n-------------"
  takeAnd do
    print 1
    takeAnd do
      print 2
      takeAnd do 
        print 3
        putStrLn "here!"

  putStrLn "-------------\n 2nd\n-------------"
  takeAnd do
    print 1
    takeAnd do
      print 2
      takeAnd do 
        print 3
        takeAnd do 
          print 4
          putStrLn "here!"

prints

-------------
 1st
-------------
1
2
3
here!
-------------
 2nd
-------------
1
2
3

And gets stuck. Of course, it's a user-code bug. But it's better to get exception than get stuck forever

I'm ready to implement it

goosedb commented 2 years ago

anybody @arybczak @soareschen ?

arybczak commented 2 years ago

Sounds good to me. You can add a new configuration option to PoolConfig (a Maybe would work best so that Nothing represents no timeout).

yitz-zoomin commented 1 year ago

This is important. When our app saturates the pool, requests start backing up, memory use explodes superlinearly, and the app crashes. With a timeout, we could fail gracefully.

arybczak commented 1 year ago

There's #11, but it's abandoned. Feel free to pick it up and make it up to date if you need it, so it can be merged :slightly_smiling_face: