Last active
September 2, 2022 20:20
-
-
Save kurtismash/743b383b311d8227fc16545ffa46c650 to your computer and use it in GitHub Desktop.
Concept - Retrieve and process batches >10 messages from SQS
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
let msgIdx = 0; | |
const randomInt = (min, max) => { | |
return Math.floor(Math.random() * (max - min + 1) + min); | |
}; | |
const getMessages = (batchSize) => { | |
const messages = []; | |
for (let i = 0; i < batchSize; i += 1) { | |
messages.push({ idx: msgIdx, timeout: randomInt(0, 20) }); | |
msgIdx += 1; | |
} | |
return messages; | |
}; | |
const sleep = (ms) => new Promise((r) => setTimeout(r, ms * 1000)); | |
const doThing = async (msg) => { | |
await sleep(msg.timeout); | |
console.log(msg.idx, `done (${msg.timeout})`); | |
return undefined; | |
}; | |
const maxInFlight = 30; | |
let inFlight = 0; | |
const batchSize = 10; | |
(async () => { | |
while (true) { | |
console.log("inFlight:", inFlight); | |
if (inFlight + batchSize <= maxInFlight) { | |
const newMessages = getMessages(batchSize); | |
for (const msg of newMessages) { | |
doThing(msg) | |
.then(() => (inFlight -= 1)) | |
.catch(() => (inFlight -= 1)); | |
inFlight += 1; | |
} | |
} | |
//console.log("inFlight (after)", inFlight); | |
await sleep(0.2); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment