From 4c156784c891b228896d8629c4a8704e3b554162 Mon Sep 17 00:00:00 2001 From: William Elwood Date: Wed, 30 Oct 2019 04:24:59 +0000 Subject: [PATCH] Refactor calculation of update delay when reading cache Set the default delay once at the top instead of before every early return. --- dnscrypt-proxy/sources.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/dnscrypt-proxy/sources.go b/dnscrypt-proxy/sources.go index 7101fe40..88cee814 100644 --- a/dnscrypt-proxy/sources.go +++ b/dnscrypt-proxy/sources.go @@ -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