Skip to content

Commit

Permalink
refactor: more accurate naming and documentation for connectionStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
robx committed Aug 29, 2022
1 parent 79b865b commit cdce929
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/PostgREST/Workers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ connectionWorker appState = do
work = do
AppConfig{..} <- AppState.getConfig appState
AppState.logWithZTime appState "Attempting to connect to the database..."
connected <- connectionStatus appState
connected <- establishConnection appState
case connected of
FatalConnectionError reason ->
-- Fatal error when connecting
AppState.logWithZTime appState reason >> killThread (AppState.getMainThreadId appState)
NotConnected ->
-- Unreachable because connectionStatus will keep trying to connect
-- Unreachable because establishConnection will keep trying to connect
return ()
Connected actualPgVersion -> do
-- Procede with initialization
Expand All @@ -97,17 +97,18 @@ connectionWorker appState = do
-- die if our schema cache query has an error
killThread $ AppState.getMainThreadId appState

-- | Check if a connection from the pool allows access to the PostgreSQL
-- database. If not, the pool connections are released and a new connection is
-- tried. Releasing the pool is key for rapid recovery. Otherwise, the pool
-- | Repeatedly flush the pool, and check if a connection from the
-- pool allows access to the PostgreSQL database.
--
-- Releasing the pool is key for rapid recovery. Otherwise, the pool
-- timeout would have to be reached for new healthy connections to be acquired.
-- Which might not happen if the server is busy with requests. No idle
-- connection, no pool timeout.
--
-- The connection tries are capped, but if the connection times out no error is
-- thrown, just 'False' is returned.
connectionStatus :: AppState -> IO ConnectionStatus
connectionStatus appState =
establishConnection :: AppState -> IO ConnectionStatus
establishConnection appState =
retrying retrySettings shouldRetry $
const $ AppState.flushPool appState >> getConnectionStatus
where
Expand Down

0 comments on commit cdce929

Please sign in to comment.