Skip to content

Commit

Permalink
Merge pull request #45 from nikita-volkov/extend-ready-for-use
Browse files Browse the repository at this point in the history
Extend the ReadyForUse connection status in observations with more details
  • Loading branch information
nikita-volkov authored Apr 25, 2024
2 parents 8a9389f + 4b36900 commit faed276
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.1

- `ReadyForUseConnectionStatus` got extended with the `ConnectionReadyForUseReason` details

# 1

- Optional observability event stream added. Provides a flexible mechanism for monitoring the healthiness of the pool via logs and metrics.
Expand Down
Binary file added diagrams-output/observation-status-model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions diagrams/observation-status-model.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
stateDiagram-v2

[*] --> Connecting
Connecting --> ReadyForUse
ReadyForUse --> InUse
InUse --> ReadyForUse
Connecting --> Terminated
ReadyForUse --> Terminated
InUse --> Terminated
Terminated --> [*]
11 changes: 8 additions & 3 deletions hasql-pool.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ maintainer: Nikita Volkov <nikita.y.volkov@mail.ru>
copyright: (c) 2015, Nikita Volkov
license: MIT
license-file: LICENSE
extra-source-files: CHANGELOG.md
extra-source-files:
CHANGELOG.md
diagrams-output/*.png

extra-doc-files:
diagrams-output/*.png

source-repository head
type: git
Expand Down Expand Up @@ -41,6 +46,8 @@ common base-settings
MagicHash
MultiParamTypeClasses
MultiWayIf
NoImplicitPrelude
NoMonomorphismRestriction
NumericUnderscores
OverloadedStrings
ParallelListComp
Expand All @@ -57,8 +64,6 @@ common base-settings
TypeFamilies
TypeOperators
UnboxedTuples
NoImplicitPrelude
NoMonomorphismRestriction

default-language: Haskell2010

Expand Down
6 changes: 3 additions & 3 deletions src/library/exposed/Hasql/Pool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ use Pool {..} sess = do
atomically $ modifyTVar' poolCapacity succ
return $ Left $ ConnectionUsageError connErr
Right connection -> do
poolObserver (ConnectionObservation id ReadyForUseConnectionStatus)
poolObserver (ConnectionObservation id (ReadyForUseConnectionStatus EstablishedConnectionReadyForUseReason))
onLiveConn reuseVar (Entry connection now now id)

onConn reuseVar entry = do
Expand Down Expand Up @@ -205,11 +205,11 @@ use Pool {..} sess = do
return $ Left $ SessionUsageError err
_ -> do
returnConn
poolObserver (ConnectionObservation (entryId entry) ReadyForUseConnectionStatus)
poolObserver (ConnectionObservation (entryId entry) (ReadyForUseConnectionStatus (SessionFailedConnectionReadyForUseReason err)))
return $ Left $ SessionUsageError err
Right (Right res) -> do
returnConn
poolObserver (ConnectionObservation (entryId entry) ReadyForUseConnectionStatus)
poolObserver (ConnectionObservation (entryId entry) (ReadyForUseConnectionStatus SessionSucceededConnectionReadyForUseReason))
return $ Right res
where
returnConn =
Expand Down
14 changes: 13 additions & 1 deletion src/library/exposed/Hasql/Pool/Observation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Hasql.Pool.Observation where

import Hasql.Pool.Prelude
import qualified Hasql.Session as Hasql

-- | An observation of a change of the state of a pool.
data Observation
Expand All @@ -18,13 +19,15 @@ data Observation
deriving (Show, Eq)

-- | Status of a connection.
--
-- <<diagrams-output/observation-status-model.png>>
data ConnectionStatus
= -- | Connection is being established.
--
-- This is the initial status of every connection.
ConnectingConnectionStatus
| -- | Connection is established and not occupied.
ReadyForUseConnectionStatus
ReadyForUseConnectionStatus ConnectionReadyForUseReason
| -- | Is being used by some session.
--
-- After it's done the status will transition to 'ReadyForUseConnectionStatus' or 'TerminatedConnectionStatus'.
Expand All @@ -33,6 +36,15 @@ data ConnectionStatus
TerminatedConnectionStatus ConnectionTerminationReason
deriving (Show, Eq)

data ConnectionReadyForUseReason
= -- | Connection just got established.
EstablishedConnectionReadyForUseReason
| -- | Session execution ended with a failure that does not require a connection reset.
SessionFailedConnectionReadyForUseReason Hasql.QueryError
| -- | Session execution ended with success.
SessionSucceededConnectionReadyForUseReason
deriving (Show, Eq)

-- | Explanation of why a connection was terminated.
data ConnectionTerminationReason
= -- | The age timeout of the connection has passed.
Expand Down

0 comments on commit faed276

Please sign in to comment.