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

shared-memory-disruptor

v4.2.1

Published

Shared memory LMAX Disruptor

Downloads

79

Readme

This is an implementation of the LMAX Disruptor for Node.js.

The LMAX Disruptor is a lock-free data structure and algorithm for fast message passing. The reference implementation is written in Java and supports passing messages between threads.

The implementation here supports passing messages between Node.js processes or worker threads. It’s written in C++ as a Node Addon which is callable from Javascript.

A set of tests is provided for single and multiple processes and multiple workers.

API documentation is available here. Promises (async/await), callback style and synchronous methods are supported.

Currently only POSIX shared memory is supported.

Example

Here’s a program which writes a million random numbers to a Disruptor. Run this first in one terminal.

producer.js

const { Disruptor } = require('shared-memory-disruptor');
const d = new Disruptor('/example', 1000, 4, 1, -1, true, true); // 
async function test() {
    let sum = 0;
    for (let i = 0; i < 1000000; i += 1) {
        const n = Math.floor(Math.random() * 100);
        const { buf } = await d.produceClaim(); // 
        buf.writeUInt32LE(n, 0, true);
        await d.produceCommit(); // 
        sum += n;
    }
    console.log(sum);
}
test();
  • Use a Disruptor on the shared memory object /example. The Disruptor has 1000 elements of 4 bytes each. It has 1 consumer, and because we’re only going to produce data, we give an invalid a consumer index (-1) which won’t be used. We’ll initialize the Disruptor and spin when the Disruptor is full.

  • Grab an element in the Disruptor to fill in.

  • Tell the Disruptor we’ve filled in the elements.

Here’s another program which reads a million random numbers from the same Disruptor. Run this in a second terminal.

consumer.js

const { Disruptor } = require('shared-memory-disruptor');
const d = new Disruptor('/example', 1000, 4, 1, 0, false, true); // 
async function test() {
    let sum = 0, i = 0;
    while (i < 1000000) {
        const { bufs } = await d.consumeNew(); // 
        for (let buf of bufs) {
            for (let j = 0; j < buf.length; j += 4) {
                sum += buf.readUInt32LE(j, true);
                i += 1;
            }
        }
        d.consumeCommit(); // 
    }
    console.log(sum);
}
test();
  • Use the Disruptor that formalpara_title initialized on the shared memory object /example. We must specify the same number of elements (1000) of the same size (4 bytes), and the same number of consumers (1). We’ll be the only consumer (index 0), won’t initialize the Disruptor and will spin when the Disruptor is empty.

  • Read new data from the Disruptor. We get an array of Buffers, each Buffer containing a whole number of elements.

  • Tell the Disruptor we’ve finished processing the data.

Both programs print the same result.

Streaming example

You can produce and consume arbitrary data through streams.

consumer.js

// You must run the consumer before the producer
const { Disruptor, DisruptorReadStream } = require('shared-memory-disruptor');

const d = new Disruptor('/stream', 1000, 1, 1, 0, true, false);
const rs = new DisruptorReadStream(d)

rs.pipe(process.stdout);

producer.js

const { Disruptor, DisruptorWriteStream } = require('shared-memory-disruptor');

process.stdin.pipe(
    new DisruptorWriteStream(
        new Disruptor('/stream', 1000, 1, 1, 0, false, false)
    )
);

First start the consumer by running on a terminal window:

node consumer.js

This will initialize the memory.

On a new terminal, pipe any data to the producer. For example,

{ while true; do echo $RANDOM; sleep 0.1; done; } | node producer.js

Install

npm install shared-memory-disruptor

Licence

MIT

Test

grunt test

Coverage

grunt coverage

LCOV results are available here.

Coveralls page is here.