-
-
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 2 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,30 @@ import Data.Function (id) | |
import Protolude hiding (intercalate, Proxy) | ||
import Safe (headMay) | ||
|
||
postgrest :: AppConfig -> IORef DbStructure -> P.Pool -> IO POSIXTime -> | ||
postgrest :: AppConfig -> IORef (Maybe DbStructure) -> P.Pool -> IO POSIXTime -> | ||
Application | ||
postgrest conf refDbStructure pool getTime = | ||
let middle = (if configQuiet conf then id else logStdout) . defaultMiddle in | ||
|
||
middle $ \ req respond -> do | ||
time <- getTime | ||
body <- strictRequestBody req | ||
dbStructure <- readIORef refDbStructure | ||
|
||
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 | ||
maybeDbStructure <- readIORef refDbStructure | ||
case maybeDbStructure of | ||
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. This is where I see that having an |
||
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 | ||
respond response | ||
|
||
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 | ||
|
||
|
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.
Wouldn't be better to use an
Either
withApiRequestError
on theLeft
instead of aMaybe
?