Socket pooling in node.js
The problem with socket pooling is there are many use cases. Which is why you mainly see socket pooling libraries for specific protocols (http, mongo, mysql, etc.).
This library neither tries to be too high level (easier but assumes too much), or too low level (flexibility but managing your socket pool becomes hard).
This is for the mid-level.
var Pool = require'socket-pool';var pool =host: '127.0.0.1' port: 80 weight: 5host: '127.0.0.2' port: 80 weight: 10min: 5max: 20;varsocketon'data'// do stuff with datasocketrelease;;socketwrite'some data';var socket = poolacquire;// acquire is 'sync', you either get back a socket or you don'tif socketdo_stuff_with_socketsocket;else// if there's no available socket...// we can queue and let the pool// call it whenever a socket is availablepoolqueuedo_stuff_with_socket;// Or we can create our own socket for now...socket = ;socketconnectdo some stuff with the socket// when we're done we can give it to the pool toopooladdsocket;