-
I'm trying to use runTransactionsWithConnection ::
forall es a.
IOE :> es =>
Hasql.Connection ->
Eff (DB : es) a ->
Eff es a
runTransactionsWithConnection connection m = do
rngState <- newCryptoRNGState
reinterpret (runFailIO . runCryptoRNG rngState . runStatementsWithConn connection) interpreter m
where
interpreter
:: (IOE :> handlerEs, Statement :> localEs, RNG :> localEs, Fail :> localEs)
=> LocalEnv localEs handlerEs -> DB (Eff localEs) b -> Eff handlerEs b
interpreter env (RunTransaction transaction) = localSeqUnliftIO env \runInIO -> do
-- This is essentially
-- Database.PostgreSQL.Simple.Transaction.withTransactionModeRetry',
-- but we use recovering with a fullJitterBackoff RetryPolicy, rather
-- than retrying transactions as fast as possible. We do this as
-- serialization errors occur when multiple transactions try read/write
-- common tables. Having these transactions race as fast as possible
-- doesn't reduce the likelihood of another serialization error, hence
-- adding a bit of delay.
inTransactionSpan $
monitorSerialisationFailures $
recovering retryPolicy [retryOnSerializationError] \_ ->
bracket_ monitorStart monitorEnd do
either throwIO return =<< flip Hasql.run connection do
Hasql.statement () $
Hasql.Statement
"BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE"
Hasql.noParams
Hasql.noResult
False
res <- do
runInIO (inject (unTransaction transaction))
`onException` Prometheus.incCounter transactionsExceptionsTotal
`onException` liftIO rollback_
either throwIO return =<< do
flip Hasql.run connection do
Hasql.statement () $
Hasql.Statement
"COMMIT"
Hasql.noParams
Hasql.noResult
False
return res But this gives me the error:
What am I doing wrong? I have a feeling I might need to use My effect definitions are: data DB :: Effect where
RunTransaction :: Transaction a -> DB m a
newtype Transaction a = Transaction {unTransaction :: Eff '[Statement, RNG, Fail] a}
deriving newtype (Functor, Applicative, Monad, MonadStatement, MonadCatch, MonadThrow, MonadFail, CryptoRNG, MonadRandom)
data Statement :: Effect where
RunStatement :: Hasql.Statement () a -> Statement m a |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I note that changing |
Beta Was this translation helpful? Give feedback.
-
Hmm, that looks complicated. Do you have a standalone piece of code I can compile? One thing I can see is that the type signature
Looks a bit suspect. Context of localEs is supplied from outside and by default will only have |
Beta Was this translation helpful? Give feedback.
Ah ok, I see it now.
I got rid of the irrelevant bits and this compiles: