Skip to content

Commit

Permalink
fix: set undefined variables
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Correa Casablanca <[email protected]>
  • Loading branch information
castarco committed Feb 7, 2023
1 parent 30b7d4b commit 1ccc1bf
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,21 @@ export interface Options {
function connect(connectionConfig: pg.ClientConfig | undefined, emitter: TypedEventEmitter<PgListenEvents>, options: Options) {
connectionLogger("Creating PostgreSQL client for notification streaming")

const { retryInterval = 500, retryLimit = Infinity, retryTimeout = 3000 } = options
const effectiveConnectionConfig: pg.ClientConfig = { ...connectionConfig, keepAlive: true }

const Client = options.native && pg.native ? pg.native.Client : pg.Client
const dbClient = new Client(effectiveConnectionConfig)
const getRetryInterval = typeof retryInterval === "function" ? retryInterval : () => retryInterval
const retryInterval = options?.retryInterval
const getRetryInterval = typeof retryInterval === "function"
? retryInterval
: () => retryInterval as number | undefined ?? 0

const reconnect = async (onAttempt: (attempt: number) => void): Promise<pg.Client> => {
connectionLogger("Reconnecting to PostgreSQL for notification streaming")
const startTime = Date.now()

for (let attempt = 1; attempt < retryLimit || !retryLimit; attempt++) {
const retryLimit = options?.retryLimit
for (let attempt = 1; !retryLimit || attempt < retryLimit; attempt++) {
connectionLogger(`PostgreSQL reconnection attempt #${attempt}...`)
onAttempt(attempt)

Expand All @@ -115,6 +118,7 @@ function connect(connectionConfig: pg.ClientConfig | undefined, emitter: TypedEv
connectionLogger("PostgreSQL reconnection attempt failed:", error)
await delay(getRetryInterval(attempt - 1))

const retryTimeout = options?.retryTimeout
if (retryTimeout && (Date.now() - startTime) > retryTimeout) {
throw new Error(`Stopping PostgreSQL reconnection attempts after ${retryTimeout}ms timeout has been reached.`)
}
Expand Down

0 comments on commit 1ccc1bf

Please sign in to comment.