From f3e70c73ca73b2d25278af79abd6de2cfff8d864 Mon Sep 17 00:00:00 2001 From: Conicaw Date: Sun, 21 Jan 2024 20:19:12 -0600 Subject: [PATCH] feat!: configure default retryCondition to retry more Configure the default retryCondition to retry by default on gateway errors or rate limit errors commit-id:a7faaa02 --- lib/http.module.ts | 2 ++ lib/http.util.ts | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/http.module.ts b/lib/http.module.ts index 7fbb8e6..885df39 100644 --- a/lib/http.module.ts +++ b/lib/http.module.ts @@ -6,6 +6,7 @@ import axiosRetry, { exponentialDelay } from 'axios-retry'; import { AXIOS_INSTANCE_TOKEN, HTTP_MODULE_ID, HTTP_MODULE_OPTIONS } from './http.constants'; import { HttpService } from './http.service'; +import { isNetworkOrIdempotentRequestOrGatewayOrRateLimitError } from './http.util'; import type { HttpModuleAsyncOptions, HttpModuleOptions, @@ -18,6 +19,7 @@ const createAxiosInstance = (config?: HttpModuleOptions) => { axiosRetry(axiosInstance, { // Default exponential backoff retryDelay: exponentialDelay, + retryCondition: isNetworkOrIdempotentRequestOrGatewayOrRateLimitError, onRetry(retryCount, error, requestConfig) { logger.warn( { diff --git a/lib/http.util.ts b/lib/http.util.ts index 77b0cf8..cd0451c 100644 --- a/lib/http.util.ts +++ b/lib/http.util.ts @@ -5,6 +5,12 @@ export function isGatewayError(error: AxiosError): boolean { return !!error.response && error.response.status >= 502 && error.response.status <= 504; } -export function isNetworkOrIdempotentRequestOrGatewayError(error: AxiosError): boolean { - return isNetworkOrIdempotentRequestError(error) || isGatewayError(error); +export function isRateLimitError(error: AxiosError): boolean { + return !!error.response && error.response.status === 429; +} + +export function isNetworkOrIdempotentRequestOrGatewayOrRateLimitError(error: AxiosError): boolean { + return ( + isNetworkOrIdempotentRequestError(error) || isGatewayError(error) || isRateLimitError(error) + ); }