Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggested Feature: borrowTimeoutMillis #302

Open
daveroberts opened this issue Oct 6, 2022 · 2 comments
Open

Suggested Feature: borrowTimeoutMillis #302

daveroberts opened this issue Oct 6, 2022 · 2 comments

Comments

@daveroberts
Copy link

daveroberts commented Oct 6, 2022

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
Copy link

alanwu4321 commented Oct 12, 2022

Will setTimeout cause overhead? - is there a way to not check this every time I use the client

@daveroberts
Copy link
Author

I modified the function above to only throw the timeout, and only call setTimeout, if opts.borrowTimeoutMillis is set. More to the point though, the code snippet above was only representing how I was using the pool library in my code, and wasn't supposed to be generic to everyone's code. If incorporated into this library, I'd expect edits would need to be made to accommodate more use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants