-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: attempts to connect the correct number of times (fixes #473)
- Loading branch information
Showing
4 changed files
with
110 additions
and
59 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
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,60 @@ | ||
import { ConnectionError, UnexpectedStateError } from '../errors'; | ||
import { getPoolState, poolClientStateMap } from '../state'; | ||
import { type Logger } from '../types'; | ||
import { createUid } from '../utilities/createUid'; | ||
import { type Pool as PgPool, type PoolClient as PgPoolClient } from 'pg'; | ||
import { serializeError } from 'serialize-error'; | ||
|
||
export const establishConnection = async ( | ||
parentLog: Logger, | ||
pool: PgPool, | ||
connectionRetryLimit: number, | ||
) => { | ||
const poolState = getPoolState(pool); | ||
|
||
let connection: PgPoolClient; | ||
|
||
let remainingConnectionRetryLimit = connectionRetryLimit + 1; | ||
|
||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
remainingConnectionRetryLimit--; | ||
|
||
try { | ||
connection = await pool.connect(); | ||
|
||
poolClientStateMap.set(connection, { | ||
connectionId: createUid(), | ||
mock: poolState.mock, | ||
poolId: poolState.poolId, | ||
terminated: null, | ||
transactionDepth: null, | ||
transactionId: null, | ||
}); | ||
|
||
break; | ||
} catch (error) { | ||
parentLog.error( | ||
{ | ||
error: serializeError(error), | ||
remainingConnectionRetryLimit, | ||
}, | ||
'cannot establish connection', | ||
); | ||
|
||
if (remainingConnectionRetryLimit > 1) { | ||
parentLog.info('retrying connection'); | ||
|
||
continue; | ||
} else { | ||
throw new ConnectionError(error.message); | ||
} | ||
} | ||
} | ||
|
||
if (!connection) { | ||
throw new UnexpectedStateError('Connection handle is not present.'); | ||
} | ||
|
||
return connection; | ||
}; |
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
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,46 @@ | ||
import { Logger } from '../../../src/Logger'; | ||
import { establishConnection } from '../../../src/routines/establishConnection'; | ||
import { type PoolState, poolStateMap } from '../../../src/state'; | ||
import test from 'ava'; | ||
import { type Pool as PgPool } from 'pg'; | ||
import * as sinon from 'sinon'; | ||
|
||
test('attempts to connection X times', async (t) => { | ||
const pool = { | ||
connect: sinon.stub(), | ||
}; | ||
|
||
poolStateMap.set(pool as unknown as PgPool, {} as unknown as PoolState); | ||
|
||
const connectionRetryLimit = 3; | ||
|
||
await t.throwsAsync( | ||
establishConnection( | ||
Logger, | ||
pool as unknown as PgPool, | ||
connectionRetryLimit, | ||
), | ||
); | ||
|
||
t.is(pool.connect.callCount, connectionRetryLimit); | ||
}); | ||
|
||
test('does not attempt to retry connection when set to 0', async (t) => { | ||
const pool = { | ||
connect: sinon.stub(), | ||
}; | ||
|
||
poolStateMap.set(pool as unknown as PgPool, {} as unknown as PoolState); | ||
|
||
const connectionRetryLimit = 0; | ||
|
||
await t.throwsAsync( | ||
establishConnection( | ||
Logger, | ||
pool as unknown as PgPool, | ||
connectionRetryLimit, | ||
), | ||
); | ||
|
||
t.is(pool.connect.callCount, 1); | ||
}); |