Skip to content

Commit

Permalink
Switch isAlive to be connection leng
Browse files Browse the repository at this point in the history
  • Loading branch information
jfberry authored and Fabio1988 committed Nov 13, 2024
1 parent 11278cd commit a93f55f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/client/src/app/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export const makeServer = ({ environment = 'test' } = {}) => {
isAlive() {
return faker.datatype.boolean();
},
heartbeatCheckStatus() {
return faker.datatype.boolean();
},
lastMemory() {
return {
memFree: faker.datatype.number(),
Expand Down Expand Up @@ -69,6 +72,7 @@ export const makeServer = ({ environment = 'test' } = {}) => {
init: faker.datatype.boolean(),
instanceNo: 0,
isAlive: faker.datatype.boolean(),
heartbeatCheckStatus: faker.datatype.boolean(),
noMessagesReceived: 1,
noMessagesSent: 1,
origin: 'none',
Expand Down
11 changes: 8 additions & 3 deletions packages/connections/src/lib/controllerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class ControllerConnection extends EventEmitter {
dateLastMessageSent: number;
instanceNo: number;
workerName: string;
heartbeatCheckStatus: boolean;
isAlive: boolean;
loginListener: number;

Expand All @@ -34,6 +35,7 @@ export class ControllerConnection extends EventEmitter {
this.ws = ws;
this.log = log;
this.dateLastMessageSent = Date.now();
this.heartbeatCheckStatus = true;
this.isAlive = true;
this.workerName = '<unknown>';

Expand Down Expand Up @@ -68,6 +70,7 @@ export class ControllerConnection extends EventEmitter {
* Handle Mitm device disconnection
*/
#handleMitmDisconnection() {
this.isAlive = false;
this.disconnect();
}

Expand Down Expand Up @@ -128,6 +131,7 @@ export class ControllerConnection extends EventEmitter {
}

#handleControllerDisconnection() {
this.heartbeatCheckStatus = false;
this.isAlive = false;
clearInterval(this.heartbeatHandle);

Expand Down Expand Up @@ -157,11 +161,11 @@ export class ControllerConnection extends EventEmitter {
}

heartbeat() {
this.isAlive = true;
this.heartbeatCheckStatus = true;
}

checkHeartbeat() {
if (!this.isAlive) {
if (!this.heartbeatCheckStatus) {
// Pong has not been received in last interval seconds
this.log.warn(
`${this.workerName}/${this.instanceNo}->${this.workerId}: Scanner - No response to ping - forcing disconnect`,
Expand All @@ -182,14 +186,15 @@ export class ControllerConnection extends EventEmitter {
return;
}

this.isAlive = false;
this.heartbeatCheckStatus = false;
this.ws.ping();
}

serialize(): ControllerConnectionDTO {
return {
dateLastMessageSent: this.dateLastMessageSent,
instanceNo: this.instanceNo,
heartbeatCheckStatus: this.heartbeatCheckStatus,
isAlive: this.isAlive,
loginListener: this.loginListener,
origin: this.origin,
Expand Down
10 changes: 7 additions & 3 deletions packages/connections/src/lib/deviceControlConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class DeviceControlConnection extends EventEmitter {
version?: string;
publicIp?: string;
ws: WebSocket;
heartbeatCheckStatus: boolean;
isAlive: boolean;
instanceNo: number;
heartbeatHandle: NodeJS.Timer;
Expand All @@ -52,6 +53,7 @@ export class DeviceControlConnection extends EventEmitter {
this.log = log;
this.ws = ws;
this.init = true;
this.heartbeatCheckStatus = true;
this.isAlive = true;
this.origin = '';

Expand Down Expand Up @@ -125,11 +127,11 @@ export class DeviceControlConnection extends EventEmitter {
}

heartbeat() {
this.isAlive = true;
this.heartbeatCheckStatus = true;
}

checkHeartbeat() {
if (!this.isAlive) {
if (!this.heartbeatCheckStatus) {
// Pong has not been received in last interval seconds
this.log.warn(`${this.deviceId}/${this.instanceNo}: DEVICE - No response to ping - forcing disconnect`);
clearInterval(this.heartbeatHandle);
Expand All @@ -138,11 +140,12 @@ export class DeviceControlConnection extends EventEmitter {
return;
}

this.isAlive = false;
this.heartbeatCheckStatus = false;
this.ws.ping();
}

disconnected() {
this.heartbeatCheckStatus = false;
this.isAlive = false;
clearInterval(this.heartbeatHandle);

Expand Down Expand Up @@ -216,6 +219,7 @@ export class DeviceControlConnection extends EventEmitter {
deviceId: this.deviceId,
init: this.init,
instanceNo: this.instanceNo,
heartbeatCheckStatus: this.heartbeatCheckStatus,
isAlive: this.isAlive,
lastMemory: this.lastMemory,
nextId: this.nextId,
Expand Down
12 changes: 8 additions & 4 deletions packages/connections/src/lib/deviceWorkerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class DeviceWorkerConnection extends EventEmitter {
origin?: string;
version?: string;
ws: WebSocket;
heartbeatCheckStatus: boolean;
isAlive: boolean;
instanceNo: number;
heartbeatHandle: NodeJS.Timer;
Expand All @@ -33,6 +34,7 @@ export class DeviceWorkerConnection extends EventEmitter {
this.ws = ws;
this.log = log;
this.init = true;
this.heartbeatCheckStatus = true;
this.isAlive = true;
this.origin = '';
this.traceMessages = false;
Expand Down Expand Up @@ -101,11 +103,11 @@ export class DeviceWorkerConnection extends EventEmitter {
}

heartbeat() {
this.isAlive = true;
this.heartbeatCheckStatus = true;
}

checkHeartbeat() {
if (!this.isAlive) {
if (!this.heartbeatCheckStatus) {
// Pong has not been received in last interval seconds
this.log.warn(`${this.workerId}/${this.instanceNo}: MITM - No response to ping - forcing disconnect`);
clearInterval(this.heartbeatHandle);
Expand All @@ -114,12 +116,13 @@ export class DeviceWorkerConnection extends EventEmitter {
return;
}

this.isAlive = false;
this.heartbeatCheckStatus = false;
this.ws.ping();
}

disconnected() {
this.isAlive = false;
this.heartbeatCheckStatus = false;
this.isAlive = true;
clearInterval(this.heartbeatHandle);

this.emit('disconnected', this);
Expand All @@ -131,6 +134,7 @@ export class DeviceWorkerConnection extends EventEmitter {
dateLastMessageSent: this.dateLastMessageSent,
init: this.init,
instanceNo: this.instanceNo,
heartbeatCheckStatus: this.heartbeatCheckStatus,
isAlive: this.isAlive,
noMessagesReceived: this.noMessagesReceived,
noMessagesSent: this.noMessagesSent,
Expand Down

0 comments on commit a93f55f

Please sign in to comment.