-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement RateLimiter & Retry axios middlewares (#113)
* adding implementations for delay & retry functionality * working on retry() * implement axios interceptor for rate limiter functionality * work on implementing axios interceptors for rate limit & retry * implement retryDecider concept, i.e. expect a function from caller that will be called on error and can decide whether to retry the request or not * adding default axios, combining rate-limiting & retrying middlewares * reorder types * remove retry fn, as it's not being used * improve logging for axios middlewares, - log via user key if given - don't log error messages while retries are still going on, to not trigger any alarms prematurely
- Loading branch information
Showing
6 changed files
with
187 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { AxiosInstance } from 'axios'; | ||
import { RateLimitConfig, useRateLimitInterceptor } from './rate-limited-axios'; | ||
import { RetryConfig, useRetryOnErrorInterceptor } from './retrying-axios'; | ||
import { randomUUID } from 'crypto'; | ||
|
||
export function useDefaultInterceptors( | ||
axiosInstance: AxiosInstance, | ||
rateLimitConfig: RateLimitConfig, | ||
retryConfig: RetryConfig, | ||
key?: string, | ||
): AxiosInstance { | ||
const effectiveKey = key || randomUUID(); | ||
|
||
return useRateLimitInterceptor( | ||
useRetryOnErrorInterceptor(axiosInstance, retryConfig, effectiveKey), | ||
rateLimitConfig, | ||
effectiveKey, | ||
); | ||
} |
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,104 @@ | ||
import { AxiosError, AxiosInstance, InternalAxiosRequestConfig } from 'axios'; | ||
import { infoLogger, warnLogger } from '../logger.util'; | ||
import { delay } from '../lang/delay'; | ||
import { randomUUID } from 'crypto'; | ||
|
||
export type RetryDecision = { | ||
retryDesired: boolean; | ||
delayMs?: number; | ||
}; | ||
|
||
export type RetryDecider = ( | ||
error: AxiosError, | ||
retryCount: number, | ||
) => RetryDecision; | ||
|
||
export type RetryConfig = { | ||
retryDecider: RetryDecider; | ||
retryCountHeader?: string; | ||
}; | ||
|
||
function formatAxiosErrorForLogging(error: AxiosError) { | ||
return { | ||
code: error.code, | ||
message: error.message, | ||
stack: error.stack, | ||
method: error.config?.method, | ||
url: error.config?.url, | ||
responseStatus: error.response?.status, | ||
responseStatusText: error.response?.statusText, | ||
responseHeaders: { ...error.response?.headers }, | ||
responseData: error.response?.data, | ||
}; | ||
} | ||
|
||
export function useRetryOnErrorInterceptor( | ||
axiosInstance: AxiosInstance, | ||
config: RetryConfig, | ||
key?: string, | ||
): AxiosInstance { | ||
const effectiveKey = key || randomUUID(); | ||
const retryCountHeader = | ||
config.retryCountHeader || 'X-SipgateIntegration-RetryCount'; | ||
|
||
axiosInstance.interceptors.request.use( | ||
(config: InternalAxiosRequestConfig) => { | ||
if (config.headers[retryCountHeader] !== undefined) { | ||
config.headers[retryCountHeader] = | ||
parseInt(config.headers[retryCountHeader]) + 1; | ||
} else { | ||
config.headers[retryCountHeader] = 0; | ||
} | ||
|
||
return config; | ||
}, | ||
); | ||
|
||
axiosInstance.interceptors.response.use( | ||
(response) => response, | ||
async (error: AxiosError) => { | ||
const retryCount: number | undefined = | ||
error.config && error.config.headers[retryCountHeader] !== undefined | ||
? parseInt(error.config.headers[retryCountHeader]) | ||
: undefined; | ||
|
||
if (retryCount !== undefined) { | ||
const { retryDesired, delayMs } = config.retryDecider( | ||
error, | ||
retryCount, | ||
); | ||
|
||
if (retryDesired && error.config) { | ||
infoLogger( | ||
'axiosRetryInterceptor', | ||
'request was not successful - will retry', | ||
effectiveKey, | ||
{ | ||
status: error.response?.status, | ||
retryCount, | ||
delayMs, | ||
}, | ||
); | ||
|
||
delayMs && (await delay(delayMs)); | ||
|
||
return axiosInstance.request(error.config); | ||
} | ||
} | ||
|
||
warnLogger( | ||
'axiosRetryInterceptor', | ||
'request finally failed with error', | ||
effectiveKey, | ||
{ | ||
error: formatAxiosErrorForLogging(error), | ||
retryCount, | ||
}, | ||
); | ||
|
||
return Promise.reject(error); | ||
}, | ||
); | ||
|
||
return axiosInstance; | ||
} |
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,5 @@ | ||
export function delay(ms: number = 100) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { diffArrays } from './diff'; | ||
|
||
export { diffArrays }; | ||
|
||
export * from './delay'; |