-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Retry fetch requests and disable dispatch on failure. (#174)
- Loading branch information
Showing
6 changed files
with
350 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { HttpHandler, HttpRequest, HttpResponse } from '@aws-sdk/protocol-http'; | ||
|
||
export type BackoffFunction = (retry: number) => number; | ||
|
||
/** | ||
* An HttpHandler which wraps other HttpHandlers to retry requests. | ||
* | ||
* Requests will be retried if (1) there is an error (e.g., with the network or | ||
* credentials) and the promise rejects, or (2) the response status is not 2xx. | ||
*/ | ||
export class RetryHttpHandler implements HttpHandler { | ||
private handler: HttpHandler; | ||
private retries: number; | ||
private backoff: BackoffFunction; | ||
|
||
public constructor( | ||
handler: HttpHandler, | ||
retries: number, | ||
backoff: BackoffFunction = (n) => n * 2000 | ||
) { | ||
this.handler = handler; | ||
this.retries = retries; | ||
this.backoff = backoff; | ||
} | ||
|
||
public async handle( | ||
request: HttpRequest | ||
): Promise<{ response: HttpResponse }> { | ||
let retriesLeft = this.retries; | ||
while (true) { | ||
try { | ||
const response = await this.handler.handle(request); | ||
if (this.isStatusCode2xx(response.response.statusCode)) { | ||
return response; | ||
} | ||
throw new Error(`${response.response.statusCode}`); | ||
} catch (e) { | ||
if (!retriesLeft) { | ||
throw e; | ||
} | ||
retriesLeft--; | ||
await this.sleep(this.backoff(this.retries - retriesLeft)); | ||
} | ||
} | ||
} | ||
|
||
private async sleep(milliseconds): Promise<void> { | ||
return new Promise<void>((resolve) => | ||
setTimeout(resolve, milliseconds) | ||
); | ||
} | ||
|
||
private isStatusCode2xx = (statusCode: number): boolean => { | ||
return statusCode >= 200 && statusCode < 300; | ||
}; | ||
} |
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
Oops, something went wrong.