Skip to content

Commit 80f7be6

Browse files
committed
Merge branch 'master' into db-messages
2 parents 6c66cf3 + 8372124 commit 80f7be6

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/Simplex/Messaging/Agent/Store/Postgres.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ connectPostgresStore DBOpts {connstr, schema, poolSize, createSchema} = do
5656
dbPriorityPool <- newDBStorePool poolSize
5757
dbPool <- newDBStorePool poolSize
5858
dbClosed <- newTVarIO True
59-
let st = DBStore {dbConnstr = connstr, dbSchema = schema, dbPoolSize = fromIntegral poolSize, dbPriorityPool, dbPool, dbNew = False, dbClosed}
59+
let dbConnect = fst <$> connectDB connstr schema False
60+
st = DBStore {dbConnstr = connstr, dbSchema = schema, dbPoolSize = fromIntegral poolSize, dbPriorityPool, dbPool, dbConnect, dbNew = False, dbClosed}
6061
dbNew <- connectStore st createSchema
6162
pure st {dbNew}
6263

src/Simplex/Messaging/Agent/Store/Postgres/Common.hs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE NamedFieldPuns #-}
33
{-# LANGUAGE OverloadedStrings #-}
44
{-# LANGUAGE QuasiQuotes #-}
5+
{-# LANGUAGE ScopedTypeVariables #-}
56
{-# LANGUAGE TupleSections #-}
67

78
module Simplex.Messaging.Agent.Store.Postgres.Common
@@ -19,7 +20,7 @@ where
1920

2021
import Control.Concurrent.MVar
2122
import Control.Concurrent.STM
22-
import Control.Exception (bracket)
23+
import qualified Control.Exception as E
2324
import Data.ByteString (ByteString)
2425
import qualified Database.PostgreSQL.Simple as PSQL
2526
import Numeric.Natural (Natural)
@@ -32,11 +33,7 @@ data DBStore = DBStore
3233
dbPoolSize :: Int,
3334
dbPriorityPool :: DBStorePool,
3435
dbPool :: DBStorePool,
35-
-- dbPoolSize :: Int,
36-
-- dbPool :: TBQueue PSQL.Connection,
37-
-- -- MVar is needed for fair pool distribution, without STM retry contention.
38-
-- -- Only one thread can be blocked on STM read.
39-
-- dbSem :: MVar (),
36+
dbConnect :: IO PSQL.Connection,
4037
dbClosed :: TVar Bool,
4138
dbNew :: Bool
4239
}
@@ -55,15 +52,23 @@ data DBStorePool = DBStorePool
5552
}
5653

5754
withConnectionPriority :: DBStore -> Bool -> (PSQL.Connection -> IO a) -> IO a
58-
withConnectionPriority DBStore {dbPriorityPool, dbPool} priority =
59-
withConnectionPool $ if priority then dbPriorityPool else dbPool
55+
withConnectionPriority DBStore {dbPriorityPool, dbPool, dbConnect} priority =
56+
withConnectionPool (if priority then dbPriorityPool else dbPool) dbConnect
6057
{-# INLINE withConnectionPriority #-}
6158

62-
withConnectionPool :: DBStorePool -> (PSQL.Connection -> IO a) -> IO a
63-
withConnectionPool DBStorePool {dbPoolConns, dbSem} =
64-
bracket
65-
(withMVar dbSem $ \_ -> atomically $ readTBQueue dbPoolConns)
66-
(atomically . writeTBQueue dbPoolConns)
59+
withConnectionPool :: DBStorePool -> IO PSQL.Connection -> (PSQL.Connection -> IO a) -> IO a
60+
withConnectionPool DBStorePool {dbPoolConns, dbSem} dbConnect action =
61+
E.mask $ \restore -> do
62+
conn <- withMVar dbSem $ \_ -> atomically $ readTBQueue dbPoolConns
63+
r <- restore (action conn) `E.onException` reset conn
64+
atomically $ writeTBQueue dbPoolConns conn
65+
pure r
66+
where
67+
reset conn = do
68+
conn' <- E.try dbConnect >>= \case
69+
Right conn' -> PSQL.close conn >> pure conn'
70+
Left (_ :: E.SomeException) -> pure conn
71+
atomically $ writeTBQueue dbPoolConns conn'
6772

6873
withConnection :: DBStore -> (PSQL.Connection -> IO a) -> IO a
6974
withConnection st = withConnectionPriority st False

0 commit comments

Comments
 (0)