UNPKG

4.11 kBTypeScriptView Raw
1/**
2 * @public
3 */
4export type RetryErrorType =
5/**
6 * This is a connection level error such as a socket timeout, socket connect
7 * error, tls negotiation timeout etc...
8 * Typically these should never be applied for non-idempotent request types
9 * since in this scenario, it's impossible to know whether the operation had
10 * a side effect on the server.
11 */
12"TRANSIENT"
13/**
14 * This is an error where the server explicitly told the client to back off,
15 * such as a 429 or 503 Http error.
16 */
17 | "THROTTLING"
18/**
19 * This is a server error that isn't explicitly throttling but is considered
20 * by the client to be something that should be retried.
21 */
22 | "SERVER_ERROR"
23/**
24 * Doesn't count against any budgets. This could be something like a 401
25 * challenge in Http.
26 */
27 | "CLIENT_ERROR";
28/**
29 * @public
30 */
31export interface RetryErrorInfo {
32 errorType: RetryErrorType;
33 /**
34 * Protocol hint. This could come from Http's 'retry-after' header or
35 * something from MQTT or any other protocol that has the ability to convey
36 * retry info from a peer.
37 *
38 * @returns the Date after which a retry should be attempted.
39 */
40 retryAfterHint?: Date;
41}
42/**
43 * @public
44 */
45export interface RetryBackoffStrategy {
46 /**
47 * @returns the number of milliseconds to wait before retrying an action.
48 */
49 computeNextBackoffDelay(retryAttempt: number): number;
50}
51/**
52 * @public
53 */
54export interface StandardRetryBackoffStrategy extends RetryBackoffStrategy {
55 /**
56 * Sets the delayBase used to compute backoff delays.
57 * @param delayBase -
58 */
59 setDelayBase(delayBase: number): void;
60}
61/**
62 * @public
63 */
64export interface RetryStrategyOptions {
65 backoffStrategy: RetryBackoffStrategy;
66 maxRetriesBase: number;
67}
68/**
69 * @public
70 */
71export interface RetryToken {
72 /**
73 * @returns the current count of retry.
74 */
75 getRetryCount(): number;
76 /**
77 * @returns the number of milliseconds to wait before retrying an action.
78 */
79 getRetryDelay(): number;
80}
81/**
82 * @public
83 */
84export interface StandardRetryToken extends RetryToken {
85 /**
86 * @returns wheather token has remaining tokens.
87 */
88 hasRetryTokens(errorType: RetryErrorType): boolean;
89 /**
90 * @returns the number of available tokens.
91 */
92 getRetryTokenCount(errorInfo: RetryErrorInfo): number;
93 /**
94 * @returns the cost of the last retry attemp.
95 */
96 getLastRetryCost(): number | undefined;
97 /**
98 * Releases a number of tokens.
99 *
100 * @param amount - of tokens to release.
101 */
102 releaseRetryTokens(amount?: number): void;
103}
104/**
105 * @public
106 */
107export interface RetryStrategyV2 {
108 /**
109 * Called before any retries (for the first call to the operation). It either
110 * returns a retry token or an error upon the failure to acquire a token prior.
111 *
112 * tokenScope is arbitrary and out of scope for this component. However,
113 * adding it here offers us a lot of future flexibility for outage detection.
114 * For example, it could be "us-east-1" on a shared retry strategy, or
115 * "us-west-2-c:dynamodb".
116 */
117 acquireInitialRetryToken(retryTokenScope: string): Promise<RetryToken>;
118 /**
119 * After a failed operation call, this function is invoked to refresh the
120 * retryToken returned by acquireInitialRetryToken(). This function can
121 * either choose to allow another retry and send a new or updated token,
122 * or reject the retry attempt and report the error either in an exception
123 * or returning an error.
124 */
125 refreshRetryTokenForRetry(tokenToRenew: RetryToken, errorInfo: RetryErrorInfo): Promise<RetryToken>;
126 /**
127 * Upon successful completion of the operation, a user calls this function
128 * to record that the operation was successful.
129 */
130 recordSuccess(token: RetryToken): void;
131}
132/**
133 * @public
134 */
135export type ExponentialBackoffJitterType = "DEFAULT" | "NONE" | "FULL" | "DECORRELATED";
136/**
137 * @public
138 */
139export interface ExponentialBackoffStrategyOptions {
140 jitterType: ExponentialBackoffJitterType;
141 backoffScaleValue?: number;
142}