Skip to content

Commit 8a6264e

Browse files
committed
move system clock offset check to AWS SDK
1 parent e5c9eeb commit 8a6264e

File tree

5 files changed

+32
-37
lines changed

5 files changed

+32
-37
lines changed

packages/middleware-retry/src/configurations.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ export interface PreviouslyResolved {
5757
* @internal
5858
*/
5959
retryMode: string | Provider<string>;
60-
61-
systemClockOffset?: number;
6260
}
6361

6462
/**

packages/middleware-retry/src/retryDecider.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import {
66
} from "@smithy/service-error-classification";
77
import { SdkError } from "@smithy/types";
88

9+
/**
10+
* @deprecated this is only used in the deprecated StandardRetryStrategy. Do not use in new code.
11+
*/
912
export const defaultRetryDecider = (error: SdkError) => {
1013
if (!error) {
1114
return false;

packages/middleware-retry/src/retryMiddleware.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@ import {
1616
RetryStrategyV2,
1717
RetryToken,
1818
SdkError,
19-
SkdErrorWithClockSkewMetadata,
2019
} from "@smithy/types";
2120
import { INVOCATION_ID_HEADER, REQUEST_HEADER } from "@smithy/util-retry";
2221
import { v4 } from "uuid";
2322

24-
import { PreviouslyResolved, RetryResolvedConfig } from "./configurations";
23+
import { RetryResolvedConfig } from "./configurations";
2524
import { isStreamingPayload } from "./isStreamingPayload/isStreamingPayload";
2625
import { asSdkError } from "./util";
2726

28-
export const retryMiddleware = (options: RetryResolvedConfig & Partial<PreviouslyResolved>) => <
29-
Output extends MetadataBearer = MetadataBearer
30-
>(
27+
export const retryMiddleware = (options: RetryResolvedConfig) => <Output extends MetadataBearer = MetadataBearer>(
3128
next: FinalizeHandler<any, Output>,
3229
context: HandlerExecutionContext
3330
): FinalizeHandler<any, Output> => async (
@@ -49,30 +46,19 @@ export const retryMiddleware = (options: RetryResolvedConfig & Partial<Previousl
4946
request.headers[INVOCATION_ID_HEADER] = v4();
5047
}
5148

52-
let initialSystemClockOffset = 0;
53-
5449
while (true) {
5550
try {
5651
if (isRequest) {
5752
request.headers[REQUEST_HEADER] = `attempt=${attempts + 1}; max=${maxAttempts}`;
5853
}
59-
initialSystemClockOffset = options.systemClockOffset ?? 0 | 0;
6054
const { response, output } = await next(args);
6155
retryStrategy.recordSuccess(retryToken);
6256
output.$metadata.attempts = attempts + 1;
6357
output.$metadata.totalRetryDelay = totalRetryDelay;
6458
return { response, output };
65-
} catch (e: unknown) {
66-
const latestSystemClockOffset = options.systemClockOffset ?? 0 | 0;
67-
const clockSkewCorrected = initialSystemClockOffset !== latestSystemClockOffset;
68-
69-
const sdkError = e as SkdErrorWithClockSkewMetadata;
70-
if (clockSkewCorrected && sdkError.$metadata && typeof sdkError.$metadata === "object") {
71-
sdkError.$metadata.clockSkewCorrected = true;
72-
}
73-
74-
const retryErrorInfo = getRetryErrorInfo(sdkError);
75-
lastError = asSdkError(sdkError);
59+
} catch (e: any) {
60+
const retryErrorInfo = getRetryErrorInfo(e);
61+
lastError = asSdkError(e);
7662

7763
if (isRequest && isStreamingPayload(request)) {
7864
(context.logger instanceof NoOpLogger ? console : context.logger)?.warn(
@@ -110,7 +96,7 @@ const isRetryStrategyV2 = (retryStrategy: RetryStrategy | RetryStrategyV2) =>
11096
typeof (retryStrategy as RetryStrategyV2).refreshRetryTokenForRetry !== "undefined" &&
11197
typeof (retryStrategy as RetryStrategyV2).recordSuccess !== "undefined";
11298

113-
const getRetryErrorInfo = (error: SkdErrorWithClockSkewMetadata): RetryErrorInfo => {
99+
const getRetryErrorInfo = (error: SdkError): RetryErrorInfo => {
114100
const errorInfo: RetryErrorInfo = {
115101
error,
116102
errorType: getRetryErrorType(error),
@@ -122,7 +108,7 @@ const getRetryErrorInfo = (error: SkdErrorWithClockSkewMetadata): RetryErrorInfo
122108
return errorInfo;
123109
};
124110

125-
const getRetryErrorType = (error: SkdErrorWithClockSkewMetadata): RetryErrorType => {
111+
const getRetryErrorType = (error: SdkError): RetryErrorType => {
126112
if (isThrottlingError(error)) return "THROTTLING";
127113
if (isTransientError(error)) return "TRANSIENT";
128114
if (isServerError(error)) return "SERVER_ERROR";

packages/service-error-classification/src/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SdkError, SkdErrorWithClockSkewMetadata } from "@smithy/types";
1+
import { SdkError } from "@smithy/types";
22

33
import {
44
CLOCK_SKEW_ERROR_CODES,
@@ -10,8 +10,16 @@ import {
1010

1111
export const isRetryableByTrait = (error: SdkError) => error.$retryable !== undefined;
1212

13+
/**
14+
* @deprecated use isClockSkewCorrectedError. This is only used in deprecated code.
15+
*/
1316
export const isClockSkewError = (error: SdkError) => CLOCK_SKEW_ERROR_CODES.includes(error.name);
1417

18+
/**
19+
* @returns whether the error resulted in a systemClockOffset aka clock skew correction.
20+
*/
21+
export const isClockSkewCorrectedError = (error: SdkError) => error.$metadata?.clockSkewCorrected;
22+
1523
export const isThrottlingError = (error: SdkError) =>
1624
error.$metadata?.httpStatusCode === 429 ||
1725
THROTTLING_ERROR_CODES.includes(error.name) ||
@@ -23,8 +31,8 @@ export const isThrottlingError = (error: SdkError) =>
2331
* cause where the NodeHttpHandler does not decorate the Error with
2432
* the name "TimeoutError" to be checked by the TRANSIENT_ERROR_CODES condition.
2533
*/
26-
export const isTransientError = (error: SdkError | SkdErrorWithClockSkewMetadata) =>
27-
(error as SkdErrorWithClockSkewMetadata).$metadata?.clockSkewCorrected ||
34+
export const isTransientError = (error: SdkError) =>
35+
isClockSkewCorrectedError(error) ||
2836
TRANSIENT_ERROR_CODES.includes(error.name) ||
2937
NODEJS_TIMEOUT_ERROR_CODES.includes((error as { code?: string })?.code || "") ||
3038
TRANSIENT_ERROR_STATUS_CODES.includes(error.$metadata?.httpStatusCode || 0);

packages/types/src/shapes.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ export interface SmithyException {
7979
* the base exception for the service should be used. Each client exports
8080
* a base ServiceException prefixed with the service name.
8181
*/
82-
export type SdkError = Error & Partial<SmithyException> & Partial<MetadataBearer>;
83-
84-
/**
85-
* @internal
86-
*
87-
* @deprecated for same reason as SdkError. Use public client modeled exceptions in application code.
88-
*/
89-
export type SkdErrorWithClockSkewMetadata = SdkError & {
90-
$metadata: SdkError["$metadata"] & {
91-
clockSkewCorrected?: boolean;
82+
export type SdkError = Error &
83+
Partial<SmithyException> &
84+
Partial<MetadataBearer> & {
85+
$metadata: MetadataBearer["$metadata"] & {
86+
/**
87+
* If present, will have value of true and indicates that the error resulted in a
88+
* correction of the clock skew, a.k.a. config.systemClockOffset.
89+
* This is specific to AWS SDK and sigv4.
90+
*/
91+
readonly clockSkewCorrected?: true;
92+
};
9293
};
93-
};

0 commit comments

Comments
 (0)