Skip to content

Commit

Permalink
Merge pull request #10588 from DestinyItemManager/rate-limit
Browse files Browse the repository at this point in the history
Rate limit on arrival rate
  • Loading branch information
bhollis authored Jun 22, 2024
2 parents 7060cf9 + 363401f commit ab5440a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/app/bungie-api/rate-limit-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@ export default function setupRateLimiter() {
1000,
),
);
addLimiter(
new RateLimiterQueue(
/www\.bungie\.net\/Platform\/Destiny2\/\d+\/Profile\/\d+\/Character\/\d+\/Vendors\/\d+\//,
100,
),
);
}
2 changes: 1 addition & 1 deletion src/app/bungie-api/rate-limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ export class RateLimiterQueue {
if (this.canProcess()) {
const config = this.queue.shift()!;
this.count++;
this.lastRequestTime = window.performance.now();
config
.fetcher(config.request, config.options)
.finally(() => {
this.count--;
this.lastRequestTime = window.performance.now();
this.processQueue();
})
.then(config.resolver, config.rejecter);
Expand Down
48 changes: 29 additions & 19 deletions src/app/vendors/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,38 @@ export function loadAllVendors(
);

const vendorResponses: [vendorHash: number, DestinyVendorResponse][] = [];
const promises: Promise<void>[] = [];
for (const vendorHash of vendorsNeedingComponents) {
try {
start = Date.now();
const vendorResponse = await getVendorSaleComponents(account, characterId, vendorHash);
timings.componentsTime += Date.now() - start;
timings.componentsFetched++;
if (isVendorsPage) {
dispatch(
loadedVendorComponents({
vendorResponses: [[vendorHash, vendorResponse]],
promises.push(
(async () => {
try {
start = Date.now();
const vendorResponse = await getVendorSaleComponents(
account,
characterId,
}),
);
} else {
vendorResponses.push([vendorHash, vendorResponse]);
}
} catch {
// TO-DO: what to do here if a single vendor component call fails?
// not necessarily knock the overall vendors state into error mode.
// maybe retry failed single-vendors later? add them to a new list in vendors state?
}
vendorHash,
);
timings.componentsTime += Date.now() - start;
timings.componentsFetched++;
if (isVendorsPage) {
dispatch(
loadedVendorComponents({
vendorResponses: [[vendorHash, vendorResponse]],
characterId,
}),
);
} else {
vendorResponses.push([vendorHash, vendorResponse]);
}
} catch {
// TO-DO: what to do here if a single vendor component call fails?
// not necessarily knock the overall vendors state into error mode.
// maybe retry failed single-vendors later? add them to a new list in vendors state?
}
})(),
);
}
await Promise.all(promises);
if (!isVendorsPage) {
dispatch(loadedVendorComponents({ vendorResponses, characterId }));
}
Expand Down

0 comments on commit ab5440a

Please sign in to comment.