Skip to content

Commit

Permalink
fix: adds refresh logic when one connection is expired
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypalmudaliyar24 committed Dec 17, 2024
1 parent 5a30ab7 commit 91f0125
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 37 additions & 3 deletions lib/mobility-core/src/Kernel/Storage/Esqueleto/Transactionable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@

module Kernel.Storage.Esqueleto.Transactionable where

import qualified Data.Pool as DP
import Database.Esqueleto.Experimental (runSqlPool)
import Database.Persist.Postgresql (runSqlPoolNoTransaction)
import Kernel.Prelude
import qualified EulerHS.Types as ET
import Kernel.Prelude hiding (either)
import Kernel.Storage.Esqueleto.Config
import Kernel.Storage.Esqueleto.DTypeBuilder
import Kernel.Storage.Esqueleto.Logger
import Kernel.Storage.Esqueleto.SqlDB
import Kernel.Types.Logging
import Kernel.Types.Time (getCurrentTime)
import Kernel.Utils.IOLogging (LoggerEnv)
import Kernel.Utils.Logging (logTagError)

type Transactionable m = Transactionable' SelectSqlDB m

Expand Down Expand Up @@ -76,7 +79,16 @@ runTransactionIO logEnv dbEnv (SqlDB run) = do
SqlDBEnv
{ currentTime = now
}
runLoggerIO logEnv $ runSqlPool (runReaderT run sqlDBEnv) dbEnv.connPool
runLoggerIO logEnv $ do
res <- try @_ @SomeException $ runSqlPool (runReaderT run sqlDBEnv) dbEnv.connPool
fromEitherOrM res $ \e -> do
let res' = transformException e
logTagError "DB QUERY" $ "Transformed ERROR response: " <> show res'
case res' of
ET.DBError (ET.SQLError (ET.PostgresError (ET.PostgresSqlError "" ET.PostgresFatalError "" "" ""))) _ -> do
liftIO $ DP.destroyAllResources dbEnv.connPool
runSqlPool (runReaderT run sqlDBEnv) dbEnv.connPool
_ -> throwIO e

runInReplica :: (EsqDBReplicaFlow m r, MonadThrow m, Log m) => SelectSqlDB a -> m a
runInReplica (SelectSqlDB m) = do
Expand Down Expand Up @@ -104,4 +116,26 @@ runNoTransactionIO logEnv dbEnv (SqlDB run) = do
SqlDBEnv
{ currentTime = now
}
runLoggerIO logEnv $ runSqlPoolNoTransaction (runReaderT run sqlDBEnv) dbEnv.connPool Nothing
runLoggerIO logEnv $ do
res <- try @_ @SomeException $ runSqlPoolNoTransaction (runReaderT run sqlDBEnv) dbEnv.connPool Nothing
fromEitherOrM res $ \e -> do
let res' = transformException e
logTagError "DB QUERY" $ "Transformed ERROR response: " <> show res'
case res' of
ET.DBError (ET.SQLError (ET.PostgresError (ET.PostgresSqlError "" ET.PostgresFatalError "" "" ""))) _ -> do
liftIO $ DP.destroyAllResources dbEnv.connPool
runSqlPoolNoTransaction (runReaderT run sqlDBEnv) dbEnv.connPool Nothing
_ -> throwIO e

transformException :: SomeException -> ET.DBError
transformException e =
maybe
(ET.DBError ET.UnrecognizedError $ show e)
(ET.postgresErrorToDbError (show e))
$ fromException e

fromEitherOrM :: Monad m => Either e a -> (e -> m a) -> m a
fromEitherOrM either f =
case either of
Right x -> pure x
Left e -> f e

0 comments on commit 91f0125

Please sign in to comment.