GithubHelp home page GithubHelp logo

kaelzhang / node-socket-pool Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 4.0 43 KB

Persistent socket connections with pool for node server side.

License: Other

JavaScript 100.00%
nodejs socket-pool tcp-connection socket

node-socket-pool's Introduction

Build Status

socket-pool

Node socket pool for persistent TCP/IPC connections

Install

$ npm install socket-pool --save

Usage

import Pool from 'socket-pool'

const pool = new 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.acquire()
.then(socket => {
  socket.on('data', chunk => {
    // concat chunks

    // To re-use TCP connections, it is better NOT to end or destroy a socket
    // after data received.
    // Some mechanism could be used to tell the client if there is no more
    // chunks, such as:
    // - design a protocol to define the content-length of the incoming chunks.
    if (dataFinished) {
      // Release the socket resource,
      // then it can be required again.
      socket.release()
    }
  })
})

// And then, the usage of `socket` is nearly the same as `new net.Socket`

new Pool({connect, pool, ...socketOptions})

  • pool Object the options of generic-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 to 3000 milliseconds.
  • socketOptions Object the options of new net.Socket(options) of the vanilla node.js. The only difference is that the option socketOptions.allowHalfOpen defaults to true. If half-opened TCP connections are not allowed, allowHalfOpen should be explicit set to false. But setting this to false is kind of silly, since that's the whole purpose of this lib.

connect Object

If connect.path is specified, then other socket options will be ignored, and it is only for IPC connections.

  • path String the same argument of socket.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 connected
  • Promise.reject(error) If there are any errors
    • error SocketError|TimeoutError
import {
  // If connectTimeout is specified and timed out to connect to server
  TimeoutError,
  // Socket error
  SocketError
} from 'socket-pool'

pool.acquire()
.then(
  socket => {
    // do something with socket
  },

  error => {
    console.log(error instanceof SocketError || error instanceof TimeoutError)
    // true
  }
)

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

node-socket-pool's People

Contributors

kaelzhang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-socket-pool's Issues

现在不支持 socket 的 timeout 设置

如果不能连接目标机器(也就是socket不能接受到‘connect’事件)就不会resolve socket,使用者就拿不到socket对象。会hang在那儿。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.