Skip to content

Suggested Feature: borrowTimeoutMillis #302

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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