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

@wooddeep/node_ipc

v0.1.10

Published

ipc for nodejs base on share memory and semaphore! [jump to github](https://github.com/wooddeep/node_ipc)

Downloads

6

Readme

node_ipc

ipc for nodejs base on share memory and semaphore! jump to github

description

The default IPC mechanism of Node.js does not support direct communication between worker processes, only communication between the master process and worker processes. Communication between worker processes requires intermediate transfer through the master process. The purpose of developing this project is to implement IPC communication between worker processes in Node.js cluster mode through napi-rs. IPC between processes is achieved through shared memory and semaphores. Currently, there are no open-source Node.js plugins available on the Internet, so I'm implementing one myself.

Support platform: window, linux

usage

  1. semaphore test:
  • 1.1 source code
// test_sem.js
let backend = require("@wooddeep/node_ipc")
const bodyParser = require('koa-bodyparser')
const Router = require('koa-router')
const Koa = require('koa')

const app = new Koa()
let router = new Router()
app.use(bodyParser())

router.get('/create', async (ctx) => {
  await backend.semCreate("test")
  ctx.body = 'semCreate response'
});

router.get('/require', async (ctx) => {
  await backend.semRequire("test")
  ctx.body = 'semRequire response'
});

router.get('/release', async (ctx) => {
  await backend.semRelease("test")
  ctx.body = 'semRelease response'
});

app.use(router.routes()).use(router.allowedMethods())


process.on("SIGINT", () => {
  backend.processExit()
  process.exit()
});

process.on("beforeExit", (code) => {
  console.log("## pre exit in node...")
  backend.processExit()
})

async function main() {

  let port = Number.parseInt(process.argv[2]);
  port = port == undefined ? 3000 : port;

  app.listen(port, () => {
    console.log(`start server at localhost:${port}`);
  })

}

main().then(() => 0);
  • 1.2 test script:
# start the first server
node test_sem.js 3000

# start the second server
node test_sem.js 3001

# create semaphore and require sem
curl "http://127.0.0.1:3000/create"
curl "http://127.0.0.1:3000/require"

# release sem
curl "http://127.0.0.1:3001/release"
  1. share memory test:
  • 2.1 source code
// test_shm.js
let backend = require("@wooddeep/node_ipc")
const bodyParser = require('koa-bodyparser')
const Router = require('koa-router')
const Koa = require('koa')

const app = new Koa()
let router = new Router()
app.use(bodyParser())

// curl "http://127.0.0.1:3000/write?shm_name=rustMapping&content=helloworld"
router.get('/write', async (ctx) => {
  let shm_name = ctx.request.query["shm_name"];
  shm_name = shm_name == undefined ? "RustMapping" : shm_name;

  let content = ctx.request.query["content"];
  content = content == undefined ? "hello world!" : content;

  backend.shmOpen(shm_name, 1024);

  backend.shmWriteStr(shm_name, 0, content)
  ctx.body = '{"code": 0, "msg":"write ok!"}'
});

// curl -v http://127.0.0.1:3001/write?shm_name=rustMapping
router.get('/read', async (ctx) => {
  let shm_name = ctx.request.query["shm_name"];
  shm_name = shm_name == undefined ? "RustMapping" : shm_name;
  backend.shmOpen(shm_name, 1024);
  let data = backend.shmReadStr(shm_name, 0, 1024);

  ctx.body = data
});

app.use(router.routes()).use(router.allowedMethods())

process.on("SIGINT", () => {
  backend.processExit()
  process.exit()
});

process.on("beforeExit", (code) => {
  console.log("## pre exit in node...")
  backend.processExit()
})

async function main() {

  let port = Number.parseInt(process.argv[2]);
  port = port == undefined ? 3000 : port;

  app.listen(port, () => {
    console.log(`start server at localhost:${port}`);
  })
}

main().then(() => 0);

  • 2.2 test script:
# start the first server
node test_shm.js 3000

# start the second server
node test_shm.js 3001

# write string to shm
curl "http://127.0.0.1:3000/write?shm_name=rustMapping&content=helloworld"

# read string from shm
curl -v "http://127.0.0.1:3001/write?shm_name=rustMapping"
  1. message queue test:
  • 3.1 source code
// test_mq.js
let backend = require("@wooddeep/node_ipc")
const cluster = require("cluster")
const events = require("events")

const child_proc_num = 3; // /*os.cpus().length*/

process.on("SIGINT", () => {
  backend.processExit()
  process.exit()
});

process.on("beforeExit", (code) => {
  console.log("## pre exit in node...")
  backend.processExit()
})

async function main() {
  if (cluster.isMaster) { // main process
    cluster.schedulingPolicy = cluster.SCHED_RR;

    let mq_index = await backend.mqCreate("0");
    console.log(`mq create, index: ${mq_index}`);

    backend.mqListen(async (data) => {
      console.log(`##[master:${process.pid}] msg from other process; data.length = ${data.length}, data = ${data}, time = ${new Date()}`)
    }, mq_index);

    let child_map = new Map()
    for (var i = 0, n = child_proc_num; i < n; i += 1) {
      let new_worker_env = {};
      new_worker_env["WORKER_INDEX"] = i;
      let child = cluster.fork(new_worker_env); // start child process
      child_map.set(child.process.pid, i)
    }

    cluster.on("exit", (worker, code, signal) => { // start again when one child exit!
      let new_worker_env = {};
      let index = child_map.get(worker.process.pid)
      new_worker_env["WORKER_INDEX"] = index;
      child_map.delete(worker.process.pid)
      let child = cluster.fork(new_worker_env);
      child_map.set(child.process.pid, index)
    })

  } else {

    let sender_index = await backend.mqEstablish("0");
    setInterval(() => {
      let data = `msg form worker[${process.pid}]`
      backend.mqPublish(sender_index, data);
    }, 2000);
    //const emitter = new events.EventEmitter();
  }
}

main().then(() => 0);
  • 3.2 test script:
# start the server cluster
node test_mq.js

# observing the main process receiving data from three child processes in in console ouput:
##[master:14216] msg from other process; data.length = 1024, data = msg form worker[14226], time = Wed Jun 07 2023 15:34:56 GMT+0800 (China Standard Time)
##[master:14216] msg from other process; data.length = 1024, data = msg form worker[14227], time = Wed Jun 07 2023 15:34:56 GMT+0800 (China Standard Time)
##[master:14216] msg from other process; data.length = 1024, data = msg form worker[14229], time = Wed Jun 07 2023 15:34:56 GMT+0800 (China Standard Time)