From 384b7b48c9a79203a1b7639fa4b7475434cfa8b4 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 24 Jan 2024 15:57:53 +0800 Subject: [PATCH] feat: set ClusterServer default maximum listeners up to 20 (#66) closes https://github.com/node-modules/cluster-client/pull/65 increase to avoid nodejs warning ``` MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [ClusterServer]. Use emitter.setMaxListeners() to increase limit ``` Co-authored-by: gaoquanzero --- .github/workflows/release.yml | 2 -- lib/server.js | 12 ++++++++---- lib/wrapper/cluster.js | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a4e1158..1c6cbb1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -11,5 +11,3 @@ jobs: secrets: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} GIT_TOKEN: ${{ secrets.GIT_TOKEN }} - with: - checkTest: false diff --git a/lib/server.js b/lib/server.js index 44b22ab..4f2f861 100644 --- a/lib/server.js +++ b/lib/server.js @@ -69,8 +69,9 @@ class ClusterServer extends Base { * * @class * @param {Object} options - * - {net.Server} server - the server - * - {Number} port - the port + * @param {net.Server} options.server - the server + * @param {Number} options.port - the port + * @param {Number} [options.maxListeners] maximum of listeners, default is 20 */ constructor(options) { super(); @@ -88,6 +89,7 @@ class ClusterServer extends Base { this._server.once('error', err => { this.emit('error', err); }); + this.setMaxListeners(options.maxListeners || 20); } get isClosed() { @@ -164,9 +166,11 @@ class ClusterServer extends Base { * * @param {String} name - the client name * @param {Number} port - the port + * @param {Object} [options] - create instance options + * @param {Number} [options.maxListeners] maximum of listeners, default is 20 * @return {ClusterServer} server */ - static async create(name, port) { + static async create(name, port, options) { const key = `${name}@${port}`; if (typeof port !== 'number') { throw new Error(`[ClusterClient.server.create:${name}] port should be a number, but got ${JSON.stringify(port)}`); @@ -185,7 +189,7 @@ class ClusterServer extends Base { // compete for the local port, if got => leader, otherwise follower try { const server = await claimServer(port); - instance = new ClusterServer({ server, port }); + instance = new ClusterServer({ server, port, ...options }); typeSet.add(key); serverMap.set(port, instance); return instance; diff --git a/lib/wrapper/cluster.js b/lib/wrapper/cluster.js index 93ac044..5b73bf0 100644 --- a/lib/wrapper/cluster.js +++ b/lib/wrapper/cluster.js @@ -33,7 +33,7 @@ class ClusterClient extends Base { const port = this.options.port; let server; if (this.options.isLeader === true) { - server = await ClusterServer.create(name, port); + server = await ClusterServer.create(name, port, { maxListeners: this.options.maxListeners }); if (!server) { throw new Error(`create "${name}" leader failed, the port:${port} is occupied by other`); } @@ -42,7 +42,7 @@ class ClusterClient extends Base { await ClusterServer.waitFor(port, this.options.maxWaitTime); } else { debug('[ClusterClient:%s] init cluster client, try to seize the leader on port:%d', name, port); - server = await ClusterServer.create(name, port); + server = await ClusterServer.create(name, port, { maxListeners: this.options.maxListeners }); } if (server) {