-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathendpointDiscoveryRetryPolicy.ts
76 lines (65 loc) · 2.26 KB
/
endpointDiscoveryRetryPolicy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { OperationType } from "../common";
import { isReadRequest } from "../common/helper";
import { GlobalEndpointManager } from "../globalEndpointManager";
import { ErrorResponse } from "../request";
import { RetryContext } from "./RetryContext";
import { RetryPolicy } from "./RetryPolicy";
/**
* This class implements the retry policy for endpoint discovery.
* @hidden
*/
export class EndpointDiscoveryRetryPolicy implements RetryPolicy {
/** Current retry attempt count. */
public currentRetryAttemptCount: number;
/** Retry interval in milliseconds. */
public retryAfterInMs: number;
/** Max number of retry attempts to perform. */
private maxTries: number;
private static readonly maxTries = 120; // TODO: Constant?
private static readonly retryAfterInMs = 1000;
/**
* @param globalEndpointManager - The GlobalEndpointManager instance.
*/
constructor(
private globalEndpointManager: GlobalEndpointManager,
private operationType: OperationType
) {
this.maxTries = EndpointDiscoveryRetryPolicy.maxTries;
this.currentRetryAttemptCount = 0;
this.retryAfterInMs = EndpointDiscoveryRetryPolicy.retryAfterInMs;
}
/**
* Determines whether the request should be retried or not.
* @param err - Error returned by the request.
*/
public async shouldRetry(
err: ErrorResponse,
retryContext?: RetryContext,
locationEndpoint?: string
): Promise<boolean | [boolean, string]> {
if (!err) {
return false;
}
if (!retryContext || !locationEndpoint) {
return false;
}
if (!this.globalEndpointManager.enableEndpointDiscovery) {
return false;
}
if (this.currentRetryAttemptCount >= this.maxTries) {
return false;
}
this.currentRetryAttemptCount++;
if (isReadRequest(this.operationType)) {
await this.globalEndpointManager.markCurrentLocationUnavailableForRead(locationEndpoint);
} else {
await this.globalEndpointManager.markCurrentLocationUnavailableForWrite(locationEndpoint);
}
retryContext.retryCount = this.currentRetryAttemptCount;
retryContext.clearSessionTokenNotAvailable = false;
retryContext.retryRequestOnPreferredLocations = false;
return true;
}
}