-
Notifications
You must be signed in to change notification settings - Fork 260
Open
Description
I implemented a feature in my usage of this library to throw an error if the client hasn't been returned after a set amount of time, and automatically return the client to the pool:
async function use_client(fn){
let client = await pool.acquire()
let promises = [fn(client)]
if (opts.borrowTimeoutMillis){
promises.push(new Promise((_, reject)=>setTimeout(()=>reject('BORROW_TIMEOUT_REACHED'), opts.borrowTimeoutMillis)))
try {
return await Promise.race(promises)
} finally {.if (client){.pool.release(client) } }
}
use_client(client => {./*your code using the client here */ })Edit: I modified the code to only throw the timeout if opts.borrowTimeoutMillis is set
Would be good to have this incorporated into the library somehow, I'm sure I'm not the first to implement something like this.
alanwu4321 and bmccarthy
Metadata
Metadata
Assignees
Labels
No labels