Skip to content

Commit

Permalink
fix: release pending pool connections on error (#632)
Browse files Browse the repository at this point in the history
* release pending pool connections on error

* changeset

* simplify try/catch

* oops - remove package-lock.json

---------

Co-authored-by: Michael Nigh <[email protected]>
Co-authored-by: Gajus Kuizinas <[email protected]>
  • Loading branch information
3 people authored Aug 1, 2024
1 parent c415b16 commit 48263cd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-clocks-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"slonik": patch
---

Fix: release pending pool connections on error
5 changes: 4 additions & 1 deletion packages/slonik/src/factories/createConnectionPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ export const createConnectionPool = ({

pendingConnections.push(pendingConnection);

const connection = await pendingConnection;
const connection = await pendingConnection.catch((error) => {
pendingConnections.pop();
throw error;
});

const onRelease = () => {
const waitingClient = waitingClients.shift();
Expand Down
50 changes: 50 additions & 0 deletions packages/slonik/src/integration.test/lost-connection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable no-console */

import { createPool, sql } from '..';
import { startTestContainer } from './termination.test';
import test from 'ava';
import { execSync } from 'node:child_process';

/**
* @see https://github.com/gajus/slonik/issues/631
*/
test('releases failed connections', async (t) => {
t.timeout(10_000);

try {
const output = execSync('docker --version', { encoding: 'utf8' });
console.log('Docker CLI is available:', output.trim());
} catch {
console.log('Skipper the test. Docker CLI is not available.');

return;
}

const { dsn, terminate } = await startTestContainer();

const pool = await createPool(dsn, {
connectionRetryLimit: 1,
connectionTimeout: 500,
maximumPoolSize: 1,
});

terminate();

const isConnectionAvailable = async () => {
try {
await pool.connect((connection) => {
return connection.oneFirst(sql.unsafe`SELECT NOW()`);
});
return true;
} catch {
// do nothing
}

return false;
};

t.deepEqual(
[await isConnectionAvailable(), await isConnectionAvailable()],
[false, false],
);
});
2 changes: 1 addition & 1 deletion packages/slonik/src/integration.test/termination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { execSync, spawn } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { setTimeout } from 'node:timers/promises';

const startTestContainer = async () => {
export const startTestContainer = async () => {
const dockerContainerName = `slonik-test-${randomUUID()}`;
const servicePort = await getPort();

Expand Down

0 comments on commit 48263cd

Please sign in to comment.