Skip to content

Commit

Permalink
Flag to stop LDS loop while no timer is active
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-pm authored Sep 10, 2021
1 parent 6692c2a commit 7bfd7bd
Showing 1 changed file with 7 additions and 3 deletions.
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;

const ldSubscription = {
close: async () => {
clearTimeout(loopTimeout);
nextSleepDuration = 0;
await client.close();
},
};

let nextStaleCheck = Date.now() + DROP_STALE_SLOTS_INTERVAL;
async function loop() {
nextSleepDuration = sleepDuration;
try {
const rows = await client.getChanges(null, 500);
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;
}
if (nextSleepDuration) { // else loop has been stopped
loopTimeout = setTimeout(loop, nextSleepDuration);
}
loopTimeout = setTimeout(loop, sleepDuration);
}
loop();
return ldSubscription;
Expand Down

0 comments on commit 7bfd7bd

Please sign in to comment.