graphql-engine/server/src-lib/Database/MSSQL/Pool.hs
Rakesh Emmadi 5666161ac9 server/multitenant: resize sources' connection pools when a cloud project is scaled, global connection pooling
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5708
Co-authored-by: Naveen Naidu <30195193+Naveenaidu@users.noreply.github.com>
Co-authored-by: pranshi06 <85474619+pranshi06@users.noreply.github.com>
Co-authored-by: Puru Gupta <32328846+purugupta99@users.noreply.github.com>
Co-authored-by: Rikin Kachhia <54616969+rikinsk@users.noreply.github.com>
Co-authored-by: Anon Ray <616387+ecthiender@users.noreply.github.com>
GitOrigin-RevId: 513d497548d89b397d4a299355b11607daec3c7e
2022-10-17 08:06:12 +00:00

80 lines
2.1 KiB
Haskell

-- | MSSQL Connection Pooling
module Database.MSSQL.Pool
( -- * Types
ConnectionString (..),
ConnectionOptions (..),
MSSQLPool (..),
-- * Functions
initMSSQLPool,
drainMSSQLPool,
withMSSQLPool,
resizePool,
getInUseConnections,
)
where
import Autodocodec (HasCodec (codec), dimapCodec)
import Control.Exception.Lifted
import Control.Monad.Trans.Control
import Data.Aeson
import Data.Pool qualified as Pool
import Database.ODBC.SQLServer qualified as ODBC
import Hasura.Prelude (Generic, Text)
import Prelude
-- | ODBC connection string for MSSQL server
newtype ConnectionString = ConnectionString {unConnectionString :: Text}
deriving (Show, Eq, ToJSON, FromJSON, Generic)
instance HasCodec ConnectionString where
codec = dimapCodec ConnectionString unConnectionString codec
data ConnectionOptions = ConnectionOptions
{ _coConnections :: Int,
_coStripes :: Int,
_coIdleTime :: Int
}
deriving (Show, Eq)
-- | ODBC connection pool
newtype MSSQLPool = MSSQLPool (Pool.Pool ODBC.Connection)
-- | Initialize an MSSQL pool with given connection configuration
initMSSQLPool ::
ConnectionString ->
ConnectionOptions ->
IO MSSQLPool
initMSSQLPool (ConnectionString connString) ConnectionOptions {..} = do
MSSQLPool
<$> Pool.createPool
(ODBC.connect connString)
ODBC.close
_coStripes
(fromIntegral _coIdleTime)
_coConnections
-- | Destroy all pool resources
drainMSSQLPool :: MSSQLPool -> IO ()
drainMSSQLPool (MSSQLPool pool) =
Pool.destroyAllResources pool
withMSSQLPool ::
(MonadBaseControl IO m) =>
MSSQLPool ->
(ODBC.Connection -> m a) ->
m (Either ODBC.ODBCException a)
withMSSQLPool (MSSQLPool pool) action = do
try $ Pool.withResource pool action
-- | Resize a pool
resizePool :: MSSQLPool -> Int -> IO ()
resizePool (MSSQLPool pool) resizeTo = do
-- Resize the pool max resources
Pool.resizePool pool resizeTo
-- Trim pool by destroying excess resources, if any
Pool.tryTrimPool pool
getInUseConnections :: MSSQLPool -> IO Int
getInUseConnections (MSSQLPool pool) = Pool.getInUseResourceCount $ pool