分类 Node.js相关 下的文章

一个进程打开了哪些端口在监听

为了让Node.js 能够充分利用多核的CPU,会开一个进程多个worker的模式, 每个worker是一个Node.js event loop. 如何查看开了哪些端口.

eric@eric1:~$ sudo netstat --all --program | grep '8481'
tcp        0      0 localhost:6666          *:*                     LISTEN      8481/pm2: Daemon
tcp        0      0 localhost:ircd          *:*                     LISTEN      8481/pm2: Daemon
tcp6       0      0 [::]:http-alt           [::]:*                  LISTEN      8481/pm2: Daemon
tcp6       0      0 [::]:8082               [::]:*                  LISTEN      8481/pm2: Daemon
tcp6       0      0 [::]:10100              [::]:*                  LISTEN      8481/pm2: Daemon
tcp6       0      0 [::]:10101              [::]:*                  LISTEN      8481/pm2: Daemon
tcp6       0      0 [::]:10102              [::]:*                  LISTEN      8481/pm2: Daemon
unix  3      [ ]         STREAM     CONNECTED     573099484 8481/pm2: Daemon
unix  3      [ ]         STREAM     CONNECTED     573099481 8481/pm2: Daemon
unix  3      [ ]         STREAM     CONNECTED     573099491 8481/pm2: Daemon

eric@eric1:~$ sudo lsof -i -P |grep 8481
pm2:       8481 rebot    3u  IPv6 573099604      0t0  TCP *:10100 (LISTEN)
pm2:       8481 rebot   12u  IPv4 573098742      0t0  TCP localhost:6666 (LISTEN)
pm2:       8481 rebot   13u  IPv4 573098743      0t0  TCP localhost:6667 (LISTEN)
pm2:       8481 rebot   17u  IPv6 573099599      0t0  TCP *:8082 (LISTEN)
pm2:       8481 rebot   18u  IPv6 573099600      0t0  TCP *:8080 (LISTEN)
pm2:       8481 rebot   20u  IPv6 573099610      0t0  TCP *:10101 (LISTEN)
pm2:       8481 rebot   22u  IPv6 573099619      0t0  TCP *:10102 (LISTEN)

进程的线程之间的相互关系:

pstree -a -p -H 8481
pstree -a -l -p -s 8481
top -H -p 8481
ps -L H 8481
ps -eLf
htop 8481

Node.js heap analysis 内存分析

  1. 这个模块可以帮忙做heap dump: https://github.com/bnoordhuis/node-heapdump
  2. 参考文章列表:
    http://jayconrod.com/posts/55/a-tour-of-v8-garbage-collection
    https://blog.risingstack.com/finding-a-memory-leak-in-node-js/

关于 Node.js 的setImmediate, setTimeout, process.nextTick

要根据 Node.js 的event loop 来理解这些 setImmediate, setTimeout, process.nextTick

Node.js actually provides its own implementation of these methods. Timers integrate very closely with the system, and despite the fact that the API mirrors the browser API, there are some differences in implementation.

console.log("start of hello");

setImmediate(()=>{
    console.log("in setImmediate");
    setImmediate(()=>{
        console.log("in setImmediate's setImmediate"); //the 3rd loop
    });
});

setTimeout(()=>{
    console.log("in setTimeout");
    setImmediate(()=>{
        console.log("in setTimeout's setImmediate"); //2nd loop 
    });
}, 0);

process.nextTick(()=>{
    console.log("in nextTick"); //next event action
    process.nextTick(()=>{
        console.log("in nextTick's nextTick"); // next event action
    });
});

console.log("end of hello");

process.nextTick 是当前event action 处理完之后, 下一个会处理的action.

Node.js 文档

Node.js 使用event loop的并发模型 vs 其它语言的 多线程并发模型;
Node.js 的关键是 对于所有的非CPU 计算操作全部都使用异步的callback 模式;

基本语法

node [options] [V8 options] [script.js | -e "script" | -] [--] [arguments]
node inspect [script.js | -e "script" | <host>:<port>] …
node --v8-options

Node.js 对于 ES2015 Support
https://node.green/

Node.js 文档: https://nodejs.org/en/docs/

启用debug:
node --inspect hello.js
会打出如下:

chrome-devtools://devtools/remote/serve_file/@60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/6285270a-4795-40d7-992f-cfc452461144

然后打开9229端口:

http://localhost:9229/json/list

即便一开始没有启用 --inspect 参数, 启动之后, 还是可以通过发送 SIGUSR1 signal, 让它打开debuger (kill -l 查看全部signal)

Node.js is an event-based platform. This means that everything that happens in Node is the reaction to an event. A transaction passing through Node traverses a cascade of callbacks.

关于 Event Loop: link
https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop