Refactor calculation of update delay when reading cache

Set the default delay once at the top instead of before every early return.
This commit is contained in:
William Elwood 2019-10-30 04:24:59 +00:00 committed by Frank Denis
parent e818eeb800
commit 4c156784c8
1 changed files with 7 additions and 11 deletions

View File

@ -49,31 +49,27 @@ var timeNow = time.Now
func fetchFromCache(cacheFile string, refreshDelay time.Duration) (in string, expired bool, delayTillNextUpdate time.Duration, err error) {
expired = false
delayTillNextUpdate = time.Duration(0)
if refreshDelay < MinSourcesUpdateDelay {
refreshDelay = MinSourcesUpdateDelay
}
fi, err := os.Stat(cacheFile)
if err != nil {
dlog.Debugf("Cache file [%s] not present", cacheFile)
delayTillNextUpdate = time.Duration(0)
return
}
var bin []byte
bin, err = ioutil.ReadFile(cacheFile)
if err != nil {
return
}
in = string(bin)
elapsed := timeNow().Sub(fi.ModTime())
if elapsed < refreshDelay {
dlog.Debugf("Cache file [%s] is still fresh", cacheFile)
delayTillNextUpdate = MinSourcesUpdateDelay - elapsed
} else {
dlog.Debugf("Cache file [%s] needs to be refreshed", cacheFile)
delayTillNextUpdate = time.Duration(0)
}
var bin []byte
bin, err = ioutil.ReadFile(cacheFile)
if err != nil {
delayTillNextUpdate = time.Duration(0)
return
}
in = string(bin)
if delayTillNextUpdate <= time.Duration(0) {
expired = true
}
return