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

Flag to stop LDS loop while no timer is active #756

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
10 changes: 7 additions & 3 deletions packages/lds/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,19 @@ export default async function subscribeToLogicalDecoding(
}

let loopTimeout: NodeJS.Timer;
let nextSleepDuration = 0;
benjie marked this conversation as resolved.
Show resolved Hide resolved

const ldSubscription = {
close: async () => {
clearTimeout(loopTimeout);
nextSleepDuration = 0;
benjie marked this conversation as resolved.
Show resolved Hide resolved
await client.close();
},
};

let nextStaleCheck = Date.now() + DROP_STALE_SLOTS_INTERVAL;
async function loop() {
nextSleepDuration = sleepDuration;
benjie marked this conversation as resolved.
Show resolved Hide resolved
try {
const rows = await client.getChanges(null, 500);
benjie marked this conversation as resolved.
Show resolved Hide resolved
if (rows.length) {
Expand Down Expand Up @@ -161,10 +164,11 @@ export default async function subscribeToLogicalDecoding(
} catch (e) {
console.error("Error during LDS loop:", e.message);
// Recovery time...
loopTimeout = setTimeout(loop, sleepDuration * 10);
return;
nextSleepDuration *= 10;
benjie marked this conversation as resolved.
Show resolved Hide resolved
}
if (nextSleepDuration) { // else loop has been stopped
benjie marked this conversation as resolved.
Show resolved Hide resolved
loopTimeout = setTimeout(loop, nextSleepDuration);
}
benjie marked this conversation as resolved.
Show resolved Hide resolved
loopTimeout = setTimeout(loop, sleepDuration);
}
benjie marked this conversation as resolved.
Show resolved Hide resolved
loop();
return ldSubscription;
Expand Down