Schema cache reload with zero downtime (#1559)

* Improve error messages and comments

* Reorder Main.hs functions
This commit is contained in:
Steve Chavez
2020-07-16 13:39:25 -05:00
committed by GitHub
parent 6b2767d35c
commit 189847927e
5 changed files with 143 additions and 146 deletions
+3 -2
View File
@@ -66,7 +66,7 @@ import Protolude hiding (Proxy, intercalate, toS)
import Protolude.Conv (toS)
postgrest :: LogSetup -> IORef AppConfig -> IORef (Maybe DbStructure) -> P.Pool -> IO UTCTime -> IO () -> Application
postgrest logS refConf refDbStructure pool getTime worker =
postgrest logS refConf refDbStructure pool getTime connWorker =
pgrstMiddleware logS $ \ req respond -> do
time <- getTime
body <- strictRequestBody req
@@ -100,7 +100,8 @@ postgrest logS refConf refDbStructure pool getTime worker =
txMode = transactionMode proc (iAction apiRequest)
dbResp <- P.use pool $ HT.transaction HT.ReadCommitted txMode handleReq
return $ either (errorResponseFor . PgError authed) identity dbResp
when (responseStatus response == status503) worker
-- Launch the connWorker when the connection is down. The postgrest function can respond successfully(with a stale schema cache) before the connWorker is done.
when (responseStatus response == status503) connWorker
respond response
transactionMode :: Maybe ProcDescription -> Action -> HT.Mode
+3 -3
View File
@@ -132,7 +132,7 @@ instance JSON.ToJSON PgError where
instance JSON.ToJSON P.UsageError where
toJSON (P.ConnectionError e) = JSON.object [
"code" .= ("" :: Text),
"message" .= ("Database connection error" :: Text),
"message" .= ("Database connection error. Retrying the connection." :: Text),
"details" .= (toS $ fromMaybe "" e :: Text)]
toJSON (P.SessionError e) = JSON.toJSON e -- H.Error
@@ -169,7 +169,7 @@ instance JSON.ToJSON H.CommandError where
"message" .= ("Unexpected amount of rows" :: Text),
"details" .= i]
toJSON (H.ClientError d) = JSON.object [
"message" .= ("Database client error" :: Text),
"message" .= ("Database client error. Retrying the connection." :: Text),
"details" .= (fmap toS d :: Maybe Text)]
pgErrorStatus :: Bool -> P.UsageError -> HT.Status
@@ -256,7 +256,7 @@ instance JSON.ToJSON SimpleError where
toJSON (BinaryFieldError ct) = JSON.object [
"message" .= ((toS (toMime ct) <> " requested but more than one column was selected") :: Text)]
toJSON ConnectionLostError = JSON.object [
"message" .= ("Database connection lost, retrying the connection." :: Text)]
"message" .= ("Database connection lost. Retrying the connection." :: Text)]
toJSON PutRangeNotAllowedError = JSON.object [
"message" .= ("Range header and limit/offset querystring parameters are not allowed for PUT" :: Text)]