fix(classes): try to fix memory leak
This commit is contained in:
parent
5fcf7919ac
commit
6a968910a2
14
bin/bootstrap.js
vendored
14
bin/bootstrap.js
vendored
@ -85,14 +85,14 @@ module.exports = function ({Hub, Crypto}) {
|
||||
const app = new Hub(config);
|
||||
app.run();
|
||||
|
||||
let quitCount = 2;
|
||||
// let quitCount = 2;
|
||||
process.on('SIGINT', () => {
|
||||
if (--quitCount > 0) {
|
||||
console.log('Gracefully shutting down from SIGINT (Ctrl+C)');
|
||||
app.stop().then(() => process.exit());
|
||||
} else {
|
||||
process.exit(0);
|
||||
}
|
||||
// if (--quitCount > 0) {
|
||||
// console.log('Gracefully shutting down from SIGINT (Ctrl+C)');
|
||||
// process.exit(0);
|
||||
// } else {
|
||||
process.exit(0);
|
||||
// }
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
|
@ -15,8 +15,6 @@ export class Hub {
|
||||
|
||||
_hub = null; // instance of class net.Server
|
||||
|
||||
_sockets = []; // instances of class Socket
|
||||
|
||||
constructor(config) {
|
||||
Config.init(config);
|
||||
Logger.setLevel(Config.log_level);
|
||||
@ -28,24 +26,15 @@ export class Hub {
|
||||
|
||||
onError(err) {
|
||||
Logger.error(err);
|
||||
this._hub.close();
|
||||
}
|
||||
|
||||
onClose() {
|
||||
Logger.info('server shutdown');
|
||||
this.cleanUp();
|
||||
}
|
||||
|
||||
onConnect(socket) {
|
||||
const _socket = new Socket({id: nextId(), socket});
|
||||
this._sockets.push(_socket);
|
||||
}
|
||||
|
||||
cleanUp() {
|
||||
for (const socket of this._sockets) {
|
||||
if (!socket.destroy && typeof socket.end === 'function') {
|
||||
socket.end();
|
||||
}
|
||||
}
|
||||
new Socket({id: nextId(), socket});
|
||||
}
|
||||
|
||||
run() {
|
||||
@ -61,10 +50,4 @@ export class Hub {
|
||||
});
|
||||
}
|
||||
|
||||
stop() {
|
||||
return new Promise((resolve) => {
|
||||
this._hub.close(resolve);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -57,6 +57,7 @@ export class TcpRelay {
|
||||
}
|
||||
});
|
||||
this._socket.on('error', (err) => this.onError({host, port}, err));
|
||||
this._socket.on('close', (had_error) => this.onClose(had_error));
|
||||
this._socket.on('data', (buffer) => this.onReceiving(buffer));
|
||||
}
|
||||
|
||||
@ -74,15 +75,11 @@ export class TcpRelay {
|
||||
case 'ECONNREFUSED':
|
||||
Logger.warn(`[${this._id}] =x=> ${host}:${port}`);
|
||||
break;
|
||||
case 'EADDRNOTAVAIL':
|
||||
case 'ENETDOWN':
|
||||
case 'ECONNRESET':
|
||||
Logger.warn(`[${this._id}] ${err.message}`);
|
||||
break;
|
||||
case 'ETIMEDOUT':
|
||||
Logger.warn(`[${this._id}] ${err.message}`);
|
||||
break;
|
||||
case 'EAI_AGAIN':
|
||||
Logger.warn(`[${this._id}] ${err.message}`);
|
||||
break;
|
||||
case 'EPIPE':
|
||||
Logger.warn(`[${this._id}] ${err.message}`);
|
||||
return;
|
||||
@ -90,11 +87,22 @@ export class TcpRelay {
|
||||
Logger.error(err);
|
||||
break;
|
||||
}
|
||||
this.onClose(true);
|
||||
}
|
||||
|
||||
onClose(had_error) {
|
||||
if (had_error) {
|
||||
Logger.warn(`client[${this._id}] closed due to a transmission error`);
|
||||
} else {
|
||||
Logger.info(`client[${this._id}] closed normally`);
|
||||
}
|
||||
if (!this._socket.destroyed) {
|
||||
this._socket.end();
|
||||
this._socket = null;
|
||||
}
|
||||
if (!this._lsocket.destroyed) {
|
||||
this._lsocket.end();
|
||||
this._lsocket = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,8 +59,8 @@ export class Socket {
|
||||
this._socket = socket;
|
||||
this.updateCiphers();
|
||||
// events
|
||||
socket.on('error', (err) => this.onError(socket, err));
|
||||
socket.on('close', (had_error) => this.onClose(socket, had_error));
|
||||
socket.on('error', (err) => this.onError(err));
|
||||
socket.on('close', (had_error) => this.onClose(had_error));
|
||||
socket.on('data', (buffer) => this.onReceiving(socket, buffer));
|
||||
Logger.info(`client[${this._id}] connected`);
|
||||
}
|
||||
@ -170,7 +170,7 @@ export class Socket {
|
||||
}
|
||||
}
|
||||
|
||||
onError(socket, err) {
|
||||
onError(err) {
|
||||
switch (err.code) {
|
||||
case 'ECONNRESET':
|
||||
Logger.warn(`client[${this._id}] ${err.message}`);
|
||||
@ -182,9 +182,10 @@ export class Socket {
|
||||
Logger.error(err);
|
||||
break;
|
||||
}
|
||||
this.onClose(true);
|
||||
}
|
||||
|
||||
onClose(socket, had_error) {
|
||||
onClose(had_error) {
|
||||
if (had_error) {
|
||||
Logger.warn(`client[${this._id}] closed due to a transmission error`);
|
||||
} else {
|
||||
@ -192,6 +193,14 @@ export class Socket {
|
||||
}
|
||||
if (this._tcpRelay !== null) {
|
||||
this._tcpRelay.close();
|
||||
this._tcpRelay = null;
|
||||
}
|
||||
if (this._udpRelay !== null) {
|
||||
this._udpRelay = null;
|
||||
}
|
||||
if (this._socket !== null && !this._socket.destroyed) {
|
||||
this._socket.end();
|
||||
this._socket = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user