{-# LANGUAGE
DeriveFunctor
, FlexibleContexts
, FlexibleInstances
, InstanceSigs
, MultiParamTypeClasses
, PolyKinds
, RankNTypes
, ScopedTypeVariables
, TypeFamilies
, TypeInType
, UndecidableInstances
#-}
module Squeal.PostgreSQL.Session.Pool
(
Pool
, createConnectionPool
, usingConnectionPool
, destroyConnectionPool
) where
import Control.Monad.Catch
import Control.Monad.IO.Class
import Data.ByteString
import Data.Time
import Data.Pool
import Squeal.PostgreSQL.Type.Schema
import Squeal.PostgreSQL.Session (PQ (..))
import Squeal.PostgreSQL.Session.Connection
createConnectionPool
:: forall (db :: SchemasType) io. MonadIO io
=> ByteString
-> Int
-> NominalDiffTime
-> Int
-> io (Pool (K Connection db))
createConnectionPool :: ByteString
-> Int -> NominalDiffTime -> Int -> io (Pool (K Connection db))
createConnectionPool ByteString
conninfo Int
stripes NominalDiffTime
idle Int
maxResrc =
IO (Pool (K Connection db)) -> io (Pool (K Connection db))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Pool (K Connection db)) -> io (Pool (K Connection db)))
-> IO (Pool (K Connection db)) -> io (Pool (K Connection db))
forall a b. (a -> b) -> a -> b
$ IO (K Connection db)
-> (K Connection db -> IO ())
-> Int
-> NominalDiffTime
-> Int
-> IO (Pool (K Connection db))
forall a.
IO a
-> (a -> IO ()) -> Int -> NominalDiffTime -> Int -> IO (Pool a)
createPool (ByteString -> IO (K Connection db)
forall (db :: SchemasType) (io :: * -> *).
MonadIO io =>
ByteString -> io (K Connection db)
connectdb ByteString
conninfo) K Connection db -> IO ()
forall k (io :: * -> *) (db :: k).
MonadIO io =>
K Connection db -> io ()
finish Int
stripes NominalDiffTime
idle Int
maxResrc
usingConnectionPool
:: (MonadIO io, MonadMask io)
=> Pool (K Connection db)
-> PQ db db io x
-> io x
usingConnectionPool :: Pool (K Connection db) -> PQ db db io x -> io x
usingConnectionPool Pool (K Connection db)
pool (PQ K Connection db -> io (K x db)
session) = ((forall a. io a -> io a) -> io x) -> io x
forall (m :: * -> *) b.
MonadMask m =>
((forall a. m a -> m a) -> m b) -> m b
mask (((forall a. io a -> io a) -> io x) -> io x)
-> ((forall a. io a -> io a) -> io x) -> io x
forall a b. (a -> b) -> a -> b
$ \forall a. io a -> io a
restore -> do
(K Connection db
conn, LocalPool (K Connection db)
local) <- IO (K Connection db, LocalPool (K Connection db))
-> io (K Connection db, LocalPool (K Connection db))
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (K Connection db, LocalPool (K Connection db))
-> io (K Connection db, LocalPool (K Connection db)))
-> IO (K Connection db, LocalPool (K Connection db))
-> io (K Connection db, LocalPool (K Connection db))
forall a b. (a -> b) -> a -> b
$ Pool (K Connection db)
-> IO (K Connection db, LocalPool (K Connection db))
forall a. Pool a -> IO (a, LocalPool a)
takeResource Pool (K Connection db)
pool
K x db
ret <- io (K x db) -> io (K x db)
forall a. io a -> io a
restore (K Connection db -> io (K x db)
session K Connection db
conn) io (K x db) -> io () -> io (K x db)
forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`onException`
IO () -> io ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Pool (K Connection db)
-> LocalPool (K Connection db) -> K Connection db -> IO ()
forall a. Pool a -> LocalPool a -> a -> IO ()
destroyResource Pool (K Connection db)
pool LocalPool (K Connection db)
local K Connection db
conn)
IO () -> io ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> io ()) -> IO () -> io ()
forall a b. (a -> b) -> a -> b
$ LocalPool (K Connection db) -> K Connection db -> IO ()
forall a. LocalPool a -> a -> IO ()
putResource LocalPool (K Connection db)
local K Connection db
conn
x -> io x
forall (m :: * -> *) a. Monad m => a -> m a
return (x -> io x) -> x -> io x
forall a b. (a -> b) -> a -> b
$ K x db -> x
forall k a (b :: k). K a b -> a
unK K x db
ret
destroyConnectionPool
:: MonadIO io
=> Pool (K Connection db)
-> io ()
destroyConnectionPool :: Pool (K Connection db) -> io ()
destroyConnectionPool = IO () -> io ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> io ())
-> (Pool (K Connection db) -> IO ())
-> Pool (K Connection db)
-> io ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pool (K Connection db) -> IO ()
forall a. Pool a -> IO ()
destroyAllResources