socket-pool
Node socket pool for persistent TCP/IPC connections
Install
$ npm install socket-pool --save
Usage
const pool = connect: host: 'localhost' port: 2181 // Defaults to `3000` connectTimeout: 3000 pool: // the options of generic-pool max: 100 min: 10 // other options of net.Socket pool // And then, the usage of `socket` is nearly the same as `new net.Socket`
new Pool({connect, pool, ...socketOptions})
- pool
Object
the options ofgeneric-pool
, and the value is simply passed - connectTimeout
Number=3000
the milliseconds socket pool will wait for a socket to connect to the server before timing out. Defaults to3000
milliseconds. - socketOptions
Object
the options ofnew net.Socket(options)
of the vanilla node.js. The only difference is that the optionsocketOptions.allowHalfOpen
defaults totrue
. If half-opened TCP connections are not allowed,allowHalfOpen
should be explicit set tofalse
. But setting this tofalse
is kind of silly, since that's the whole purpose of this lib.
Object
connect If connect.path
is specified, then other socket options will be ignored, and it is only for IPC connections.
- path
String
the same argument ofsocket.connect(path)
of the vanilla node.js
Otherwise, it is for TCP connections, available options are:
- port
- host
- localAddress
- localPort
- family
- hints
- lookup
pool.acquire()
Returns Promise
.
Promise.resolve(socket)
If the socket is successful connectedPromise.reject(error)
If there are any errors- error
SocketError|TimeoutError
- error
pool
The acquired socket is a wrapped net.Socket
instance which will be destroyed when 'end'
event occurs, and some additional methods are available:
socket.release()
The socket-pool
-specified method to release the socket to the pool
socket.destroy()
Destroy the socket instance.
License
MIT