Skip to content

Suggested Feature: borrowTimeoutMillisΒ #302

@daveroberts

Description

@daveroberts

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions