Merge pull request #10907 from jfsiebel/add-custom-heartbeat-flag

Add flag to enable/disabled custom heartbeat
This commit is contained in:
Anton Georgiev 2020-11-26 10:08:55 -05:00 committed by GitHub
commit 38ba6bc7db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 50 deletions

View File

@ -23,60 +23,65 @@ Meteor.startup(() => {
const CDN_URL = APP_CONFIG.cdn; const CDN_URL = APP_CONFIG.cdn;
let heapDumpMbThreshold = 100; let heapDumpMbThreshold = 100;
// https://github.com/sockjs/sockjs-node/blob/1ef08901f045aae7b4df0f91ef598d7a11e82897/lib/transport/websocket.js#L74-L82 const { customHeartbeat } = APP_CONFIG;
const newHeartbeat = function heartbeat() {
const currentTime = new Date().getTime();
// Skipping heartbeat, because websocket is sending data if (customHeartbeat) {
if (currentTime - this.ws.lastSentFrameTimestamp < 10000) { Logger.warn('Custom heartbeat functions are enabled');
Logger.info('Skipping heartbeat, because websocket is sending data', { // https://github.com/sockjs/sockjs-node/blob/1ef08901f045aae7b4df0f91ef598d7a11e82897/lib/transport/websocket.js#L74-L82
currentTime, const newHeartbeat = function heartbeat() {
lastSentFrameTimestamp: this.ws.lastSentFrameTimestamp, const currentTime = new Date().getTime();
userId: this.session.connection._meteorSession.userId,
});
return;
}
const supportsHeartbeats = this.ws.ping(null, () => clearTimeout(this.hto_ref)); // Skipping heartbeat, because websocket is sending data
if (supportsHeartbeats) { if (currentTime - this.ws.lastSentFrameTimestamp < 10000) {
this.hto_ref = setTimeout(() => { Logger.info('Skipping heartbeat, because websocket is sending data', {
Logger.info('Heartbeat timeout', { userId: this.session.connection._meteorSession.userId, sentAt: currentTime, now: new Date().getTime() }); currentTime,
}, Meteor.server.options.heartbeatTimeout); lastSentFrameTimestamp: this.ws.lastSentFrameTimestamp,
} else { userId: this.session.connection._meteorSession.userId,
Logger.error('Unexpected error supportsHeartbeats=false'); });
} return;
};
// https://github.com/davhani/hagty/blob/6a5c78e9ae5a5e4ade03e747fb4cc8ea2df4be0c/faye-websocket/lib/faye/websocket/api.js#L84-L88
const newSend = function send(data) {
this.lastSentFrameTimestamp = new Date().getTime();
// Call https://github.com/meteor/meteor/blob/1e7e56eec8414093cd0c1c70750b894069fc972a/packages/ddp-common/heartbeat.js#L80-L88
this.meteorHeartbeat._seenPacket = true;
if (this.meteorHeartbeat._heartbeatTimeoutHandle) {
this.meteorHeartbeat._clearHeartbeatTimeoutTimer();
}
if (this.readyState > 1/* API.OPEN = 1 */) return false;
if (!(data instanceof Buffer)) data = String(data);
return this._driver.messages.write(data);
};
Meteor.setInterval(() => {
for (const session of Meteor.server.sessions.values()) {
const { socket } = session;
const recv = socket._session.recv;
if (session.bbbFixApplied || !recv || !recv.ws) {
continue;
} }
recv.ws.meteorHeartbeat = session.heartbeat; const supportsHeartbeats = this.ws.ping(null, () => clearTimeout(this.hto_ref));
recv.__proto__.heartbeat = newHeartbeat; if (supportsHeartbeats) {
recv.ws.__proto__.send = newSend; this.hto_ref = setTimeout(() => {
session.bbbFixApplied = true; Logger.info('Heartbeat timeout', { userId: this.session.connection._meteorSession.userId, sentAt: currentTime, now: new Date().getTime() });
} }, Meteor.server.options.heartbeatTimeout);
}, 5000); } else {
Logger.error('Unexpected error supportsHeartbeats=false');
}
};
// https://github.com/davhani/hagty/blob/6a5c78e9ae5a5e4ade03e747fb4cc8ea2df4be0c/faye-websocket/lib/faye/websocket/api.js#L84-L88
const newSend = function send(data) {
this.lastSentFrameTimestamp = new Date().getTime();
// Call https://github.com/meteor/meteor/blob/1e7e56eec8414093cd0c1c70750b894069fc972a/packages/ddp-common/heartbeat.js#L80-L88
this.meteorHeartbeat._seenPacket = true;
if (this.meteorHeartbeat._heartbeatTimeoutHandle) {
this.meteorHeartbeat._clearHeartbeatTimeoutTimer();
}
if (this.readyState > 1/* API.OPEN = 1 */) return false;
if (!(data instanceof Buffer)) data = String(data);
return this._driver.messages.write(data);
};
Meteor.setInterval(() => {
for (const session of Meteor.server.sessions.values()) {
const { socket } = session;
const recv = socket._session.recv;
if (session.bbbFixApplied || !recv || !recv.ws) {
continue;
}
recv.ws.meteorHeartbeat = session.heartbeat;
recv.__proto__.heartbeat = newHeartbeat;
recv.ws.__proto__.send = newSend;
session.bbbFixApplied = true;
}
}, 5000);
}
const memoryMonitoringSettings = Meteor.settings.private.memoryMonitoring; const memoryMonitoringSettings = Meteor.settings.private.memoryMonitoring;
if (memoryMonitoringSettings.stat.enabled) { if (memoryMonitoringSettings.stat.enabled) {

View File

@ -36,6 +36,7 @@ public:
# can generate excessive overhead to the server. We recommend # can generate excessive overhead to the server. We recommend
# this value to be kept under 12. # this value to be kept under 12.
breakoutRoomLimit: 8 breakoutRoomLimit: 8
customHeartbeat: false
defaultSettings: defaultSettings:
application: application:
animations: true animations: true