Skip to content

Commit

Permalink
refactor: rename releasePool to flushPool and document its intended b…
Browse files Browse the repository at this point in the history
…ehaviour

Also fix documentation of AppState pool field.
  • Loading branch information
robx committed Aug 29, 2022
1 parent 5c262a2 commit cdb9caf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/PostgREST/AppState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module PostgREST.AppState
( AppState
, destroy
, flushPool
, getConfig
, getDbStructure
, getIsListenerOn
Expand All @@ -21,7 +22,6 @@ module PostgREST.AppState
, putJsonDbS
, putPgVersion
, putRetryNextIn
, releasePool
, signalListener
, usePool
, waitListener
Expand All @@ -46,7 +46,9 @@ import Protolude


data AppState = AppState
{ statePool :: SQL.Pool -- | Connection pool, either a 'Connection' or a 'ConnectionError'
-- | Database connection pool
{ statePool :: SQL.Pool
-- | Database server version, will be updated by the connectionWorker
, statePgVersion :: IORef PgVersion
-- | No schema cache at the start. Will be filled in by the connectionWorker
, stateDbStructure :: IORef (Maybe DbStructure)
Expand Down Expand Up @@ -91,7 +93,7 @@ initWithPool newPool conf =
<*> newIORef 0

destroy :: AppState -> IO ()
destroy = releasePool
destroy AppState{..} = SQL.release statePool

initPool :: AppConfig -> IO SQL.Pool
initPool AppConfig{..} =
Expand All @@ -100,8 +102,14 @@ initPool AppConfig{..} =
usePool :: AppState -> SQL.Session a -> IO (Either SQL.UsageError a)
usePool AppState{..} = SQL.use statePool

releasePool :: AppState -> IO ()
releasePool AppState{..} = SQL.release statePool
-- | Flush the connection pool so that any future use of the pool will
-- use connections freshly established after this call.
--
-- FIXME: #2401 Connections that are in-use during the call to flushPool
-- will currently be returned to the pool and reused afterwards, in
-- conflict with the intention.
flushPool :: AppState -> IO ()
flushPool AppState{..} = SQL.release statePool

getPgVersion :: AppState -> IO PgVersion
getPgVersion = readIORef . statePgVersion
Expand Down
2 changes: 1 addition & 1 deletion src/PostgREST/Workers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ connectionWorker appState = do
connectionStatus :: AppState -> IO ConnectionStatus
connectionStatus appState =
retrying retrySettings shouldRetry $
const $ AppState.releasePool appState >> getConnectionStatus
const $ AppState.flushPool appState >> getConnectionStatus
where
retrySettings = capDelay delayMicroseconds $ exponentialBackoff backoffMicroseconds
delayMicroseconds = 32000000 -- 32 seconds
Expand Down

0 comments on commit cdb9caf

Please sign in to comment.