Skip to content

Commit 270fa07

Browse files
authored
fix: utilize all allowed connections (#638)
* feat: utilize all allowed connections * feat: utilize all allowed connections * wip * wip * wip * feat: utilize all allowed connections
1 parent 6b9d069 commit 270fa07

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

packages/slonik/src/factories/createConnectionPool.ts

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const createConnectionPool = ({
6262
}: {
6363
driver: Driver;
6464
idleTimeout?: number;
65+
// TODO rename to `maxPoolSize`
6566
poolSize?: number;
6667
}): ConnectionPool => {
6768
// See test "waits for all connections to be established before attempting to terminate the pool"
@@ -124,23 +125,14 @@ export const createConnectionPool = ({
124125
throw new Error('Connection pool has ended.');
125126
}
126127

127-
const idleConnection = connections.find(
128-
(connection) => connection.state() === 'IDLE',
129-
);
130-
131-
if (idleConnection) {
132-
idleConnection.acquire();
133-
134-
return idleConnection;
135-
}
136-
137-
if (pendingConnections.length + connections.length < poolSize) {
128+
const addConnection = async () => {
138129
const pendingConnection = driver.createClient();
139130

140131
pendingConnections.push(pendingConnection);
141132

142133
const connection = await pendingConnection.catch((error) => {
143134
pendingConnections.pop();
135+
144136
throw error;
145137
});
146138

@@ -183,8 +175,6 @@ export const createConnectionPool = ({
183175

184176
connection.on('destroy', onDestroy);
185177

186-
connection.acquire();
187-
188178
connections.push(connection);
189179

190180
pendingConnections.splice(
@@ -193,38 +183,56 @@ export const createConnectionPool = ({
193183
);
194184

195185
return connection;
196-
} else {
197-
const deferred = defer<ConnectionPoolClient>();
186+
};
198187

199-
waitingClients.push({
200-
deferred,
201-
});
188+
const idleConnection = connections.find(
189+
(connection) => connection.state() === 'IDLE',
190+
);
191+
192+
if (idleConnection) {
193+
idleConnection.acquire();
194+
195+
return idleConnection;
196+
}
197+
198+
if (pendingConnections.length + connections.length < poolSize) {
199+
const newConnection = await addConnection();
200+
201+
newConnection.acquire();
202+
203+
return newConnection;
204+
}
205+
206+
const deferred = defer<ConnectionPoolClient>();
202207

203-
const queuedAt = process.hrtime.bigint();
208+
waitingClients.push({
209+
deferred,
210+
});
204211

205-
logger.warn(
212+
const queuedAt = process.hrtime.bigint();
213+
214+
logger.warn(
215+
{
216+
connections: connections.length,
217+
pendingConnections: pendingConnections.length,
218+
poolSize,
219+
waitingClients: waitingClients.length,
220+
},
221+
`connection pool full; client has been queued`,
222+
);
223+
224+
// eslint-disable-next-line promise/prefer-await-to-then
225+
return deferred.promise.then((connection) => {
226+
logger.debug(
206227
{
207-
connections: connections.length,
208-
pendingConnections: pendingConnections.length,
209-
poolSize,
210-
waitingClients: waitingClients.length,
228+
connectionId: connection.id(),
229+
duration: Number(process.hrtime.bigint() - queuedAt) / 1e6,
211230
},
212-
`connection pool full; client has been queued`,
231+
'connection has been acquired from the queue',
213232
);
214233

215-
// eslint-disable-next-line promise/prefer-await-to-then
216-
return deferred.promise.then((connection) => {
217-
logger.debug(
218-
{
219-
connectionId: connection.id(),
220-
duration: Number(process.hrtime.bigint() - queuedAt) / 1e6,
221-
},
222-
'connection has been acquired from the queue',
223-
);
224-
225-
return connection;
226-
});
227-
}
234+
return connection;
235+
});
228236
};
229237

230238
return {

0 commit comments

Comments
 (0)