Refactor cache reading to reduce number of return values

This commit is contained in:
William Elwood 2019-10-30 23:40:06 +00:00 committed by Frank Denis
parent fe34d07b68
commit 4a792026eb
2 changed files with 20 additions and 27 deletions

View File

@ -47,27 +47,23 @@ func (source *Source) checkSignature(bin, sig []byte) (err error) {
// timeNow can be replaced by tests to provide a static value // timeNow can be replaced by tests to provide a static value
var timeNow = time.Now var timeNow = time.Now
func fetchFromCache(cacheFile string, refreshDelay time.Duration) (bin []byte, expired bool, delayTillNextUpdate time.Duration, err error) { func fetchFromCache(cacheFile string, refreshDelay time.Duration) (bin []byte, delayTillNextUpdate time.Duration, err error) {
expired = false
delayTillNextUpdate = time.Duration(0) delayTillNextUpdate = time.Duration(0)
if refreshDelay < MinSourcesUpdateDelay { if refreshDelay < MinSourcesUpdateDelay {
refreshDelay = MinSourcesUpdateDelay refreshDelay = MinSourcesUpdateDelay
} }
fi, err := os.Stat(cacheFile) var fi os.FileInfo
if err != nil { if fi, err = os.Stat(cacheFile); err != nil {
dlog.Debugf("Cache file [%s] not present", cacheFile)
return return
} }
if bin, err = ioutil.ReadFile(cacheFile); err != nil { if bin, err = ioutil.ReadFile(cacheFile); err != nil {
return return
} }
elapsed := timeNow().Sub(fi.ModTime()) if elapsed := timeNow().Sub(fi.ModTime()); 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)
expired = true
} }
return return
} }
@ -82,16 +78,15 @@ func fetchFromURL(xTransport *XTransport, u *url.URL) (bin []byte, err error) {
} }
func fetchWithCache(xTransport *XTransport, urlStr string, cacheFile string, refreshDelay time.Duration) (bin []byte, delayTillNextUpdate time.Duration, err error) { func fetchWithCache(xTransport *XTransport, urlStr string, cacheFile string, refreshDelay time.Duration) (bin []byte, delayTillNextUpdate time.Duration, err error) {
expired := false if bin, delayTillNextUpdate, err = fetchFromCache(cacheFile, refreshDelay); err != nil {
bin, expired, delayTillNextUpdate, err = fetchFromCache(cacheFile, refreshDelay) if len(urlStr) == 0 {
if err == nil && !expired { dlog.Errorf("Cache file [%s] not present and no URL given to retrieve it", cacheFile)
dlog.Debugf("Delay till next update: %v", delayTillNextUpdate)
return return
} }
if len(urlStr) == 0 { dlog.Debugf("Cache file [%s] not present", cacheFile)
if !expired {
err = fmt.Errorf("Cache file [%s] not present and no URL given to retrieve it", cacheFile)
} }
if err == nil && delayTillNextUpdate > 0 {
dlog.Debugf("Delay till next update: %v", delayTillNextUpdate)
return return
} }
@ -123,14 +118,14 @@ type URLToPrefetch struct {
when time.Time when time.Time
} }
func NewSource(xTransport *XTransport, urls []string, minisignKeyStr string, cacheFile string, formatStr string, refreshDelay time.Duration) (Source, []URLToPrefetch, error) { func NewSource(xTransport *XTransport, urls []string, minisignKeyStr string, cacheFile string, formatStr string, refreshDelay time.Duration) (source Source, urlsToPrefetch []URLToPrefetch, err error) {
source := Source{urls: urls} source = Source{urls: urls}
urlsToPrefetch = []URLToPrefetch{}
if formatStr == "v2" { if formatStr == "v2" {
source.format = SourceFormatV2 source.format = SourceFormatV2
} else { } else {
return source, []URLToPrefetch{}, fmt.Errorf("Unsupported source format: [%s]", formatStr) return source, []URLToPrefetch{}, fmt.Errorf("Unsupported source format: [%s]", formatStr)
} }
urlsToPrefetch := []URLToPrefetch{}
if minisignKey, err := minisign.NewPublicKey(minisignKeyStr); err == nil { if minisignKey, err := minisign.NewPublicKey(minisignKeyStr); err == nil {
source.minisignKey = &minisignKey source.minisignKey = &minisignKey
} else { } else {
@ -141,7 +136,7 @@ func NewSource(xTransport *XTransport, urls []string, minisignKeyStr string, cac
var bin, sig []byte var bin, sig []byte
var delayTillNextUpdate, sigDelayTillNextUpdate time.Duration var delayTillNextUpdate, sigDelayTillNextUpdate time.Duration
var err, sigErr error var sigErr error
var preloadURL string var preloadURL string
if len(urls) <= 0 { if len(urls) <= 0 {
bin, delayTillNextUpdate, err = fetchWithCache(xTransport, "", cacheFile, refreshDelay) bin, delayTillNextUpdate, err = fetchWithCache(xTransport, "", cacheFile, refreshDelay)
@ -169,15 +164,15 @@ func NewSource(xTransport *XTransport, urls []string, minisignKeyStr string, cac
err = sigErr err = sigErr
} }
if err != nil { if err != nil {
return source, urlsToPrefetch, err return
} }
if err = source.checkSignature(bin, sig); err != nil { if err = source.checkSignature(bin, sig); err != nil {
return source, urlsToPrefetch, err return
} }
dlog.Noticef("Source [%s] loaded", cacheFile) dlog.Noticef("Source [%s] loaded", cacheFile)
source.in = bin source.in = bin
return source, urlsToPrefetch, nil return
} }
func (source *Source) Parse(prefix string) ([]RegisteredServer, error) { func (source *Source) Parse(prefix string) ([]RegisteredServer, error) {

View File

@ -255,7 +255,7 @@ func prepSourceTestCache(t *testing.T, d *SourceTestData, e *SourceTestExpect, s
case TestStatePartial, TestStatePartialSig: case TestStatePartial, TestStatePartialSig:
e.err = "signature" e.err = "signature"
case TestStateMissing, TestStateMissingSig: case TestStateMissing, TestStateMissingSig:
e.err = "not present" e.err = "stat"
case TestStateOpenErr, TestStateOpenSigErr: case TestStateOpenErr, TestStateOpenSigErr:
e.err = os.ErrPermission.Error() e.err = os.ErrPermission.Error()
} }
@ -302,8 +302,6 @@ func prepSourceTestDownload(t *testing.T, d *SourceTestData, e *SourceTestExpect
if e.success { if e.success {
e.err = "" e.err = ""
} }
} else if !e.success {
e.err = "no URL"
} }
} }