Skip to content

Commit

Permalink
Allow configuring retry interval by callback. (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
andywer authored Jul 24, 2020
1 parent 30c2298 commit f7c1932
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ export interface Options {

/**
* How much time to wait between reconnection attempts (if failed).
* Can also be a callback returning a delay in milliseconds.
* Defaults to 500 ms.
*/
retryInterval?: number
retryInterval?: number | ((attempt: number) => number)

/**
* How many attempts to reconnect after connection loss.
Expand Down Expand Up @@ -87,6 +88,7 @@ function connect(connectionConfig: pg.ClientConfig | undefined, emitter: TypedEv

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

const reconnect = async (onAttempt: (attempt: number) => void): Promise<pg.Client> => {
connectionLogger("Reconnecting to PostgreSQL for notification streaming")
Expand All @@ -111,7 +113,7 @@ function connect(connectionConfig: pg.ClientConfig | undefined, emitter: TypedEv
return newClient
} catch (error) {
connectionLogger("PostgreSQL reconnection attempt failed:", error)
await delay(retryInterval)
await delay(getRetryInterval(attempt - 1))

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 f7c1932

Please sign in to comment.