npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

socket-pool

v1.2.3

Published

Node socket pool for persistent TCP connections

Downloads

28

Readme

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