-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get back health check for postgres legacy (#3010)
- Loading branch information
1 parent
780ebf3
commit 5a0edff
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
waku/waku_archive_legacy/driver/postgres_driver/postgres_healthcheck.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{.push raises: [].} | ||
|
||
import chronos, chronicles, results | ||
import ../../../common/databases/db_postgres, ../../../common/error_handling | ||
|
||
## Simple query to validate that the postgres is working and attending requests | ||
const HealthCheckQuery = "SELECT version();" | ||
const CheckConnectivityInterval = 60.seconds | ||
const MaxNumTrials = 20 | ||
const TrialInterval = 1.seconds | ||
|
||
proc checkConnectivity*( | ||
connPool: PgAsyncPool, onFatalErrorAction: OnFatalErrorHandler | ||
) {.async.} = | ||
while true: | ||
(await connPool.pgQuery(HealthCheckQuery)).isOkOr: | ||
## The connection failed once. Let's try reconnecting for a while. | ||
## Notice that the 'pgQuery' proc tries to establish a new connection. | ||
|
||
block errorBlock: | ||
## Force close all the opened connections. No need to close gracefully. | ||
(await connPool.resetConnPool()).isOkOr: | ||
onFatalErrorAction("checkConnectivity legacy resetConnPool error: " & error) | ||
|
||
var numTrial = 0 | ||
while numTrial < MaxNumTrials: | ||
let res = await connPool.pgQuery(HealthCheckQuery) | ||
if res.isOk(): | ||
## Connection resumed. Let's go back to the normal healthcheck. | ||
break errorBlock | ||
|
||
await sleepAsync(TrialInterval) | ||
numTrial.inc() | ||
|
||
## The connection couldn't be resumed. Let's inform the upper layers. | ||
onFatalErrorAction("postgres legacy health check error: " & error) | ||
|
||
await sleepAsync(CheckConnectivityInterval) |