File tree Expand file tree Collapse file tree 2 files changed +45
-0
lines changed
waku/waku_archive_legacy/driver/postgres_driver Expand file tree Collapse file tree 2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 1515 ../../../ waku_core,
1616 ../../ common,
1717 ../../ driver,
18+ ./ postgres_healthcheck,
1819 ../../../ common/ databases/ db_postgres as waku_postgres
1920
2021type PostgresDriver * = ref object of ArchiveDriver
@@ -132,6 +133,12 @@ proc new*(
132133 let writeConnPool = PgAsyncPool .new (dbUrl, maxNumConnOnEachPool).valueOr:
133134 return err (" error creating write conn pool PgAsyncPool" )
134135
136+ if not isNil (onFatalErrorAction):
137+ asyncSpawn checkConnectivity (readConnPool, onFatalErrorAction)
138+
139+ if not isNil (onFatalErrorAction):
140+ asyncSpawn checkConnectivity (writeConnPool, onFatalErrorAction)
141+
135142 let driver = PostgresDriver (writeConnPool: writeConnPool, readConnPool: readConnPool)
136143 return ok (driver)
137144
Original file line number Diff line number Diff line change 1+ {.push raises : [].}
2+
3+ import chronos, chronicles, results
4+ import ../../../ common/ databases/ db_postgres, ../../../ common/ error_handling
5+
6+ # # Simple query to validate that the postgres is working and attending requests
7+ const HealthCheckQuery = " SELECT version();"
8+ const CheckConnectivityInterval = 60 .seconds
9+ const MaxNumTrials = 20
10+ const TrialInterval = 1 .seconds
11+
12+ proc checkConnectivity * (
13+ connPool: PgAsyncPool , onFatalErrorAction: OnFatalErrorHandler
14+ ) {.async .} =
15+ while true :
16+ (await connPool.pgQuery (HealthCheckQuery )).isOkOr:
17+ # # The connection failed once. Let's try reconnecting for a while.
18+ # # Notice that the 'pgQuery' proc tries to establish a new connection.
19+
20+ block errorBlock:
21+ # # Force close all the opened connections. No need to close gracefully.
22+ (await connPool.resetConnPool ()).isOkOr:
23+ onFatalErrorAction (" checkConnectivity legacy resetConnPool error: " & error)
24+
25+ var numTrial = 0
26+ while numTrial < MaxNumTrials :
27+ let res = await connPool.pgQuery (HealthCheckQuery )
28+ if res.isOk ():
29+ # # Connection resumed. Let's go back to the normal healthcheck.
30+ break errorBlock
31+
32+ await sleepAsync (TrialInterval )
33+ numTrial.inc ()
34+
35+ # # The connection couldn't be resumed. Let's inform the upper layers.
36+ onFatalErrorAction (" postgres legacy health check error: " & error)
37+
38+ await sleepAsync (CheckConnectivityInterval )
You can’t perform that action at this time.
0 commit comments