Skip to content

Commit

Permalink
Merge 66fb351 into b23a412
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Aug 3, 2022
2 parents b23a412 + 66fb351 commit 9a4325a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -53,7 +53,7 @@
"nyc": "^15.1.0",
"pg-native": "^3.0.0",
"postgres": "^3.2.4",
"postgres-bridge": "^1.13.0",
"postgres-bridge": "^1.14.0",
"semantic-release": "^18.0.1",
"sinon": "^12.0.1",
"ts-node": "^10.7.0",
Expand Down
27 changes: 8 additions & 19 deletions src/factories/createConnection.ts
Expand Up @@ -44,23 +44,10 @@ type ConnectionHandlerType = (
type PoolHandlerType = (pool: DatabasePool) => Promise<unknown>;

const terminatePoolConnection = (
pool: PgPool,
connection: PgPoolClient,
error: Error,
) => {
const poolState = getPoolState(pool);
const poolClientState = getPoolClientState(connection);

if (!poolClientState.terminated) {
poolClientState.terminated = error;
}

if (poolState.mock) {
return;
}

pool._remove(connection);
pool._pulseQueue();
// tells the pool to destroy this client
connection.release(true);
};

// eslint-disable-next-line complexity
Expand Down Expand Up @@ -176,7 +163,7 @@ export const createConnection = async (
}
}
} catch (error) {
terminatePoolConnection(pool, connection, error);
terminatePoolConnection(connection);

throw error;
}
Expand All @@ -191,7 +178,7 @@ export const createConnection = async (
clientConfiguration,
);
} catch (error) {
terminatePoolConnection(pool, connection, error);
terminatePoolConnection(connection);

throw error;
}
Expand All @@ -203,7 +190,7 @@ export const createConnection = async (
}
}
} catch (error) {
terminatePoolConnection(pool, connection, error);
terminatePoolConnection(connection);

throw error;
}
Expand Down Expand Up @@ -233,8 +220,10 @@ export const createConnection = async (
// `pool._remove(connection)` ensures that we create a new connection for each `pool.connect()`.
//
// The downside of this approach is that we cannot leverage idle connection pooling.
terminatePoolConnection(pool, connection, new ConnectionError('Forced connection termination (explicit connection).'));
terminatePoolConnection(connection);
}

// terminatePoolConnection(boundConnection);

return result;
};
20 changes: 10 additions & 10 deletions test/slonik/binders/bindPool/connect.ts
Expand Up @@ -18,8 +18,8 @@ test('ends connection after promise is resolved (explicit connection)', async (t
});

t.is(pool.connectSpy.callCount, 1);
t.is(pool.releaseSpy.callCount, 0);
t.is(pool.removeSpy.callCount, 1);
t.is(pool.releaseSpy.callCount, 1);
t.true(pool.releaseSpy.calledWith(true));
});

test('release connection after promise is resolved (implicit connection)', async (t) => {
Expand All @@ -40,8 +40,8 @@ test('ends connection after promise is rejected', async (t) => {
}));

t.is(pool.connectSpy.callCount, 1);
t.is(pool.releaseSpy.callCount, 0);
t.is(pool.removeSpy.callCount, 1);
t.is(pool.releaseSpy.callCount, 1);
t.true(pool.releaseSpy.calledWith(true));
});

test('does not connect if `beforePoolConnection` throws an error', async (t) => {
Expand Down Expand Up @@ -80,8 +80,8 @@ test('ends connection if `afterPoolConnection` throws an error', async (t) => {
}));

t.is(pool.connectSpy.callCount, 1);
t.is(pool.releaseSpy.callCount, 0);
t.is(pool.removeSpy.callCount, 1);
t.is(pool.releaseSpy.callCount, 1);
t.true(pool.releaseSpy.calledWith(true));
});

test('ends connection if `beforePoolConnectionRelease` throws an error', async (t) => {
Expand All @@ -100,8 +100,8 @@ test('ends connection if `beforePoolConnectionRelease` throws an error', async (
}));

t.is(pool.connectSpy.callCount, 1);
t.is(pool.releaseSpy.callCount, 0);
t.is(pool.removeSpy.callCount, 1);
t.is(pool.releaseSpy.callCount, 1);
t.true(pool.releaseSpy.calledWith(true));
});

test('if `beforePoolConnection` returns pool object, then the returned pool object is used to create a new connection (IMPLICIT_QUERY)', async (t) => {
Expand Down Expand Up @@ -172,8 +172,8 @@ test('if `beforePoolConnection` returns pool object, then the returned pool obje
});

t.is(pool0.connectSpy.callCount, 1);
t.is(pool0.releaseSpy.callCount, 0);
t.is(pool0.removeSpy.callCount, 1);
t.is(pool0.releaseSpy.callCount, 1);
t.true(pool0.releaseSpy.calledWith(true));

t.is(pool1.connectSpy.callCount, 0);
t.is(pool1.releaseSpy.callCount, 0);
Expand Down

0 comments on commit 9a4325a

Please sign in to comment.