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(NODE-3116): reschedule unreliable async interval first #3006

Merged
merged 3 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
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
32 changes: 16 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ export function makeInterruptibleAsyncInterval(
): InterruptibleAsyncInterval {
let timerId: NodeJS.Timeout | undefined;
let lastCallTime: number;
let lastWakeTime: number;
let cannotBeExpedited = false;
let stopped = false;

options = options ?? {};
Expand All @@ -965,34 +965,34 @@ export function makeInterruptibleAsyncInterval(

function wake() {
const currentTime = clock();
const timeSinceLastWake = currentTime - lastWakeTime;
const timeSinceLastCall = currentTime - lastCallTime;
const timeUntilNextCall = interval - timeSinceLastCall;
lastWakeTime = currentTime;
const nextScheduledCallTime = lastCallTime + interval;
const timeUntilNextCall = nextScheduledCallTime - currentTime;

// For the streaming protocol: there is nothing obviously stopping this
// interval from being woken up again while we are waiting "infinitely"
// for `fn` to be called again`. Since the function effectively
// never completes, the `timeUntilNextCall` will continue to grow
// negatively unbounded, so it will never trigger a reschedule here.

// This is possible in virtualized environments like AWS Lambda where our
// clock is unreliable. In these cases the timer is "running" but never
// actually completes, so we want to execute immediately and then attempt
// to reschedule.
if (timeUntilNextCall < 0) {
executeAndReschedule();
return;
}

// debounce multiple calls to wake within the `minInterval`
if (timeSinceLastWake < minInterval) {
if (cannotBeExpedited) {
return;
}

// reschedule a call as soon as possible, ensuring the call never happens
// faster than the `minInterval`
if (timeUntilNextCall > minInterval) {
reschedule(minInterval);
}

// This is possible in virtualized environments like AWS Lambda where our
// clock is unreliable. In these cases the timer is "running" but never
// actually completes, so we want to execute immediately and then attempt
// to reschedule.
if (timeUntilNextCall < 0) {
executeAndReschedule();
cannotBeExpedited = true;
}
}

Expand All @@ -1004,7 +1004,7 @@ export function makeInterruptibleAsyncInterval(
}

lastCallTime = 0;
lastWakeTime = 0;
cannotBeExpedited = false;
}

function reschedule(ms?: number) {
Expand All @@ -1017,7 +1017,7 @@ export function makeInterruptibleAsyncInterval(
}

function executeAndReschedule() {
lastWakeTime = 0;
cannotBeExpedited = false;
lastCallTime = clock();

fn(err => {
Expand Down
Loading