coopernurse / node-pool

Generic resource pooling for node.js
2.38k stars 259 forks source link

factoryCreate continues to be called even after draining pool #256

Open dhensby opened 5 years ago

dhensby commented 5 years ago

I've been investigating the issue reported in #175 (where a rejected factoryCreate can cause an infinite loop).

I've found that if you call pool.drain() whilst a resource is being acquired the pool will continue to make calls to the factoryCreate function. I believe this is caused by the fact that _dispense doesn't check if the pool has been drained since the initial acquire call was made.

The specific circumstance I'm dealing with is managing a pool of DB connections and if the DB server goes away and you attempt to close the pool, connection attempts still continue to be made.

sandfox commented 5 years ago

Pool.drain is designed to allow any previously made calls to pool.acquire to be given a resource, and for that happen _dispense still needs to be able to potentially create resources etc.

dhensby commented 5 years ago

I think this issue is exacerbated by the immediate retry to create a resource on factoryCreateFailure because if there's a problem that means the pool needs to be closed whilst an acquire call is attempting to create a resource, there's no way to exit or abort the acquire calls that will never be able to fulfil.

If I understand correctly, it's intentional that you could never break out of the acquire/_dispense loop in the event that resource creation is no longer possible?

edit: This is highly problematic for managing a pool of DB connections because you'd want to be able to close the pool if the DB server goes away, but that's not possible if the pool is stuck in an acquire loop because connections can't be created