Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/PostgREST/Listener.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import PostgREST.Version (prettyVersion)
import qualified PostgREST.AppState as AppState
import qualified PostgREST.Config as Config

import Data.Either.Combinators (whenRight)
import Protolude

-- | Starts the Listener in a thread
Expand Down Expand Up @@ -46,25 +47,25 @@ retryingListen appState = do

-- forkFinally allows to detect if the thread dies
void . flip forkFinally handleFinally $ do
dbOrError <- SQL.acquire $ toUtf8 (Config.addTargetSessionAttrs $ Config.addFallbackAppName prettyVersion configDbUri)
case dbOrError of
Right db -> do
SQL.listen db $ SQL.toPgIdentifier dbChannel
AppState.putIsListenerOn appState True
bracket (SQL.acquire $ toUtf8 (Config.addTargetSessionAttrs $ Config.addFallbackAppName prettyVersion configDbUri)) (`whenRight` releaseConnection) $ \dbOrError -> do
Comment thread
steve-chavez marked this conversation as resolved.
Outdated
case dbOrError of
Right db -> do
SQL.listen db $ SQL.toPgIdentifier dbChannel
AppState.putIsListenerOn appState True

delay <- AppState.getNextListenerDelay appState
when (delay > 1) $ do -- if we did a retry
-- assume we lost notifications, refresh the schema cache
AppState.schemaCacheLoader appState
-- reset the delay
AppState.putNextListenerDelay appState 1
delay <- AppState.getNextListenerDelay appState
when (delay > 1) $ do -- if we did a retry
-- assume we lost notifications, refresh the schema cache
AppState.schemaCacheLoader appState
-- reset the delay
AppState.putNextListenerDelay appState 1

observer $ DBListenStart dbChannel
SQL.waitForNotifications handleNotification db
observer $ DBListenStart dbChannel
SQL.waitForNotifications handleNotification db

Left err -> do
observer $ DBListenFail dbChannel (Left err)
exitFailure
Left err -> do
observer $ DBListenFail dbChannel (Left err)
exitFailure
where
observer = AppState.getObserver appState
mainThreadId = AppState.getMainThreadId appState
Expand All @@ -79,3 +80,5 @@ retryingListen appState = do

cacheReloader =
AppState.schemaCacheLoader appState

releaseConnection = void . forkIO . SQL.release
Comment thread
steve-chavez marked this conversation as resolved.
Outdated