Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cluster-daemon

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cluster-daemon

cluster daemon

  • 1.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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,则该进程会在下次请求处理完毕后自杀。

Keywords

FAQs

Package last updated on 08 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc