Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
fix(stats): add timeout for exchange rate api request
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Mar 27, 2019
1 parent 7bc4446 commit 947ef64
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions back/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"nanoid": "^2.0.1",
"nest-telegram": "^0.2.3",
"pg": "^7.8.0",
"promise-timeout": "^1.3.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^6.3.3",
"typeorm": "^0.2.12"
Expand Down
13 changes: 11 additions & 2 deletions back/src/money/insfrastructure/ExchangeRateApi/ApiClientUnity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common'
import { Option } from 'tsoption'
import { timeout } from 'promise-timeout'

import { Currency } from '@shared/enum/Currency'
import { Configuration } from '@back/config/Configuration'
Expand All @@ -22,7 +23,7 @@ export class ApiClientUnity implements ExchangeRateApi {
for (const client of this.clients) {
// await in loop because we want try sequentially get rate
// eslint-disable-next-line no-await-in-loop
const rate = await client.getExchangeRate(from, to)
const rate = await this.fetchWithTimeout(client.getExchangeRate(from, to))

if (rate.nonEmpty()) {
return rate
Expand All @@ -40,7 +41,9 @@ export class ApiClientUnity implements ExchangeRateApi {
for (const client of this.clients) {
// await in loop because we want try sequentially get rate
// eslint-disable-next-line no-await-in-loop
const rate = await client.getHistoryExchangeRate(from, to, when)
const rate = await this.fetchWithTimeout(
client.getHistoryExchangeRate(from, to, when),
)

if (rate.nonEmpty()) {
return rate
Expand All @@ -49,4 +52,10 @@ export class ApiClientUnity implements ExchangeRateApi {

return Option.of(null)
}

private fetchWithTimeout(
promise: Promise<Option<number>>,
): Promise<Option<number>> {
return timeout(promise, 1000).catch(() => Option.of(null))
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9658,6 +9658,11 @@ promise-inflight@^1.0.1:
resolved "https://registry.yarnpkg.com/promise-inflight/-/promise-inflight-1.0.1.tgz#98472870bf228132fcbdd868129bad12c3c029e3"
integrity sha1-mEcocL8igTL8vdhoEputEsPAKeM=

promise-timeout@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/promise-timeout/-/promise-timeout-1.3.0.tgz#d1c78dd50a607d5f0a5207410252a3a0914e1014"
integrity sha512-5yANTE0tmi5++POym6OgtFmwfDvOXABD9oj/jLQr5GPEyuNEb7jH4wbbANJceJid49jwhi1RddxnhnEAb/doqg==

promise@^7.0.1, promise@^7.1.1:
version "7.3.1"
resolved "https://registry.yarnpkg.com/promise/-/promise-7.3.1.tgz#064b72602b18f90f29192b8b1bc418ffd1ebd3bf"
Expand Down

0 comments on commit 947ef64

Please sign in to comment.