Comparing version 40.1.0 to 40.1.1
@@ -19,2 +19,32 @@ "use strict"; | ||
let isEnded = false; | ||
let poolEndPromise = null; | ||
/** | ||
* This function must not throw. | ||
*/ | ||
const endPool = async () => { | ||
try { | ||
await Promise.all(pendingConnections); | ||
} | ||
catch (error) { | ||
logger.error({ | ||
error: (0, serialize_error_1.serializeError)(error), | ||
}, 'error in pool termination sequence while waiting for pending connections to be established'); | ||
} | ||
try { | ||
await Promise.all(connections.map((connection) => connection.release())); | ||
} | ||
catch (error) { | ||
logger.error({ | ||
error: (0, serialize_error_1.serializeError)(error), | ||
}, 'error in pool termination sequence while releasing connections'); | ||
} | ||
try { | ||
await Promise.all(connections.map((connection) => connection.destroy())); | ||
} | ||
catch (error) { | ||
logger.error({ | ||
error: (0, serialize_error_1.serializeError)(error), | ||
}, 'error in pool termination sequence while destroying connections'); | ||
} | ||
}; | ||
return { | ||
@@ -68,30 +98,8 @@ acquire: async () => { | ||
end: async () => { | ||
if (isEnded) { | ||
return; | ||
if (poolEndPromise) { | ||
return poolEndPromise; | ||
} | ||
poolEndPromise = endPool(); | ||
isEnded = true; | ||
try { | ||
await Promise.all(pendingConnections); | ||
} | ||
catch (error) { | ||
logger.error({ | ||
error: (0, serialize_error_1.serializeError)(error), | ||
}, 'error in pool termination sequence while waiting for pending connections to be established'); | ||
} | ||
try { | ||
await Promise.all(connections.map((connection) => connection.release())); | ||
} | ||
catch (error) { | ||
logger.error({ | ||
error: (0, serialize_error_1.serializeError)(error), | ||
}, 'error in pool termination sequence while releasing connections'); | ||
} | ||
try { | ||
await Promise.all(connections.map((connection) => connection.destroy())); | ||
} | ||
catch (error) { | ||
logger.error({ | ||
error: (0, serialize_error_1.serializeError)(error), | ||
}, 'error in pool termination sequence while destroying connections'); | ||
} | ||
return poolEndPromise; | ||
}, | ||
@@ -98,0 +106,0 @@ id: () => { |
@@ -1291,2 +1291,18 @@ "use strict"; | ||
}); | ||
test('pool.end() resolves only when pool ends', async (t) => { | ||
const pool = await (0, __1.createPool)(t.context.dsn, { | ||
driverFactory, | ||
maximumPoolSize: 1, | ||
}); | ||
const promise = pool.query(__1.sql.unsafe ` | ||
SELECT pg_sleep(0.1); | ||
`); | ||
const startTime = Date.now(); | ||
// Earlier implementation was checking if pool end routine has been initiated, | ||
// and was immediately resolving the promise if it was initiated, i.e. | ||
// The second call to pool.end() would resolve the promise immediately. | ||
await Promise.race([pool.end(), pool.end()]); | ||
t.true(Date.now() - startTime >= 100); | ||
await promise; | ||
}); | ||
test('retains explicit connection beyond the idle timeout', async (t) => { | ||
@@ -1293,0 +1309,0 @@ const pool = await (0, __1.createPool)(t.context.dsn, { |
@@ -97,3 +97,3 @@ { | ||
"types": "./dist/index.d.ts", | ||
"version": "40.1.0" | ||
"version": "40.1.1" | ||
} |
@@ -66,2 +66,42 @@ import { Logger } from '../Logger'; | ||
let poolEndPromise: Promise<void> | null = null; | ||
/** | ||
* This function must not throw. | ||
*/ | ||
const endPool = async () => { | ||
try { | ||
await Promise.all(pendingConnections); | ||
} catch (error) { | ||
logger.error( | ||
{ | ||
error: serializeError(error), | ||
}, | ||
'error in pool termination sequence while waiting for pending connections to be established', | ||
); | ||
} | ||
try { | ||
await Promise.all(connections.map((connection) => connection.release())); | ||
} catch (error) { | ||
logger.error( | ||
{ | ||
error: serializeError(error), | ||
}, | ||
'error in pool termination sequence while releasing connections', | ||
); | ||
} | ||
try { | ||
await Promise.all(connections.map((connection) => connection.destroy())); | ||
} catch (error) { | ||
logger.error( | ||
{ | ||
error: serializeError(error), | ||
}, | ||
'error in pool termination sequence while destroying connections', | ||
); | ||
} | ||
}; | ||
return { | ||
@@ -141,44 +181,11 @@ acquire: async () => { | ||
end: async () => { | ||
if (isEnded) { | ||
return; | ||
if (poolEndPromise) { | ||
return poolEndPromise; | ||
} | ||
poolEndPromise = endPool(); | ||
isEnded = true; | ||
try { | ||
await Promise.all(pendingConnections); | ||
} catch (error) { | ||
logger.error( | ||
{ | ||
error: serializeError(error), | ||
}, | ||
'error in pool termination sequence while waiting for pending connections to be established', | ||
); | ||
} | ||
try { | ||
await Promise.all( | ||
connections.map((connection) => connection.release()), | ||
); | ||
} catch (error) { | ||
logger.error( | ||
{ | ||
error: serializeError(error), | ||
}, | ||
'error in pool termination sequence while releasing connections', | ||
); | ||
} | ||
try { | ||
await Promise.all( | ||
connections.map((connection) => connection.destroy()), | ||
); | ||
} catch (error) { | ||
logger.error( | ||
{ | ||
error: serializeError(error), | ||
}, | ||
'error in pool termination sequence while destroying connections', | ||
); | ||
} | ||
return poolEndPromise; | ||
}, | ||
@@ -185,0 +192,0 @@ id: () => { |
@@ -1700,2 +1700,24 @@ /* eslint-disable id-length */ | ||
test('pool.end() resolves only when pool ends', async (t) => { | ||
const pool = await createPool(t.context.dsn, { | ||
driverFactory, | ||
maximumPoolSize: 1, | ||
}); | ||
const promise = pool.query(sql.unsafe` | ||
SELECT pg_sleep(0.1); | ||
`); | ||
const startTime = Date.now(); | ||
// Earlier implementation was checking if pool end routine has been initiated, | ||
// and was immediately resolving the promise if it was initiated, i.e. | ||
// The second call to pool.end() would resolve the promise immediately. | ||
await Promise.race([pool.end(), pool.end()]); | ||
t.true(Date.now() - startTime >= 100); | ||
await promise; | ||
}); | ||
test('retains explicit connection beyond the idle timeout', async (t) => { | ||
@@ -1702,0 +1724,0 @@ const pool = await createPool(t.context.dsn, { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1046847
18372