From c281520057d6b727f976dfd2029cb9f095e0ee46 Mon Sep 17 00:00:00 2001 From: Saransh Date: Tue, 21 Sep 2021 15:49:33 +0530 Subject: [PATCH 1/3] changing default kill timer to 15m --- lib/config/config.json.sample | 1 - lib/config/constants.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/config/config.json.sample b/lib/config/config.json.sample index 4e2c92d..58a4227 100644 --- a/lib/config/config.json.sample +++ b/lib/config/config.json.sample @@ -26,7 +26,6 @@ "upstream": "ws://localhost:9125", "retryDelay": 10000, "closeTimer": 15000, - "workerKillTimer": 10000, "enableInstrumentation": true, "instrumentationTimer": 60000 } diff --git a/lib/config/constants.js b/lib/config/constants.js index a8f6707..7bfd183 100644 --- a/lib/config/constants.js +++ b/lib/config/constants.js @@ -151,7 +151,7 @@ class ConfigParser { setWorkerKillTimer() { const { workerKillTimer } = config; - let defaultTimer = 60 * 10 * 1000; + let defaultTimer = 60 * 15 * 1000; if (isNotNumber(workerKillTimer)) { logger.info( `No worker kill timer configured using default (${defaultTimer})` From 05f58aec42c80dd8d5dd8c9269c18a56087e4425 Mon Sep 17 00:00:00 2001 From: Saransh Date: Wed, 22 Sep 2021 15:04:22 +0530 Subject: [PATCH 2/3] handling pings from server --- lib/config/constants.js | 2 ++ lib/core/Context.js | 10 ++++++++++ lib/core/IncomingWebSocket.js | 7 +++++++ lib/core/OutgoingWebSocket.js | 6 ++++++ 4 files changed, 25 insertions(+) diff --git a/lib/config/constants.js b/lib/config/constants.js index 7bfd183..2f48a0c 100644 --- a/lib/config/constants.js +++ b/lib/config/constants.js @@ -13,6 +13,8 @@ const { const kUpstreamClosed = Symbol('kUpstreamClosed'); const kMessageReceived = Symbol('kMessageReceived'); +const kPingReceived = Symbol('kPingReceived'); +const kSendPing = Symbol('kSendPing'); const kError = Symbol('kError'); const kSendMessage = Symbol('kSendMessage'); const kQueueMessage = Symbol('kQueueMessage'); diff --git a/lib/core/Context.js b/lib/core/Context.js index a300274..c7a0c31 100644 --- a/lib/core/Context.js +++ b/lib/core/Context.js @@ -8,6 +8,8 @@ const { createTarget } = require('../util/util'); const { kUpstreamClosed, kMessageReceived, + kPingReceived, + kSendPing, kEnableIncomingQueue, kEnableOutgoingQueue, kError, @@ -107,6 +109,10 @@ class Context extends EventEmitter { logger.debug(`${INCOMING} [${this.connectionId}] [MESSAGE] - ${msg}`); }); + this.incomingSocket.on(kSendPing, () => { + this.incomingSocket.ping(); + }); + this.incomingSocket.on(kDrainMessage, (msg) => { this.outgoingSocket.emit(kSendMessage, msg); }); @@ -165,6 +171,10 @@ class Context extends EventEmitter { this.emit(kAddNewContext, connectionId); }); + this.outgoingSocket.on(kPingReceived, () => { + this.incomingSocket.emit(kSendPing); + }); + this.outgoingSocket.on(kMessageReceived, (msg) => { if (this.outgoingLock) { logger.debug(`${OUTGOING} [${this.connectionId}] [QUEUE] - ${msg}`); diff --git a/lib/core/IncomingWebSocket.js b/lib/core/IncomingWebSocket.js index 300d4d5..3c3dddb 100644 --- a/lib/core/IncomingWebSocket.js +++ b/lib/core/IncomingWebSocket.js @@ -128,6 +128,13 @@ class IncomingWebSocket extends EventEmitter { send(msg) { this.socket.send(msg); } + + /** + * Relays ping from upstream. + */ + ping() { + this.socket.ping(); + } } module.exports = IncomingWebSocket; diff --git a/lib/core/OutgoingWebSocket.js b/lib/core/OutgoingWebSocket.js index a82e187..61f2d00 100644 --- a/lib/core/OutgoingWebSocket.js +++ b/lib/core/OutgoingWebSocket.js @@ -13,6 +13,7 @@ const { kAddNewContext, kReleaseTap, kMessageReceived, + kPingReceived, kError, kUpstreamRestart, kUpstreamClosed, @@ -71,6 +72,7 @@ class OutgoingWebSocket extends EventEmitter { this.socket.on('message', this.messageHandler.bind(this)); this.socket.on('close', this.closeHandler.bind(this)); this.socket.on('error', this.errorHandler.bind(this)); + this.socket.on('ping', this.pingHandler.bind(this)); } /** @@ -108,6 +110,10 @@ class OutgoingWebSocket extends EventEmitter { this.emit(kMessageReceived, msg); } + pingHandler() { + this.emit(kPingReceived); + } + /** * Triggers when socket connection is closed. * From 634be51ff62792bd080847ff9a016a39f7a21839 Mon Sep 17 00:00:00 2001 From: Saransh Date: Thu, 23 Sep 2021 00:40:16 +0530 Subject: [PATCH 3/3] keeping code dir dynamic for cluster --- cluster.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cluster.js b/cluster.js index b768306..7bed360 100644 --- a/cluster.js +++ b/cluster.js @@ -1,17 +1,18 @@ 'use strict'; const cluster = require('cluster'); -const { config } = require('./lib/config/constants.js'); const { watch } = require('fs'); -const logger = require('./lib/util/loggerFactory.js'); -const Proxy = require('./lib/core/Proxy.js'); +const path = require('path'); + +const codeDir = process.env.CODE_DIR || path.join(__dirname, '/'); +const { config } = require(path.join(codeDir, 'lib/config/constants.js')); +const logger = require(path.join(codeDir, 'lib/util/loggerFactory.js')); +const Proxy = require(path.join(codeDir, 'lib/core/Proxy.js')); const WORKER_CNT = config.workerVal; const activeWorkers = []; -const path = require('path'); - -const RESTART_FILE = path.join(__dirname, 'tmp', 'restart.txt'); +const RESTART_FILE = path.join(codeDir, 'tmp/restart.txt'); const forceKill = (worker) => { if (!worker.isDead()) {