-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add connection retrying on startup and SIGHUP, Fix #742 #869
Changes from 5 commits
fa37faf
8323655
1dd2c35
7c83a57
dacfc9d
cf5abfe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ import PostgREST.DbRequestBuilder( readRequest | |
import PostgREST.Error ( simpleError, pgError | ||
, apiRequestError | ||
, singularityError, binaryFieldError | ||
, connectionLostError | ||
) | ||
import PostgREST.RangeQuery (allRange, rangeOffset) | ||
import PostgREST.Middleware | ||
|
@@ -62,28 +63,37 @@ import Data.Function (id) | |
import Protolude hiding (intercalate, Proxy) | ||
import Safe (headMay) | ||
|
||
postgrest :: AppConfig -> IORef DbStructure -> P.Pool -> IO POSIXTime -> | ||
Application | ||
postgrest conf refDbStructure pool getTime = | ||
postgrest :: AppConfig -> IORef (Maybe DbStructure) -> P.Pool -> IO POSIXTime -> | ||
IO () -> Application | ||
postgrest conf refDbStructure pool getTime worker = | ||
let middle = (if configQuiet conf then id else logStdout) . defaultMiddle in | ||
|
||
middle $ \ req respond -> do | ||
time <- getTime | ||
body <- strictRequestBody req | ||
dbStructure <- readIORef refDbStructure | ||
maybeDbStructure <- readIORef refDbStructure | ||
case maybeDbStructure of | ||
Nothing -> respond connectionLostError | ||
Just dbStructure -> do | ||
response <- case userApiRequest (configSchema conf) req body of | ||
Left err -> return $ apiRequestError err | ||
Right apiRequest -> do | ||
let jwtSecret = binarySecret <$> configJwtSecret conf | ||
eClaims = jwtClaims jwtSecret (iJWT apiRequest) time | ||
authed = containsRole eClaims | ||
handleReq = runWithClaims conf eClaims (app dbStructure conf) apiRequest | ||
txMode = transactionMode dbStructure | ||
(iTarget apiRequest) (iAction apiRequest) | ||
response <- P.use pool $ HT.transaction HT.ReadCommitted txMode handleReq | ||
return $ either (pgError authed) identity response | ||
if isResponse503 response then do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be a good use for the when function: when (isResponse503 response) worker
respond respose |
||
worker | ||
respond response | ||
else | ||
respond response | ||
|
||
response <- case userApiRequest (configSchema conf) req body of | ||
Left err -> return $ apiRequestError err | ||
Right apiRequest -> do | ||
let jwtSecret = binarySecret <$> configJwtSecret conf | ||
eClaims = jwtClaims jwtSecret (iJWT apiRequest) time | ||
authed = containsRole eClaims | ||
handleReq = runWithClaims conf eClaims (app dbStructure conf) apiRequest | ||
txMode = transactionMode dbStructure | ||
(iTarget apiRequest) (iAction apiRequest) | ||
response <- P.use pool $ HT.transaction HT.ReadCommitted txMode handleReq | ||
return $ either (pgError authed) identity response | ||
respond response | ||
isResponse503 :: Response -> Bool | ||
isResponse503 resp = statusCode (responseStatus resp) == 503 | ||
|
||
transactionMode :: DbStructure -> Target -> Action -> H.Mode | ||
transactionMode structure target action = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ module PostgREST.Error ( | |
, simpleError | ||
, singularityError | ||
, binaryFieldError | ||
, connectionLostError | ||
, encodeError | ||
) where | ||
|
||
|
@@ -73,6 +74,10 @@ binaryFieldError = | |
simpleError HT.status406 (toS (toMime CTOctetStream) <> | ||
" requested but a single column was not selected") | ||
|
||
connectionLostError :: Response | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the either type we could also remove this function and use only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A special message for the reconnection state is needed. I tried to make your suggestion work by adding a new constructor for |
||
connectionLostError = | ||
simpleError HT.status503 "Database connection lost, retrying the connection." | ||
|
||
encodeError :: JSON.ToJSON a => a -> LByteString | ||
encodeError = JSON.encode | ||
|
||
|
@@ -128,7 +133,7 @@ instance JSON.ToJSON H.Error where | |
"details" .= (fmap toS d::Maybe Text)] | ||
|
||
httpStatus :: Bool -> P.UsageError -> HT.Status | ||
httpStatus _ (P.ConnectionError _) = HT.status500 | ||
httpStatus _ (P.ConnectionError _) = HT.status503 | ||
httpStatus authed (P.SessionError (H.ResultError (H.ServerError c _ _ _))) = | ||
case toS c of | ||
'0':'8':_ -> HT.status503 -- pg connection err | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is where I see that having an
Either ApiRequestError DbStructure
type would be useful, making composition easier