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