coopernurse/node-pool — pull request #54 by tikonen (2012-11-03 11:25:14+00:00)
Title: Factory can be asked to validate pooled objects

Disclaimer: This is feature I needed but not sure if it's something that belongs to common codebase, so pull request just in case.

Some type of pooled objects, like TCP connections, can be unexpectedly invalidated by external reasons while they are in the pool. For example, socket can timeout or be closed by peer. Also peer restart can cause all socket objects in pool to become instantly "invalid".

Naturally its pool users responsibility to check if acquired object is still valid and remove it, but for simplicity it's good to have separate validation factory function to do sanity checks.

Trivial example:

...
validate: function(socket) { return socket.writable; },
...

Validate is called only on acquire for pooled objects, not for freshly created. If validate returns false, pool destroys object.