-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: message batches reduce wallet setup from 80 to 20 chain trips
- Loading branch information
1 parent
12ce7c4
commit 7d17f2f
Showing
6 changed files
with
66 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const DEFAULT_BATCH_TIMEOUT_MS = 1000; | ||
|
||
export function makeBatchedDeliver( | ||
deliver, | ||
batchTimeoutMs = DEFAULT_BATCH_TIMEOUT_MS, | ||
) { | ||
let batchedMessages = []; | ||
let latestAckNum = 0; | ||
let deliverTimeout; | ||
|
||
async function batchedDeliver(newMessages, ackNum) { | ||
// If we have no existing messages, reset the deliver timeout. | ||
// | ||
// This defers sending an ack until the timeout expires or we have new | ||
// messages. | ||
if (!batchedMessages.length) { | ||
clearTimeout(deliverTimeout); | ||
deliverTimeout = setTimeout(() => { | ||
// Transfer the batched messages to the deliver function. | ||
const msgs = batchedMessages; | ||
batchedMessages = []; | ||
deliver(msgs, latestAckNum); | ||
}, batchTimeoutMs); | ||
} | ||
|
||
// Add new messages to the batch. | ||
batchedMessages.push(...newMessages); | ||
if (ackNum > latestAckNum) { | ||
// Increase the latest ack. | ||
latestAckNum = ackNum; | ||
} | ||
} | ||
|
||
return batchedDeliver; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters