Skip to content

Commit

Permalink
perf: reduce number of concurrent requests
Browse files Browse the repository at this point in the history
improve performance and reduce load on the API:

1. solve a bug with the retry mechanism - now it will retry less times
2. exponential backoff to the retry mechanism
3. reduce the number of concurrent requests
  • Loading branch information
NoamGaash authored Nov 15, 2024
1 parent b028f44 commit 01fa2fc
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/api/useVehicleLocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,22 @@ class LocationObservable {
}

const pool = new Array(10).fill(0).map(() => Promise.resolve<void | Response>(void 0))
async function fetchWithQueue(url: string, retries = 10) {
let queue = pool.shift()!
queue = queue
.then(() => fetch(url))
.catch(async () => {
await new Promise((resolve) => setTimeout(resolve, 10000 * Math.random() + 100))
return fetchWithQueue(url, retries - 1)
})
pool.push(queue)
return queue
async function fetchWithQueue(url: string) {
const task = async () => {
for (let attempt = 0; attempt < 3; attempt++) {
try {
return await fetch(url);

Check failure on line 121 in src/api/useVehicleLocations.ts

View workflow job for this annotation

GitHub Actions / local-tests

Delete `;`
} catch {
if (attempt === 2) throw new Error(`Failed after 3 attempts`);

Check failure on line 123 in src/api/useVehicleLocations.ts

View workflow job for this annotation

GitHub Actions / local-tests

Delete `;`
await new Promise((resolve) => setTimeout(resolve, 1000 * 2 ** attempt));

Check failure on line 124 in src/api/useVehicleLocations.ts

View workflow job for this annotation

GitHub Actions / local-tests

Delete `;`
}
}
};

Check failure on line 127 in src/api/useVehicleLocations.ts

View workflow job for this annotation

GitHub Actions / local-tests

Delete `;`

const queue = pool.shift()!;

Check failure on line 129 in src/api/useVehicleLocations.ts

View workflow job for this annotation

GitHub Actions / local-tests

Delete `;`
const result = queue.then(task).finally(() => pool.push(Promise.resolve()));

Check failure on line 130 in src/api/useVehicleLocations.ts

View workflow job for this annotation

GitHub Actions / local-tests

Delete `;`
pool.push(result);

Check failure on line 131 in src/api/useVehicleLocations.ts

View workflow job for this annotation

GitHub Actions / local-tests

Delete `;`
return result;

Check failure on line 132 in src/api/useVehicleLocations.ts

View workflow job for this annotation

GitHub Actions / local-tests

Delete `;`
}

// this function checks the cache for the data, and if it's not there, it loads it
Expand Down

0 comments on commit 01fa2fc

Please sign in to comment.