|
| 1 | +import { Agent } from 'node:http'; |
1 | 2 | import { HttpService } from '@nestjs/axios'; |
2 | 3 | import { CACHE_MANAGER, Inject, Injectable, Logger } from '@nestjs/common'; |
3 | 4 | import { AxiosError, AxiosRequestConfig, AxiosRequestHeaders, AxiosResponse, AxiosResponseHeaders } from 'axios'; |
| 5 | +import axiosRetry, { exponentialDelay } from 'axios-retry'; |
4 | 6 | import { Cache } from 'cache-manager'; |
5 | 7 | import { isNil } from 'lodash'; |
6 | 8 | import { config } from 'node-config-ts'; |
@@ -44,6 +46,19 @@ export class BridgeClient { |
44 | 46 | private readonly logger: Logger = new Logger(BridgeClient.name); |
45 | 47 |
|
46 | 48 | constructor(@Inject(CACHE_MANAGER) private readonly cacheManager: Cache, private readonly httpService: HttpService) { |
| 49 | + const httpAgent = new Agent({ keepAlive: true }); |
| 50 | + |
| 51 | + this.httpService.axiosRef.defaults.httpAgent = httpAgent; |
| 52 | + this.httpService.axiosRef.defaults.httpsAgent = httpAgent; |
| 53 | + |
| 54 | + axiosRetry(this.httpService.axiosRef, { |
| 55 | + retries: 3, |
| 56 | + retryDelay: exponentialDelay, |
| 57 | + retryCondition: (err: AxiosError) => |
| 58 | + axiosRetry.isNetworkOrIdempotentRequestError(err) || |
| 59 | + ['ECONNRESET', 'ETIMEDOUT', 'EAI_AGAIN'].includes(err.code as string), |
| 60 | + }); |
| 61 | + |
47 | 62 | if (config.activateBridgeRequestInterceptor) { |
48 | 63 | this.httpService.axiosRef.interceptors.request.use((_config: AxiosRequestConfig): AxiosRequestConfig => { |
49 | 64 | this.logger.log(`${_config.method} ${_config.url} - Request to Bridge`); |
@@ -82,7 +97,9 @@ export class BridgeClient { |
82 | 97 | const url: string = `${config.bridge.baseUrl}/v2/users`; |
83 | 98 |
|
84 | 99 | const resp: AxiosResponse<UserResponse> = await BridgeClient.toPromise( |
85 | | - this.httpService.post(url, userAccount, { headers: { ...BridgeClient.getHeaders(clientConfig) } }), |
| 100 | + this.httpService.post(url, userAccount, { |
| 101 | + headers: { ...BridgeClient.getHeaders(clientConfig) }, |
| 102 | + }), |
86 | 103 | ); |
87 | 104 | this.logger.debug(`User created with email ${userAccount.email}`); |
88 | 105 |
|
@@ -232,7 +249,7 @@ export class BridgeClient { |
232 | 249 | }), |
233 | 250 | ); |
234 | 251 | const { name } = resp.data; |
235 | | - await this.cacheManager.set(url, name, { ttl: 86400 }); |
| 252 | + await this.cacheManager.set(url, name, { ttl: config.cacheTtlOfCategory }); |
236 | 253 |
|
237 | 254 | return name; |
238 | 255 | } catch (err) { |
@@ -274,7 +291,7 @@ export class BridgeClient { |
274 | 291 | name: resp.data.name, |
275 | 292 | logoUrl: resp.data.logo_url, |
276 | 293 | }; |
277 | | - await this.cacheManager.set(url, bankInfo, { ttl: 86400 }); |
| 294 | + await this.cacheManager.set(url, bankInfo, { ttl: config.cacheTtlOfBankInfo }); |
278 | 295 |
|
279 | 296 | return bankInfo; |
280 | 297 | } catch (err) { |
|
0 commit comments