-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { TokensApiProvider } from '@poap-xyz/providers'; | ||
|
||
const MAX_RETRIES = 20; | ||
const INITIAL_DELAY = 1000; | ||
const BACKOFF_FACTOR = 1.2; | ||
|
||
/** | ||
* Abstract class representing a task that can be retried with an increasing delay. | ||
*/ | ||
export abstract class RetryableTask { | ||
protected retries = 0; | ||
protected delay: number = INITIAL_DELAY; | ||
protected tokensApiProvider: TokensApiProvider; | ||
|
||
/** | ||
* Constructs a new RetryableTask instance. | ||
* | ||
* @param {TokensApiProvider} tokensApiProvider - The provider used to perform operations that might be retried. | ||
*/ | ||
constructor(tokensApiProvider: TokensApiProvider) { | ||
this.tokensApiProvider = tokensApiProvider; | ||
} | ||
|
||
/** | ||
* Attempts to perform a given task. If the task fails, it retries with an increasing delay until | ||
* maximum retries are reached. | ||
* | ||
* @protected | ||
* @template T - The type of value that the callback returns. | ||
* @param {() => Promise<T>} callback - The asynchronous function representing the task to be retried. | ||
* @returns {Promise<T>} A promise that resolves to the result of the task or rejects with an error. | ||
* @throws {Error} Throws an error if maximum retries are reached. | ||
*/ | ||
protected backoffAndRetry<T>(callback: () => Promise<T>): Promise<T> { | ||
if (this.retries >= MAX_RETRIES) { | ||
throw new Error('Max retries reached'); | ||
} | ||
this.retries++; | ||
this.delay *= BACKOFF_FACTOR; | ||
|
||
return new Promise<T>((resolve, reject) => { | ||
setTimeout(() => { | ||
(async (): Promise<void> => { | ||
try { | ||
const response = await callback(); | ||
resolve(response); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
})().catch(reject); // Add a catch to handle potential rejections | ||
}, this.delay); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters