Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-rate-limiter-throttle-detection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@smithy/util-retry": patch
---

fix(util-retry): detect throttling errors from RetryErrorInfo.errorType in DefaultRateLimiter
39 changes: 39 additions & 0 deletions packages/util-retry/src/DefaultRateLimiter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,43 @@ describe(DefaultRateLimiter.name, () => {
expect(parseFloat(rateLimiter["fillRate"].toFixed(6))).toEqual(fillRate);
});
});

describe("throttle detection via RetryErrorInfo.errorType", () => {
it("enables the rate limiter when errorType is THROTTLING", () => {
vi.spyOn(Date, "now").mockImplementation(() => 0);
const rateLimiter = new DefaultRateLimiter();
expect(rateLimiter["enabled"]).toBe(false);

vi.spyOn(Date, "now").mockImplementation(() => 1000);
rateLimiter.updateClientSendingRate({ errorType: "THROTTLING" });
expect(rateLimiter["enabled"]).toBe(true);
});

it("does not enable the rate limiter for non-throttling errorType", () => {
vi.spyOn(Date, "now").mockImplementation(() => 0);
const rateLimiter = new DefaultRateLimiter();

vi.spyOn(Date, "now").mockImplementation(() => 1000);
rateLimiter.updateClientSendingRate({ errorType: "TRANSIENT" });
expect(rateLimiter["enabled"]).toBe(false);
});

it("does not call isThrottlingError when errorType is present", () => {
vi.spyOn(Date, "now").mockImplementation(() => 0);
const rateLimiter = new DefaultRateLimiter();

vi.spyOn(Date, "now").mockImplementation(() => 1000);
rateLimiter.updateClientSendingRate({ errorType: "THROTTLING" });
expect(isThrottlingError).not.toHaveBeenCalled();
});

it("falls back to isThrottlingError when errorType is absent", () => {
vi.spyOn(Date, "now").mockImplementation(() => 0);
const rateLimiter = new DefaultRateLimiter();

vi.spyOn(Date, "now").mockImplementation(() => 1000);
rateLimiter.updateClientSendingRate({});
expect(isThrottlingError).toHaveBeenCalled();
});
});
});
10 changes: 9 additions & 1 deletion packages/util-retry/src/DefaultRateLimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class DefaultRateLimiter implements RateLimiter {
let calculatedRate: number;
this.updateMeasuredRate();

if (isThrottlingError(response)) {
if (this.isThrottlingResponse(response)) {
const rateToUse = !this.enabled ? this.measuredTxRate : Math.min(this.measuredTxRate, this.fillRate);
this.lastMaxRate = rateToUse;
this.calculateTimeWindow();
Expand All @@ -113,6 +113,14 @@ export class DefaultRateLimiter implements RateLimiter {
this.updateTokenBucketRate(newRate);
}

private isThrottlingResponse(response: any): boolean {
if (response?.errorType) {
return response.errorType === "THROTTLING";
}
return isThrottlingError(response);
}


private calculateTimeWindow() {
this.timeWindow = this.getPrecise(Math.pow((this.lastMaxRate * (1 - this.beta)) / this.scaleConstant, 1 / 3));
}
Expand Down