0.0.2 • Published 2 years ago
dynamic-cluster v0.0.2
dynamic cluster
动态集群,代替 PM2 的固定集群。
起因
一个 32U 的服务器资源,如果涉及一部分计算密集型任务,需要使用 pm2 启动尽可能多的进程,每个 Node 进程占 70MB,那么在没有任务开销时,就会有 2.2GB 的内存开销.
dynamic cluster 是为了解决此类场景,它会根据请求量动态的复制集群数目, 当长时间没有请求响应时,关闭多余的集群。
Example
例子很简单:
import { dynamicCluster } from "dynamic-cluster";
import fastify from "fastify";
dynamicCluster({
emitMaxCount: 1000,
idleClusterTime: 5000,
isLogLife: true,
onWorker: (dynamicFork) => {
const app = fastify();
app.addHook("onResponse", (req, rep, done) => {
dynamicFork();
done();
});
app.get("/v1/hello", () => {
return { hello: "world" };
});
console.log("listen: http://127.0.0.1:8200");
app.listen({
port: 8200,
});
},
});