Skip to content

Commit 61c0f17

Browse files
committed
Merge branch 'hzhang/add-retry' of github.com:DataDog/datadog-api-client-typescript into hzhang/add-retry
2 parents 11fc11f + 03ee4c7 commit 61c0f17

File tree

4 files changed

+42
-88
lines changed

4 files changed

+42
-88
lines changed

packages/datadog-api-client-common/configuration.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ export class Configuration {
2626
readonly authMethods: AuthMethods;
2727
readonly httpConfig: HttpConfiguration;
2828
readonly debug: boolean | undefined;
29-
readonly enableRetry: boolean | undefined;
30-
readonly maxRetries: number;
31-
readonly backoffBase: number;
32-
readonly backoffMultiplier: number;
3329
unstableOperations: { [name: string]: boolean };
3430
servers: BaseServerConfiguration[];
3531
operationServers: { [endpoint: string]: BaseServerConfiguration[] };
@@ -42,10 +38,6 @@ export class Configuration {
4238
authMethods: AuthMethods,
4339
httpConfig: HttpConfiguration,
4440
debug: boolean | undefined,
45-
enableRetry: boolean | false,
46-
maxRetries: number,
47-
backoffBase: number,
48-
backoffMultiplier: number,
4941
unstableOperations: { [name: string]: boolean }
5042
) {
5143
this.baseServer = baseServer;
@@ -55,10 +47,6 @@ export class Configuration {
5547
this.authMethods = authMethods;
5648
this.httpConfig = httpConfig;
5749
this.debug = debug;
58-
this.enableRetry= enableRetry;
59-
this.maxRetries = maxRetries;
60-
this.backoffBase = backoffBase;
61-
this.backoffMultiplier = backoffMultiplier;
6250
this.unstableOperations = unstableOperations;
6351
this.servers = [];
6452
for (const server of servers) {
@@ -137,26 +125,6 @@ export interface ConfigurationParameters {
137125
* Callback method to compress string body with zstd
138126
*/
139127
zstdCompressorCallback?: ZstdCompressorCallback;
140-
141-
/**
142-
* Maximum of retry attempts allowed
143-
*/
144-
maxRetries?: number;
145-
146-
/**
147-
* Backoff base, the retry backoff time is (backoffmultiplier ** number of attempts) * backoffBase
148-
*/
149-
backoffBase?: number;
150-
151-
/**
152-
* Backoff multiplier, the retry backoff time is (backoffmultiplier ** number of attempts) * backoffBase
153-
*/
154-
backoffMultiplier?: number;
155-
156-
/**
157-
* Enable retry on status code 429 or 500 and above
158-
*/
159-
enableRetry?: boolean;
160128
}
161129

162130
/**
@@ -210,10 +178,6 @@ export function createConfiguration(
210178
configureAuthMethods(authMethods),
211179
conf.httpConfig || {},
212180
conf.debug,
213-
conf.enableRetry = false,
214-
conf.maxRetries = 3,
215-
conf.backoffBase = 2,
216-
conf.backoffMultiplier = 2,
217181
{
218182
"v2.createCIAppPipelineEvent": false,
219183
"v2.cancelDowntime": false,
@@ -261,10 +225,6 @@ export function createConfiguration(
261225
);
262226
configuration.httpApi.zstdCompressorCallback = conf.zstdCompressorCallback;
263227
configuration.httpApi.debug = configuration.debug;
264-
configuration.httpApi.enableRetry = configuration.enableRetry;
265-
configuration.httpApi.maxRetries = configuration.maxRetries;
266-
configuration.httpApi.backoffBase = configuration.backoffBase;
267-
configuration.httpApi.backoffMultiplier = configuration.backoffMultiplier;
268228
return configuration;
269229
}
270230

packages/datadog-api-client-common/http/http.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,6 @@ export class ResponseContext {
247247
export type ZstdCompressorCallback = (body: string) => Buffer;
248248

249249
export interface HttpLibrary {
250-
enableRetry?: boolean | undefined;
251-
maxRetries?: number;
252-
backoffBase?: number;
253-
backoffMultiplier?: number;
254250
debug?: boolean;
255251
zstdCompressorCallback?: ZstdCompressorCallback;
256252
send(request: RequestContext): Promise<ResponseContext>;

packages/datadog-api-client-common/http/isomorphic-fetch.ts

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ import { isBrowser, isNode } from "../util";
1212
export class IsomorphicFetchHttpLibrary implements HttpLibrary {
1313
public debug = false;
1414
public zstdCompressorCallback: ZstdCompressorCallback | undefined;
15-
public enableRetry!: boolean;
16-
public maxRetries!: number ;
17-
public backoffBase!: number ;
18-
public backoffMultiplier!: number;
1915

2016
public send(request: RequestContext): Promise<ResponseContext> {
2117
if (this.debug) {
@@ -56,67 +52,59 @@ export class IsomorphicFetchHttpLibrary implements HttpLibrary {
5652
}
5753
}
5854

59-
let currentAttempt = 0;
55+
let resultPromise: Promise<ResponseContext>;
56+
6057
// On non-node environments, use native fetch if available.
6158
// `cross-fetch` incorrectly assumes all browsers have XHR available.
6259
// See https://github.com/lquixada/cross-fetch/issues/78
6360
// TODO: Remove once once above issue is resolved.
64-
const fetchFunction = (!isNode && typeof fetch === "function") ? fetch : crossFetch;
65-
66-
const executeRequest = (): Promise<ResponseContext> => {
67-
return fetchFunction(request.getUrl(), {
61+
if (!isNode && typeof fetch === "function") {
62+
resultPromise = fetch(request.getUrl(), {
6863
method: method,
6964
body: body as any,
7065
headers: headers,
7166
signal: request.getHttpConfig().signal,
7267
}).then((resp: any) => {
73-
const responseHeaders: { [name: string]: string } = {};
68+
const headers: { [name: string]: string } = {};
7469
resp.headers.forEach((value: string, name: string) => {
75-
responseHeaders[name] = value;
70+
headers[name] = value;
7671
});
77-
78-
const responseBody = {
72+
73+
const body = {
7974
text: () => resp.text(),
8075
binary: () => resp.buffer(),
8176
};
82-
83-
const response = new ResponseContext(resp.status, responseHeaders, responseBody);
84-
77+
const response = new ResponseContext(resp.status, headers, body);
8578
if (this.debug) {
8679
this.logResponse(response);
8780
}
88-
89-
if (this.shouldRetry(this.enableRetry,currentAttempt,this.maxRetries,response.httpStatusCode)) {
90-
const delay = this.calculateRetryInterval(currentAttempt,this.backoffBase,this.backoffMultiplier,headers);
91-
currentAttempt++;
92-
return this.sleep(delay * 1000).then(() => executeRequest());
93-
}
9481
return response;
9582
});
96-
};
97-
98-
const resultPromise: Promise<ResponseContext> = executeRequest();
99-
return resultPromise;
100-
}
101-
102-
private sleep(milliseconds: number): Promise<void> {
103-
return new Promise((resolve) => {
104-
setTimeout(resolve, milliseconds);
105-
});
106-
}
107-
108-
private shouldRetry(enableRetry:boolean, currentAttempt:number, maxRetries:number, responseCode:number):boolean{
109-
return (responseCode == 429 || responseCode >=500 ) && maxRetries>currentAttempt && enableRetry
110-
}
111-
112-
private calculateRetryInterval(currentAttempt:number, backoffBase:number, backoffMultiplier:number, headers: {[name: string]: string}) : number{
113-
if ("x-ratelimit-reset" in headers) {
114-
const rateLimitHeaderString = headers["x-ratelimit-reset"]
115-
const retryIntervalFromHeader = parseInt(rateLimitHeaderString,10);
116-
return retryIntervalFromHeader
11783
} else {
118-
return (backoffMultiplier ** currentAttempt) * backoffBase
84+
resultPromise = crossFetch(request.getUrl(), {
85+
method: method,
86+
body: body as any,
87+
headers: headers,
88+
signal: request.getHttpConfig().signal,
89+
}).then((resp: any) => {
90+
const headers: { [name: string]: string } = {};
91+
resp.headers.forEach((value: string, name: string) => {
92+
headers[name] = value;
93+
});
94+
95+
const body = {
96+
text: () => resp.text(),
97+
binary: () => resp.buffer(),
98+
};
99+
const response = new ResponseContext(resp.status, headers, body);
100+
if (this.debug) {
101+
this.logResponse(response);
102+
}
103+
return response;
104+
});
119105
}
106+
107+
return resultPromise;
120108
}
121109

122110
private logRequest(request: RequestContext): void {

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3888,6 +3888,16 @@ nock@^13.1.3, nock@^13.2.1:
38883888
lodash.set "^4.3.2"
38893889
propagate "^2.0.0"
38903890

3891+
nock@^13.3.3:
3892+
version "13.3.3"
3893+
resolved "https://registry.yarnpkg.com/nock/-/nock-13.3.3.tgz#179759c07d3f88ad3e794ace885629c1adfd3fe7"
3894+
integrity sha512-z+KUlILy9SK/RjpeXDiDUEAq4T94ADPHE3qaRkf66mpEhzc/ytOMm3Bwdrbq6k1tMWkbdujiKim3G2tfQARuJw==
3895+
dependencies:
3896+
debug "^4.1.0"
3897+
json-stringify-safe "^5.0.1"
3898+
lodash "^4.17.21"
3899+
propagate "^2.0.0"
3900+
38913901
node-abort-controller@^3.0.1:
38923902
version "3.1.1"
38933903
resolved "https://registry.yarnpkg.com/node-abort-controller/-/node-abort-controller-3.1.1.tgz#a94377e964a9a37ac3976d848cb5c765833b8548"

0 commit comments

Comments
 (0)