Skip to content

Commit

Permalink
Debounce stream management ack requests
Browse files Browse the repository at this point in the history
So that they don't send after every stanza goes out but only when idle.
  • Loading branch information
singpolyma committed Jan 22, 2025
1 parent 20291dc commit 4aa691d
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/stream-management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default function streamManagement({
}) {
let timeoutTimeout = null;
let requestAckTimeout = null;
let requestAckDebounce = null;

const sm = new EventEmitter();
Object.assign(sm, {
Expand All @@ -61,6 +62,7 @@ export default function streamManagement({
max: null,
timeout: 60_000,
requestAckInterval: 30_000,
requestAckDebounce: 100,
});

async function sendAck() {
Expand All @@ -72,6 +74,7 @@ export default function streamManagement({
entity.on("disconnect", () => {
clearTimeout(timeoutTimeout);
clearTimeout(requestAckTimeout);
clearTimeout(requestAckDebounce);
sm.enabled = false;
});

Expand Down Expand Up @@ -199,7 +202,8 @@ export default function streamManagement({

sm.outbound_q.push({ stanza, stamp: datetime() });
// Debounce requests so we send only one after a big run of stanza together
queueMicrotask(requestAck);
clearTimeout(requestAckDebounce);
setTimeout(requestAck, sm.requestAckDebounce);

return next();
});
Expand Down

0 comments on commit 4aa691d

Please sign in to comment.