Skip to content

Commit

Permalink
feat: set ClusterServer default maximum listeners up to 20 (#66)
Browse files Browse the repository at this point in the history
closes #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 <[email protected]>
  • Loading branch information
fengmk2 and gaoquanzero authored Jan 24, 2024
1 parent f4f5ca1 commit 384b7b4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ jobs:
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
with:
checkTest: false
12 changes: 8 additions & 4 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ class ClusterServer extends Base {
*
* @class
* @param {Object} options

Check warning on line 71 in lib/server.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 14)

Missing JSDoc @param "options" description

Check warning on line 71 in lib/server.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18)

Missing JSDoc @param "options" description

Check warning on line 71 in lib/server.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 16)

Missing JSDoc @param "options" description

Check warning on line 71 in lib/server.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 20)

Missing JSDoc @param "options" description

Check warning on line 71 in lib/server.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 16)

Missing JSDoc @param "options" description

Check warning on line 71 in lib/server.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 14)

Missing JSDoc @param "options" description

Check warning on line 71 in lib/server.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

Missing JSDoc @param "options" description

Check warning on line 71 in lib/server.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

Missing JSDoc @param "options" description

Check warning on line 71 in lib/server.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 14)

Missing JSDoc @param "options" description

Check warning on line 71 in lib/server.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 16)

Missing JSDoc @param "options" description

Check warning on line 71 in lib/server.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18)

Missing JSDoc @param "options" description

Check warning on line 71 in lib/server.js

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 20)

Missing JSDoc @param "options" description
* - {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();
Expand All @@ -88,6 +89,7 @@ class ClusterServer extends Base {
this._server.once('error', err => {
this.emit('error', err);
});
this.setMaxListeners(options.maxListeners || 20);
}

get isClosed() {
Expand Down Expand Up @@ -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)}`);
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/wrapper/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand All @@ -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) {
Expand Down

0 comments on commit 384b7b4

Please sign in to comment.