Skip to content

Commit

Permalink
Add cache for rates to prevent being rate limited
Browse files Browse the repository at this point in the history
  • Loading branch information
altafan committed Mar 6, 2024
1 parent 41b125b commit 678ad90
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions pkg/rater/exchange_rates_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ type coinListInfo struct {
refreshTimestamp time.Time
}

type ratesCache struct {
rates map[string]decimal.Decimal
lastUpdate time.Time
}

type exchangeRateWrapper struct {
httpClient *http.Client

Expand Down Expand Up @@ -79,6 +84,10 @@ type exchangeRateWrapper struct {
// at startup
symbols map[string]struct{}

// cache for fiat rates
ratesCache map[string]ratesCache
ratesLock *sync.Mutex

// rateLimiter is the rate limiter(token bucket) for the coin gecko api
rateLimiter *rate.Limiter
}
Expand Down Expand Up @@ -128,6 +137,8 @@ func NewExchangeRateClient(
},
coinListMtx: sync.RWMutex{},
symbols: symbols,
ratesCache: make(map[string]ratesCache),
ratesLock: &sync.Mutex{},
rateLimiter: rate.NewLimiter(rate.Every(time.Minute), numOfCallsPerMin),
}, nil
}
Expand Down Expand Up @@ -249,15 +260,28 @@ func (e *exchangeRateWrapper) getFiatToFiatRate(
source string,
target string,
) (decimal.Decimal, error) {
e.ratesLock.Lock()
defer e.ratesLock.Unlock()

source = strings.ToUpper(source)
target = strings.ToUpper(target)

data, err := fetchRates(e.httpClient, target)
if err != nil {
return decimal.Zero, err
// Update cache once a day
if cache, ok := e.ratesCache[target]; !ok || time.Since(cache.lastUpdate).Hours() >= 24 {
data, err := fetchRates(e.httpClient, target)
if err != nil {
if !ok {
return decimal.Zero, err
}
return cache.rates[source], nil
}
e.ratesCache[target] = ratesCache{
rates: data.rates,
lastUpdate: time.Now(),
}
}

return data.rates[source], nil
return e.ratesCache[target].rates[source], nil
}

// reloadCoinList reloads the coin list from coin gecko
Expand Down

0 comments on commit 678ad90

Please sign in to comment.