Skip to content

Commit 14eeffd

Browse files
Fix EADDRINUSE impress issue
Refs: metarhia/impress#1890 PR-URL: #455 Co-authored-by: Timur Shemsedinov <timur.shemsedinov@gmail.com>
1 parent d94caee commit 14eeffd

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

lib/server.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const { MetaReadable, MetaWritable, chunkDecode } = require('./streams.js');
1010

1111
const SHORT_TIMEOUT = 500;
1212
const EMPTY_PACKET = Buffer.from('{}');
13+
const DEFAULT_LISTEN_RETRY = 3;
1314

1415
const createProxy = (data, save) =>
1516
new Proxy(data, {
@@ -153,8 +154,10 @@ const addHeaders = ({ origin }) => {
153154
if (origin) HEADERS['Access-Control-Allow-Origin'] = origin;
154155
};
155156

156-
class Server {
157+
class Server extends EventEmitter {
157158
constructor(application, options) {
159+
super();
160+
this.retry = options.retry ?? DEFAULT_LISTEN_RETRY;
158161
this.application = application;
159162
this.options = options;
160163
this.balancer = options.kind === 'balancer';
@@ -210,8 +213,10 @@ class Server {
210213
});
211214
});
212215

213-
this.httpServer.on('error', (err) => {
216+
this.wsServer.on('error', (err) => {
214217
if (err.code !== 'EADDRINUSE') return;
218+
this.retry--;
219+
if (this.retry === 0) return void this.emit('error', err);
215220
console.warn(`Address in use: ${host}:${port}, retry...`);
216221
setTimeout(() => {
217222
this.bind();

0 commit comments

Comments
 (0)