Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adds refresh logic when one connection is expired (#689) #725

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

33 changes: 30 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 (logError)

type Transactionable m = Transactionable' SelectSqlDB m

Expand Down Expand Up @@ -76,7 +79,10 @@ runTransactionIO logEnv dbEnv (SqlDB run) = do
SqlDBEnv
{ currentTime = now
}
runLoggerIO logEnv $ runSqlPool (runReaderT run sqlDBEnv) dbEnv.connPool
runLoggerIO logEnv
. withLogTag "DB QUERY"
. destroyAllConnAndRetryIfExpired dbEnv
$ runSqlPool (runReaderT run sqlDBEnv) dbEnv.connPool

runInReplica :: (EsqDBReplicaFlow m r, MonadThrow m, Log m) => SelectSqlDB a -> m a
runInReplica (SelectSqlDB m) = do
Expand Down Expand Up @@ -104,4 +110,25 @@ runNoTransactionIO logEnv dbEnv (SqlDB run) = do
SqlDBEnv
{ currentTime = now
}
runLoggerIO logEnv $ runSqlPoolNoTransaction (runReaderT run sqlDBEnv) dbEnv.connPool Nothing
runLoggerIO logEnv
. withLogTag "DB QUERY NO TRANSACTION"
. destroyAllConnAndRetryIfExpired dbEnv
$ runSqlPoolNoTransaction (runReaderT run sqlDBEnv) dbEnv.connPool Nothing

destroyAllConnAndRetryIfExpired :: EsqDBEnv -> LoggerIO a -> LoggerIO a
destroyAllConnAndRetryIfExpired dbEnv action =
catch @_ @SomeException action $ \e -> do
let res = transformException e
logError $ "Transformed ERROR response: " <> show res
case res of
ET.DBError (ET.SQLError (ET.PostgresError (ET.PostgresSqlError "" ET.PostgresFatalError "" "" ""))) _ -> do
liftIO $ DP.destroyAllResources dbEnv.connPool
action
_ -> throwIO e

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