Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix-heart-beat-timer #665

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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