Skip to content

Commit

Permalink
Fix EADDRINUSE impress issue
Browse files Browse the repository at this point in the history
Refs: metarhia/impress#1890
PR-URL: #455
Co-authored-by: Timur Shemsedinov <[email protected]>
  • Loading branch information
MarhiievHE and tshemsedinov committed Oct 3, 2023
1 parent d94caee commit 14eeffd
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { MetaReadable, MetaWritable, chunkDecode } = require('./streams.js');

const SHORT_TIMEOUT = 500;
const EMPTY_PACKET = Buffer.from('{}');
const DEFAULT_LISTEN_RETRY = 3;

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

class Server {
class Server extends EventEmitter {
constructor(application, options) {
super();
this.retry = options.retry ?? DEFAULT_LISTEN_RETRY;
this.application = application;
this.options = options;
this.balancer = options.kind === 'balancer';
Expand Down Expand Up @@ -210,8 +213,10 @@ class Server {
});
});

this.httpServer.on('error', (err) => {
this.wsServer.on('error', (err) => {
if (err.code !== 'EADDRINUSE') return;
this.retry--;
if (this.retry === 0) return void this.emit('error', err);
console.warn(`Address in use: ${host}:${port}, retry...`);
setTimeout(() => {
this.bind();
Expand Down

0 comments on commit 14eeffd

Please sign in to comment.