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

cluster-daemon

v1.0.7

Published

cluster daemon

Downloads

8

Readme

cluster-daemon

唉,有的时候找nodejs内存泄露很麻烦,干脆写了一个守护程序,发现内存飙涨就kill掉一个worker,阻止系统崩溃。

工作原理

在cluster模式下,每个worker处理完一次请求之后,会检查自己的内存占用,如果发现超过阈值,就向master发送一个自杀消息,master负责管理所有worker的自杀消息队列,每隔3秒钟从队列中取出一个,通知那个worker允许自杀,worker接收到父进程消息之后,会在下一次处理完请求之后kill掉自己,master能够监听到worker的进程关闭事件,就会立即重启一个worker了,每3秒内最多只有一个worker会被kill掉,其他worker还能保证负载。

Usage

In master

...
for (var i = 0; i < cpuCount; i++) daemon.fork(); //使用daemon的fork方法创建worker
cluster.on('exit', function (worker) {
    logger.error('Worker ' + worker.id + ' died, time: ' + Date.now());
    daemon.fork();
});
daemon.start(3000);  // 启动守护,检查时间是3秒(3000毫秒)

In worker

var daemon = require('cluster-daemon');
var KB = 1024;
var MB = KB * KB;
var limit = 1024 * MB; //限制占用1G内存
app.use(daemon.middleware(function(){
  return process.memoryUsage().rss > limit; //超过限度就会自动kill
}));

API

daemon.fork();

介绍:包装过的cluster.fork()函数,用于fork当前进程,fork过程主要是给worker添加一些事件监听。 调用者:master

daemon.star(interval, onKill);

介绍:启动轮询检查worker的自杀请求 调用者:master 参数:

  • interval:轮询时间间隔,ms单位
  • onKill:发送子进程自杀信号前的回调,参数是要自杀的子进程worker对象

daemon.stop();

介绍:结束daemon.start()的轮询检查 调用者:master

daemon.middleware(check);

介绍:检查子进程健康状态的中间件 调用者:worker 参数:

  • check:每次请求结束后的回调,该回调函数如果返回false,则该进程会在下次请求处理完毕后自杀。