-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add failed HTTP request retry policy (#259)
- Loading branch information
1 parent
c286280
commit eaec51a
Showing
16 changed files
with
524 additions
and
19 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
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
37 changes: 37 additions & 0 deletions
37
clients/imodels-client-management/src/base/internal/AxiosRetryPolicy.ts
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,37 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
import { isAxiosError } from "axios"; | ||
|
||
import { Constants } from "../../Constants"; | ||
import { GetSleepDurationInMsParams, HttpRequestRetryPolicy, ShouldRetryParams } from "../types"; | ||
|
||
import { BackoffAlgorithm } from "./ExponentialBackoffAlgorithm"; | ||
|
||
/** Default implementation for {@link HttpRequestRetryPolicy}. */ | ||
export class AxiosRetryPolicy implements HttpRequestRetryPolicy { | ||
private readonly _backoffAlgorithm: BackoffAlgorithm; | ||
|
||
public constructor(params: { | ||
maxRetries: number; | ||
backoffAlgorithm: BackoffAlgorithm; | ||
}) { | ||
this.maxRetries = params.maxRetries; | ||
this._backoffAlgorithm = params.backoffAlgorithm; | ||
} | ||
|
||
public readonly maxRetries: number; | ||
|
||
public shouldRetry(params: ShouldRetryParams): boolean { | ||
if (isAxiosError(params.error) && params.error.response?.status != null) { | ||
return params.error.response.status >= Constants.httpStatusCodes.internalServerError; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public getSleepDurationInMs(params: GetSleepDurationInMsParams): number { | ||
return this._backoffAlgorithm.getSleepDurationInMs(params.retriesInvoked); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
clients/imodels-client-management/src/base/internal/ExponentialBackoffAlgorithm.ts
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,31 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
/** Backoff algorithm for calculating sleep time after a failed HTTP request. */ | ||
export interface BackoffAlgorithm { | ||
/** Calculates the sleep duration in milliseconds. */ | ||
getSleepDurationInMs: (attempt: number) => number; | ||
} | ||
|
||
/** | ||
* Exponential backoff algorithm for calculating sleep time after a failed HTTP request. | ||
* Default implementation for {@link BackoffAlgorithm}. | ||
*/ | ||
export class ExponentialBackoffAlgorithm implements BackoffAlgorithm { | ||
private readonly _baseDelayInMs: number; | ||
private readonly _factor: number; | ||
|
||
public constructor(params: { | ||
baseDelayInMs: number; | ||
factor: number; | ||
}) { | ||
this._baseDelayInMs = params.baseDelayInMs; | ||
this._factor = params.factor; | ||
} | ||
|
||
public getSleepDurationInMs(attempt: number): number { | ||
return Math.pow(this._factor, attempt) * this._baseDelayInMs; | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
clients/imodels-client-management/src/base/types/HttpRequestRetryPolicy.ts
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,28 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
export interface ShouldRetryParams { | ||
/** The number of already invoked retries, starting from 0. */ | ||
retriesInvoked: number; | ||
/** The error that was thrown when sending the HTTP request. */ | ||
error: unknown; | ||
} | ||
|
||
export interface GetSleepDurationInMsParams { | ||
/** The number of already invoked retries, starting from 0. */ | ||
retriesInvoked: number; | ||
} | ||
|
||
/** A policy for handling failed HTTP requests. */ | ||
export interface HttpRequestRetryPolicy { | ||
/** The maximum number of HTTP request retries. */ | ||
get maxRetries(): number; | ||
|
||
/** Returns `true` if HTTP request should be retried, `false` otherwise. */ | ||
shouldRetry(params: ShouldRetryParams): boolean | Promise<boolean>; | ||
|
||
/** Gets the duration to sleep in milliseconds before resending the HTTP request. */ | ||
getSleepDurationInMs: (params: GetSleepDurationInMsParams) => number; | ||
} |
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
10 changes: 10 additions & 0 deletions
10
common/changes/@itwin/imodels-client-authoring/ab-add-retry-policy_2024-06-18-06-49.json
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,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@itwin/imodels-client-authoring", | ||
"comment": "Add failed HTTP request retry policy.", | ||
"type": "minor" | ||
} | ||
], | ||
"packageName": "@itwin/imodels-client-authoring" | ||
} |
10 changes: 10 additions & 0 deletions
10
common/changes/@itwin/imodels-client-management/ab-add-retry-policy_2024-06-18-06-49.json
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,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@itwin/imodels-client-management", | ||
"comment": "Add failed HTTP request retry policy.", | ||
"type": "minor" | ||
} | ||
], | ||
"packageName": "@itwin/imodels-client-management" | ||
} |
Oops, something went wrong.