fix interrupt sync desync issues
This commit is contained in:
parent
98f2c39e6e
commit
790aa4959d
@ -15,6 +15,8 @@ const requestedRates = {};
|
|||||||
let timer = null;
|
let timer = null;
|
||||||
// previous cluster state for interrupt handler
|
// previous cluster state for interrupt handler
|
||||||
let prevState = {};
|
let prevState = {};
|
||||||
|
// target ms value
|
||||||
|
let targetMSTime = null;
|
||||||
|
|
||||||
export function setupClientSync (app, server, options) {
|
export function setupClientSync (app, server, options) {
|
||||||
const schemes = options.schemes;
|
const schemes = options.schemes;
|
||||||
@ -101,6 +103,7 @@ export function setupClientSync (app, server, options) {
|
|||||||
if (Object.keys(requestedRates).length === 0) { // if there are no requested rates left, clear the timer
|
if (Object.keys(requestedRates).length === 0) { // if there are no requested rates left, clear the timer
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
timer = null;
|
timer = null;
|
||||||
|
targetMSTime = null;
|
||||||
}
|
}
|
||||||
// terminate socket
|
// terminate socket
|
||||||
socket.terminate();
|
socket.terminate();
|
||||||
@ -116,6 +119,8 @@ export function setupClientSync (app, server, options) {
|
|||||||
// if timer has not started, start it with requested rate
|
// if timer has not started, start it with requested rate
|
||||||
if (!timer) {
|
if (!timer) {
|
||||||
timer = setTimeout(handleInterruptSync, rate);
|
timer = setTimeout(handleInterruptSync, rate);
|
||||||
|
const time = global.process.uptime();
|
||||||
|
targetMSTime = time - Math.floor(time);
|
||||||
}
|
}
|
||||||
// otherwise, if the timer has started but the rate is lower than the current minimum
|
// otherwise, if the timer has started but the rate is lower than the current minimum
|
||||||
// AND if the next event trigger is more than the new rate in the future,
|
// AND if the next event trigger is more than the new rate in the future,
|
||||||
@ -124,6 +129,8 @@ export function setupClientSync (app, server, options) {
|
|||||||
else if (rate < Math.min.apply(null, Object.values(requestedRates)) && getTimeLeft(timer) > rate) {
|
else if (rate < Math.min.apply(null, Object.values(requestedRates)) && getTimeLeft(timer) > rate) {
|
||||||
clearTimeout(timer);
|
clearTimeout(timer);
|
||||||
timer = setTimeout(handleInterruptSync, rate);
|
timer = setTimeout(handleInterruptSync, rate);
|
||||||
|
const time = global.process.uptime();
|
||||||
|
targetMSTime = time - Math.floor(time);
|
||||||
}
|
}
|
||||||
// otherwise just add the rate to the list, when the next even trigger happens it will be requeued with the new requested rates
|
// otherwise just add the rate to the list, when the next even trigger happens it will be requeued with the new requested rates
|
||||||
requestedRates[socket.rateIndex] = rate;
|
requestedRates[socket.rateIndex] = rate;
|
||||||
@ -159,11 +166,14 @@ export function setupClientSync (app, server, options) {
|
|||||||
const minRequestedRate = Math.min.apply(null, Object.values(requestedRates));
|
const minRequestedRate = Math.min.apply(null, Object.values(requestedRates));
|
||||||
// if the minimum rate is not Infinity, schedule the next timer
|
// if the minimum rate is not Infinity, schedule the next timer
|
||||||
if (minRequestedRate < Infinity) {
|
if (minRequestedRate < Infinity) {
|
||||||
timer = setTimeout(handleInterruptSync, minRequestedRate);
|
const time = global.process.uptime() - targetMSTime;
|
||||||
|
const delay = (time - Math.round(time)) * 1000;
|
||||||
|
timer = setTimeout(handleInterruptSync, minRequestedRate - delay);
|
||||||
}
|
}
|
||||||
// if the minimum rate is Infinity, then don't schedule anything and set timer to null
|
// if the minimum rate is Infinity, then don't schedule anything and set timer to null
|
||||||
else {
|
else {
|
||||||
timer = null;
|
timer = null;
|
||||||
|
targetMSTime = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// get current cluster resources
|
// get current cluster resources
|
||||||
|
Loading…
Reference in New Issue
Block a user