I need to write a pure client side JS function that will take in a potentially very large queue of Google API specs (URL, headers, body, content type, etc.) and fetch them. It will fetch 4 concurrently at a time. The issue is that I know some requests may hit the 429 error. I am trying to figure out how best to architect my code to handle this.
How it will work:
- Each request in the queue is one of two types:
- solo requests are just single/normal requests
- batchable requests are requests that can be multipart batched with other requests in the queue for the same endpoint
- Both solo responses, and the individual responses in a batch request may have a
nextPageToken. This will result in a new solo or batchable request being added back to the queue. - There are 3 different 429 possible situations:
- solo request could get a 429 response
- main batch request could get a 429 response
- 1 or more individual items in a batch request could get a 429 response
This is what I have so far:
Start Workers
- Take the first item from the
queue- If it is a solo request, call an
async function doItto process it - If it is a batchable request, look for the next up to 50 requests in the
queuethat are for the same API endpoint and create a single batch request and send that single batch request todoIt
- If it is a solo request, call an
- Repeat #1 until we have
maxConcurrentWorkers(4) concurrent executions/workers ofdoItor there are no more items in thequeue.
doIt
- Check if we need to wait; if so async wait/sleep (using
PromiseandsetTimeout) - Make an
await fetchcall - Check/process the response:
- For solo requests:
- If the response is good:
- Take the response and send it to a function to be processed
- If there is a
nextPageToken, add a new solo request to thequeue - Start the next worker to keep processing the
queue
- If the response is 429:
- Add the request back to the
queueas a solo request - Set a global variable that tells the other workers to wait
xseconds (#1 step indoIt) - Start the next worker to keep processing the
queue
- Add the request back to the
- If the response is good:
- For batch requests:
- If the response is 429 (meaning the entire batch request failed):
- Iterate over each individual request in the batch request and readd them back to the queue
- set a global variable that tells the other workers to wait
xseconds - Start the next worker to keep processing the
queue
- If the response is good, iterate over each individual response and check their HTTP status
- If the individual batch item response is good:
- Take the response and send it to a function to be processed
- If there is a
nextPageToken, add a new batchable request to thequeue - Start the next worker to keep processing the
queue
- If the individual batch item response is 429:
- Add that specific request back to the
queueas a batchable request - Set a global variable that tells the other workers to wait
xseconds - Start the next worker to keep processing the
queue
- Add that specific request back to the
- If the individual batch item response is good:
- If the response is 429 (meaning the entire batch request failed):
- For solo requests:
Does this logic make sense? Does it look good? Are there any cases/situations I am not accounting for? If so, what are they and how would/should I account for them?
I need to write all the code for this. I cannot use any libraries, including Google JavaScript client libraries.