refactor: move pool release on shutdown out of signal handler

This changes behaviour somewhat in that:
- We now consistently release the pool on shutdown, even on non-Unix
  platforms, and including for CmdDumpConfig.
- We release the pool *after* interrupting `App.run`, which will
  rather cause more than fewer connection to be closed properly.
  (Previously any in-use connections would not have been caught by
  `releasePool`, though *maybe* the `UserInterrupt` handling in
  the web handler ends up closing the connections properly already
  anyway).

(The main aim of the change is to make it clearer when and why the
pool is released.)
This commit is contained in:
Robert Vollmert
2022-08-03 19:02:33 +02:00
committed by Robert
parent c5849ecbe0
commit 92d00749a3
3 changed files with 18 additions and 13 deletions
+4
View File
@@ -2,6 +2,7 @@
module PostgREST.AppState module PostgREST.AppState
( AppState ( AppState
, destroy
, getConfig , getConfig
, getDbStructure , getDbStructure
, getIsListenerOn , getIsListenerOn
@@ -89,6 +90,9 @@ initWithPool newPool conf =
<*> myThreadId <*> myThreadId
<*> newIORef 0 <*> newIORef 0
destroy :: AppState -> IO ()
destroy = releasePool
initPool :: AppConfig -> IO SQL.Pool initPool :: AppConfig -> IO SQL.Pool
initPool AppConfig{..} = initPool AppConfig{..} =
SQL.acquire (configDbPoolSize, configDbPoolTimeout, toUtf8 configDbUri) SQL.acquire (configDbPoolSize, configDbPoolTimeout, toUtf8 configDbUri)
+13 -8
View File
@@ -34,13 +34,19 @@ main :: App.SignalHandlerInstaller -> Maybe App.SocketRunner -> CLI -> IO ()
main installSignalHandlers runAppWithSocket CLI{cliCommand, cliPath} = do main installSignalHandlers runAppWithSocket CLI{cliCommand, cliPath} = do
conf@AppConfig{..} <- conf@AppConfig{..} <-
either panic identity <$> Config.readAppConfig mempty cliPath Nothing either panic identity <$> Config.readAppConfig mempty cliPath Nothing
appState <- AppState.init conf
case cliCommand of -- Per https://github.com/PostgREST/postgrest/issues/268, we want to
CmdDumpConfig -> do -- explicitly close the connections to PostgreSQL on shutdown.
when configDbConfig $ reReadConfig True appState -- 'AppState.destroy' takes care of that.
putStr . Config.toText =<< AppState.getConfig appState bracket
CmdDumpSchema -> putStrLn =<< dumpSchema appState (AppState.init conf)
CmdRun -> App.run installSignalHandlers runAppWithSocket appState AppState.destroy
(\appState -> case cliCommand of
CmdDumpConfig -> do
when configDbConfig $ reReadConfig True appState
putStr . Config.toText =<< AppState.getConfig appState
CmdDumpSchema -> putStrLn =<< dumpSchema appState
CmdRun -> App.run installSignalHandlers runAppWithSocket appState)
-- | Dump DbStructure schema to JSON -- | Dump DbStructure schema to JSON
dumpSchema :: AppState -> IO LBS.ByteString dumpSchema :: AppState -> IO LBS.ByteString
@@ -54,7 +60,6 @@ dumpSchema appState = do
(toList configDbSchemas) (toList configDbSchemas)
configDbExtraSearchPath configDbExtraSearchPath
configDbPreparedStatements configDbPreparedStatements
AppState.releasePool appState
case result of case result of
Left e -> do Left e -> do
hPutStrLn stderr $ "An error ocurred when loading the schema cache:\n" <> show e hPutStrLn stderr $ "An error ocurred when loading the schema cache:\n" <> show e
+1 -5
View File
@@ -43,11 +43,7 @@ runAppWithSocket settings app socketFileMode socketFilePath =
-- | Set signal handlers, only for systems with signals -- | Set signal handlers, only for systems with signals
installSignalHandlers :: AppState.AppState -> IO () installSignalHandlers :: AppState.AppState -> IO ()
installSignalHandlers appState = do installSignalHandlers appState = do
-- Releases the connection pool whenever the program is terminated, let interrupt = throwTo (AppState.getMainThreadId appState) UserInterrupt
-- see https://github.com/PostgREST/postgrest/issues/268
let interrupt = do
AppState.releasePool appState
throwTo (AppState.getMainThreadId appState) UserInterrupt
install Signals.sigINT interrupt install Signals.sigINT interrupt
install Signals.sigTERM interrupt install Signals.sigTERM interrupt