hasql-pool 1.6.0.0 → 1.7.0.0
raw patch · API changes only · 7 files changed · hide whitespace changes · browse hasql-pool-1.7.0.0
+79/−40 lines, 7 filesdep ~hasqlPVP ok
Dependency ranges changed: hasql
API changes (from Hackage documentation)
0 removed, 3 added, 1 changed
version bump matches the API change (PVP)
+ Hasql.Pool.Observation: AcquireErrorConnectionTerminationReason :: AcquireError -> ConnectionTerminationReason
+ Hasql.Pool.Observation: ExceptionConnectionTerminationReason :: Text -> ConnectionTerminationReason
+ Hasql.Pool.Observation: SessionErrorConnectionTerminationReason :: SessionError -> ConnectionTerminationReason
- Hasql.Pool.Observation: NetworkErrorConnectionTerminationReason :: Maybe Text -> ConnectionTerminationReason
+ Hasql.Pool.Observation: NetworkErrorConnectionTerminationReason :: ConnectionError -> ConnectionTerminationReason
Files
- CHANGELOG.md +13/−0
- hasql-pool.cabal +3/−3
- src/integration-tests/Specs/ConnectionDiscardsSpec.hs +32/−10 · folded
- src/integration-tests/Specs/UseSpec.hs +6/−4 · folded
- src/library/Hasql/Pool.hs +7/−18
- src/library/Hasql/Pool/ConnectionDiscards.hs +2/−2
- src/library/Hasql/Pool/Observation.hs +16/−3
CHANGELOG.md view
@@ -1,3 +1,16 @@+# v1.7.0.0++## Breaking++- Migrate to `hasql-2.2`. `Errors.ConnectionUseError` now carries a structured `Errors.ConnectionError` instead of `Text`, and the pool's dependency bound moves to `hasql ^>=2.2` accordingly.+- `NetworkErrorConnectionTerminationReason` now carries `Errors.ConnectionError` instead of `Maybe Text`. Other termination diagnostics are also represented by their structured Hasql error type where available; exception details remain text.++## Fixes++- Connection termination observations now preserve structured acquisition, connection-use and fatal session errors, so callers can inspect SQLSTATE and diagnostic fields instead of parsing text. A `pg_terminate_backend` FATAL (`57P01`) is available through `Errors.toSqlState`.++- A connection lost before or during a receive is now classified as connection-fatal by hasql on every operation shape, where it previously could surface as a catchable session error. The pool's existing reactive disposal handles it: the entry is evicted and a later use establishes a replacement.+ # v1.6.0.0 ## Breaking
hasql-pool.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: hasql-pool-version: 1.6.0.0+version: 1.7.0.0 category: Hasql, Database, PostgreSQL synopsis: Pool of connections for Hasql homepage: https://github.com/nikita-volkov/hasql-pool@@ -86,7 +86,7 @@ build-depends: base >=4.11 && <5, bytestring >=0.10 && <0.14,- hasql ^>=2.1,+ hasql ^>=2.2, pqi >=1.0 && <1.2, stm >=2.5 && <3, text >=1.2 && <3,@@ -149,7 +149,7 @@ build-depends: base >=4.11 && <5, bytestring >=0.10 && <0.14,- hasql ^>=2.1,+ hasql ^>=2.2, hspec >=2.6 && <3, pqi >=1.0 && <1.2, stm >=2.5 && <3,
src/integration-tests/Specs/ConnectionDiscardsSpec.hs view
@@ -18,10 +18,13 @@ -- guaranteed to be either the same connection or its replacement. module Specs.ConnectionDiscardsSpec where +import Data.IORef qualified as IORef import Data.Text qualified as Text import Hasql.Decoders qualified as Decoders import Hasql.Encoders qualified as Encoders+import Hasql.Errors qualified as Errors import Hasql.Pool+import Hasql.Pool.Observation qualified as Observation import Hasql.Session qualified as Session import Hasql.Statement qualified as Statement import Helpers.Scripts qualified as Scripts@@ -80,18 +83,37 @@ shouldFailWithSessionError (use pool Sessions.selectOne) use pool Sessions.selectOne `shouldReturn` Right 1 - -- 57P01. Measured, not assumed: terminating the backend surfaces as a- -- 'Errors.ConnectionUseError' carrying the FATAL text under @pqi-ffi@ and- -- as a thrown exception under @pqi-native@ - in neither case as a server- -- error whose SQLSTATE reaches 'ConnectionDiscards.serverError'. The- -- @57P@ rule is therefore dead for this code, and is kept only because- -- the rest of the class has not been shown to behave the same way. What- -- this test asserts is the outcome all those paths share.- it "Recovers from a backend terminated underneath it" \scopeParams ->- onSingleConnectionPool scopeParams \pool -> do+ -- 57P01. Terminating the backend underneath a session is announced with a+ -- genuine FATAL error result, so hasql reports it as a+ -- 'Errors.ConnectionUseError' carrying an 'Errors.ServerConnectionError'+ -- with the SQLSTATE intact - never as a server error reaching+ -- 'ConnectionDiscards.serverError'. The @57P@ rule is therefore dead for+ -- this code, and is kept only because the rest of the class has not been+ -- shown to behave the same way. The pool must discard the connection and+ -- establish a replacement, which the size-1 pool makes observable. Its+ -- termination observation must preserve the structured server error too.+ it "Recovers from a backend terminated underneath it and observes its error" \scopeParams -> do+ observations <- IORef.newIORef []+ let observe observation =+ IORef.atomicModifyIORef' observations \recorded ->+ (observation : recorded, ())+ Scripts.onAutotaggedPoolWithObservationHandler 1 10 1_800 1_800 observe scopeParams \_ pool -> do use pool Sessions.selectOne `shouldReturn` Right 1- _ <- try @SomeException (use pool terminateSelf)+ terminated <- use pool terminateSelf+ terminated `shouldSatisfy` \case+ Left (SessionUsageError (Errors.ConnectionUseError connectionError)) ->+ Errors.toSqlState connectionError == Just "57P01"+ _ -> False use pool Sessions.selectOne `shouldReturn` Right 1+ recorded <- IORef.readIORef observations+ recorded `shouldSatisfy` any hasStructuredTerminationError++hasStructuredTerminationError :: Observation.Observation -> Bool+hasStructuredTerminationError = \case+ Observation.ConnectionObservation _ (Observation.TerminatedConnectionStatus (Observation.NetworkErrorConnectionTerminationReason (Errors.ServerConnectionError serverError))) ->+ Errors.toSqlState serverError == Just "57P01"+ _ ->+ False onSingleConnectionPool :: Scripts.ScopeParams -> (Pool -> IO ()) -> IO () onSingleConnectionPool scopeParams cont =
src/integration-tests/Specs/UseSpec.hs view
@@ -40,9 +40,11 @@ use pool (Sessions.getSetting settingName) `shouldReturn` Right Nothing -- After a server restart or @pg_terminate_backend@ the first statement on- -- the closed connection can fail with an 'Errors.UnexpectedRowCountStatementError'- -- instead of a 'Errors.ConnectionSessionError'. The pool must not hand the- -- connection out again.+ -- the closed connection used to fail with a catchable+ -- 'Errors.UnexpectedRowCountStatementError' or server error instead of a+ -- 'Errors.ConnectionUseError'; hasql now classifies the loss as+ -- connection-fatal on every receive. Either way the pool must not hand+ -- the connection out again. it "Connections closed by the server cause eviction of connection" \scopeParams -> Scripts.onAutotaggedPool 1 10 1_800 1_800 scopeParams \appName pool -> do use pool Sessions.selectOne `shouldReturn` Right 1@@ -228,7 +230,7 @@ driverError :: Session.Session () driverError = Session.onLibpqConnection \connection ->- pure (Left (Errors.ConnectionUseError "synthetic driver error"), connection)+ pure (Left (Errors.ConnectionUseError (Errors.LocalConnectionError "synthetic driver error")), connection) -- | Establish two connections, return the first before the second, and give -- their backend PIDs.
src/library/Hasql/Pool.hs view
@@ -236,11 +236,6 @@ Given reuseVar Nothing -> onSlot unmask reuseVar Given reuseVar (Just entry) -> onEntry unmask reuseVar entry where- optionalDetails details =- if details == ""- then Nothing- else Just details- enqueue :: Word64 -> STM Waiter enqueue now = do waiter <- Waiter (now + poolAcquisitionTimeout) <$> newEmptyTMVar <*> newTVar False@@ -312,13 +307,7 @@ unmask $ poolObserver (ConnectionObservation id ConnectingConnectionStatus) unmask (Connection.acquire poolAdapter settings) >>= \case Left connErr -> do- let connErrText = case connErr of- Errors.ConnectionAcquireError details -> optionalDetails details- Errors.ConnectionPasswordRequiredAcquireError details -> optionalDetails details- Errors.VersionTooOldAcquireError {} -> Just (Errors.toDetailedText connErr)- Errors.InitializationConnectionLossAcquireError details -> optionalDetails details- Errors.InitializationServerErrorAcquireError serverError -> Just (Errors.toDetailedText serverError)- unmask $ poolObserver (ConnectionObservation id (TerminatedConnectionStatus (NetworkErrorConnectionTerminationReason connErrText)))+ unmask $ poolObserver (ConnectionObservation id (TerminatedConnectionStatus (AcquireErrorConnectionTerminationReason connErr))) atomically returnSlot return $ Left $ ConnectionUsageError connErr Right connection ->@@ -327,8 +316,8 @@ Connection.release connection unmask $ case err of- Errors.ConnectionUseError details ->- poolObserver (ConnectionObservation id (TerminatedConnectionStatus (NetworkErrorConnectionTerminationReason (optionalDetails details))))+ Errors.ConnectionUseError connectionError ->+ poolObserver (ConnectionObservation id (TerminatedConnectionStatus (NetworkErrorConnectionTerminationReason connectionError))) Errors.SessionUseError sessionErr -> poolObserver (ConnectionObservation id (TerminatedConnectionStatus (InitializationErrorTerminationReason sessionErr))) atomically returnSlot@@ -358,19 +347,19 @@ $ poolObserver ( ConnectionObservation (entryId entry)- (TerminatedConnectionStatus (NetworkErrorConnectionTerminationReason (Just (Text.pack (displayException exc)))))+ (TerminatedConnectionStatus (ExceptionConnectionTerminationReason (Text.pack (displayException exc)))) ) throwIO exc Right (Left err) -> case err of- Errors.ConnectionUseError details -> do+ Errors.ConnectionUseError connectionError -> do Connection.release (entryConnection entry) atomically returnSlot unmask $ poolObserver ( ConnectionObservation (entryId entry)- (TerminatedConnectionStatus (NetworkErrorConnectionTerminationReason (optionalDetails details)))+ (TerminatedConnectionStatus (NetworkErrorConnectionTerminationReason connectionError)) ) return $ Left $ SessionUsageError err Errors.SessionUseError sessionErr -> do@@ -383,7 +372,7 @@ $ poolObserver ( ConnectionObservation (entryId entry)- (TerminatedConnectionStatus (NetworkErrorConnectionTerminationReason (Just (Errors.toMessage err))))+ (TerminatedConnectionStatus (SessionErrorConnectionTerminationReason sessionErr)) ) return $ Left $ SessionUsageError err else do
src/library/Hasql/Pool/ConnectionDiscards.hs view
@@ -159,8 +159,8 @@ -- 'Errors.ConnectionUseError', which the pool discards without consulting -- this module. @57P01@ was tested directly: terminating the backend -- underneath a session surfaces as a 'Errors.ConnectionUseError' carrying- -- the FATAL text under @pqi-ffi@, and as a thrown exception under- -- @pqi-native@ - never as a 'Errors.ServerError' reaching this function.+ -- an 'Errors.ServerConnectionError' with the SQLSTATE intact - never as a+ -- 'Errors.ServerError' reaching this function. -- The rule is kept anyway: it costs nothing, and the remaining codes in -- the class have not been shown to behave the same way. | Text.isPrefixOf "57P" code = True
src/library/Hasql/Pool/Observation.hs view
@@ -2,7 +2,13 @@ -- -- Provides a flexible mechanism for monitoring the healthiness of the pool via logs and metrics without any opinionated choices on the actual monitoring technologies. -- Specific interpreters are encouraged to be created as extension libraries.-module Hasql.Pool.Observation where+module Hasql.Pool.Observation+ ( Observation (..),+ ConnectionStatus (..),+ ConnectionReadyForUseReason (..),+ ConnectionTerminationReason (..),+ )+where import Hasql.Errors qualified as Errors import Hasql.Pool.Prelude@@ -55,8 +61,15 @@ -- -- This includes connectivity issues with the server as well as fatal -- session errors that invalidate the connection's prepared statement or- -- type caches.- NetworkErrorConnectionTerminationReason (Maybe Text)+ -- type caches. The underlying error stays structured so callers can+ -- inspect a server-reported SQLSTATE with 'Errors.toSqlState'.+ NetworkErrorConnectionTerminationReason Errors.ConnectionError+ | -- | Connection acquisition failed before the connection became usable.+ AcquireErrorConnectionTerminationReason Errors.AcquireError+ | -- | A fatal session error required the connection to be discarded.+ SessionErrorConnectionTerminationReason Errors.SessionError+ | -- | An exception caused the connection to be discarded.+ ExceptionConnectionTerminationReason Text | -- | User has invoked the 'Hasql.Pool.release' procedure. ReleaseConnectionTerminationReason | -- | Initialization session failure.