module Hasql.Pool
(
Pool,
Settings(..),
acquire,
release,
UsageError(..),
use,
)
where
import Hasql.Pool.Prelude
import qualified Hasql.Connection
import qualified Hasql.Session
import qualified Data.Pool as ResourcePool
import qualified Hasql.Pool.ResourcePool as ResourcePool
newtype Pool =
Pool (ResourcePool.Pool (Either Hasql.Connection.ConnectionError Hasql.Connection.Connection))
deriving (Show)
type Settings =
(Int, NominalDiffTime, Hasql.Connection.Settings)
acquire :: Settings -> IO Pool
acquire (size, timeout, connectionSettings) =
fmap Pool $
ResourcePool.createPool acquire release stripes timeout size
where
acquire =
Hasql.Connection.acquire connectionSettings
release =
either (const (pure ())) Hasql.Connection.release
stripes =
1
release :: Pool -> IO ()
release (Pool pool) =
ResourcePool.destroyAllResources pool
data UsageError =
ConnectionError Hasql.Connection.ConnectionError |
SessionError Hasql.Session.QueryError
deriving (Show, Eq)
use :: Pool -> Hasql.Session.Session a -> IO (Either UsageError a)
use (Pool pool) session =
fmap (either (Left . ConnectionError) (either (Left . SessionError) Right)) $
ResourcePool.withResourceOnEither pool $
traverse $
Hasql.Session.run session