implement rudimentary caching for yahoo finance to avoid http 429 rate limiting
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -3,10 +3,11 @@ package staticjsonYahooFinance
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A chart response is what we get back from Yahoo Finance
|
||||
type chart struct {
|
||||
// A chartResponse response is what we get back from Yahoo Finance
|
||||
type chartResponse struct {
|
||||
Chart struct {
|
||||
Result []struct {
|
||||
Meta struct {
|
||||
@@ -69,8 +70,16 @@ type chart struct {
|
||||
} `json:"chart"`
|
||||
}
|
||||
|
||||
// getChart first checks if the symbol chartCacheEntry is valid,
|
||||
// and if not then it downloads fresh chart data
|
||||
func (c client) getChart(symbol string) (int, error) {
|
||||
chartResponse := &chart{}
|
||||
if cacheItem, ok := chartCache[symbol]; ok {
|
||||
// if the cacheEntry is still valid, use it
|
||||
if time.Now().Before(cacheItem.LastUpdated.Add(cacheAgeSeconds)) {
|
||||
return int(cacheItem.Chart.Chart.Result[0].Meta.RegularMarketPrice * 1000), nil
|
||||
}
|
||||
}
|
||||
chartResponse := &chartResponse{}
|
||||
err := c.get(fmt.Sprintf("/chart/%s", symbol), chartResponse, url.Values{})
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("http get request error: %v", err)
|
||||
@@ -80,5 +89,7 @@ func (c client) getChart(symbol string) (int, error) {
|
||||
return 0, fmt.Errorf("unexpected length of results - expected 1 but got %d", len(chartResponse.Chart.Result))
|
||||
}
|
||||
|
||||
chartCache[symbol] = chartCacheEntry{Chart: *chartResponse, LastUpdated: time.Now()}
|
||||
|
||||
return int(chartResponse.Chart.Result[0].Meta.RegularMarketPrice * 1000), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user