You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
--| Starts a LISTEN connection and handles notifications. It recovers with exponential backoff with a cap of 32 seconds, if the LISTEN connection is lost.
30
-
retryingListen::AppState->IO()
30
+
-- | This function never returns (but can throw) and return type enforces that.
31
+
retryingListen::AppState->IOVoid
31
32
retryingListen appState =do
32
33
AppConfig{..} <-AppState.getConfig appState
33
34
let
34
35
dbChannel = toS configDbChannel
35
-
handleFinally err =do
36
+
onError err =do
36
37
AppState.putIsListenerOn appState False
37
38
observer $DBListenFail dbChannel (Right err)
38
39
unless configDbPoolAutomaticRecovery $
@@ -44,10 +45,11 @@ retryingListen appState = do
44
45
threadDelay (delay * oneSecondInMicro)
45
46
unless (delay == maxDelay) $
46
47
AppState.putNextListenerDelay appState (delay *2)
48
+
-- loop running the listener
47
49
retryingListen appState
48
50
49
-
--forkFinally allows to detect if the thread dies
50
-
void .flip forkFinally handleFinally$do
51
+
--Execute the listener with with error handling
52
+
handle onError$do
51
53
-- Make sure we don't leak connections on errors
52
54
bracket
53
55
-- acquire connection
@@ -68,7 +70,10 @@ retryingListen appState = do
68
70
AppState.putNextListenerDelay appState 1
69
71
70
72
observer $DBListenStart dbChannel
71
-
SQL.waitForNotifications handleNotification db
73
+
74
+
-- wait for notifications
75
+
-- this will never return, in case of an error it will throw and be caught by onError
76
+
forever $SQL.waitForNotifications handleNotification db
showListenerException (Right _) ="Failed getting notifications"-- should not happen as the listener will never finish (hasql-notifications uses `forever` internally) with a Right result
172
-
showListenerException (Left e) = showOnSingleLine '\t'$show e
0 commit comments