Skip to content

Commit

Permalink
Merge pull request #1134 from JupiterOne/client-add-retryCalculateDelay
Browse files Browse the repository at this point in the history
http-client: add retryCalculateDelay method
  • Loading branch information
RonaldEAM authored Nov 21, 2024
2 parents 4adf91d + 90b5736 commit 8d4a2de
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/integration-sdk-http-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import {
IntegrationLogger,
IntegrationProviderAPIError,
} from '@jupiterone/integration-sdk-core';
import { AttemptContext, retry, sleep } from '@lifeomic/attempt';
import {
AttemptContext,
AttemptOptions,
defaultCalculateDelay,
retry,
sleep,
} from '@lifeomic/attempt';
import {
fatalRequestError,
isRetryableRequest,
Expand Down Expand Up @@ -338,6 +344,9 @@ export abstract class BaseAPIClient {
delay: this.retryOptions.delay,
timeout: this.retryOptions.timeout,
factor: this.retryOptions.factor,
calculateDelay: (context, options) => {
return this.retryCalculateDelay(context, options);
},
handleError: async (err, context) => {
if (this.retryOptions.handleError) {
await this.retryOptions.handleError(err, context, this.logger);
Expand Down Expand Up @@ -418,6 +427,16 @@ export abstract class BaseAPIClient {
}
}

protected retryCalculateDelay<T>(
context: AttemptContext,
options: AttemptOptions<T>,
) {
if (context.attemptNum === 0) {
return 0; // don't wait before the first attempt
}
return defaultCalculateDelay(context, options);
}

/**
* Iteratively performs API requests based on the initial request and subsequent requests defined by a callback function.
* This method is designed to facilitate paginated API requests where each request's response determines the parameters of the next request.
Expand Down
2 changes: 2 additions & 0 deletions packages/integration-sdk-http-client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
import { AttemptContext } from '@lifeomic/attempt';
import { Agent } from 'node:https';

export { AttemptContext, AttemptOptions } from '@lifeomic/attempt';

export type OptionalPromise<T> = T | Promise<T>;

export interface RequestOptions {
Expand Down

0 comments on commit 8d4a2de

Please sign in to comment.