Skip to content

Commit

Permalink
Merge pull request #665 from prey/Fix-heart-beat-timer
Browse files Browse the repository at this point in the history
Fix-heart-beat-timer
  • Loading branch information
SoraKenji authored Sep 7, 2022
2 parents 6251c0d + 72476f9 commit 535a234
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/agent/plugins/control-panel/websockets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let hooks;
let config;
let ws;
let pingTimeout;
let pingPongTimeout;
let emitter;
let setAliveTimeInterval = null;
let setIntervalWSStatus = null;
Expand All @@ -31,6 +32,7 @@ let idTimeoutToCancel;
let websocketConnected = false;

let startupTimeout = 5000;
let heartbeatTimeout = 120000 + 1000;

exports.re_schedule = true;
exports.responses_queue = [];
Expand Down Expand Up @@ -82,14 +84,14 @@ const updateStoredConnection = (newStoredTime) => {
})
}
exports.heartbeat = () => {
if (!ws || !ws.readyState || ws.readyState !== 1) exports.heartbeatTimed(15000);
if (!ws || !ws.readyState || ws.readyState !== 1) restartWebsocketCall();
};

exports.heartbeatTimed = (timeToRetry) => {
exports.heartbeatTimed = () => {
if (pingTimeout) clearTimeout(pingTimeout);
pingTimeout = setTimeout(() => {
restartWebsocketCall();
}, timeToRetry);
}, heartbeatTimeout);
};

const restartWebsocketCall = () => {
Expand Down Expand Up @@ -180,6 +182,7 @@ exports.startWebsocket = () => {
ws = new WebSocket(`${protocol}://${url}`, options);
websocketConnected = true;
ws.on('open', () => {
pingInterval = setInterval(() => { ws.ping() }, 60000);
exports.notify_status(status);
storage.do('all', { type: 'responses' }, (errs, actions) => {
if (!actions || typeof actions === 'undefined') return;
Expand Down Expand Up @@ -250,6 +253,7 @@ exports.startWebsocket = () => {
});

ws.on('ping', () => {
heartbeatTimed();
if (!ws || !ws.readyState || ws.readyState !== 1) return;
ws.pong();
});
Expand Down Expand Up @@ -374,6 +378,8 @@ const clearIntervals = (aliveTimeReset = false) => {
if(notifyActionInterval) clearInterval(notifyActionInterval);
if(getStatusInterval) clearInterval(getStatusInterval);
if(setIntervalWSStatus) clearInterval(setIntervalWSStatus);
if(pingTimeout) clearInterval(pingTimeout);
if(pingInterval) clearIntervals(pingInterval);
if(setAliveTimeInterval && aliveTimeReset) clearInterval(setAliveTimeInterval);
}

Expand Down

0 comments on commit 535a234

Please sign in to comment.