Move cacheFile from URLToPrefetch to Source struct

This commit is contained in:
William Elwood 2019-10-31 07:01:00 +00:00 committed by Frank Denis
parent 0991749b19
commit 190700e5ba
2 changed files with 16 additions and 21 deletions

View File

@ -36,6 +36,7 @@ type Source struct {
format SourceFormat
in []byte
minisignKey *minisign.PublicKey
cacheFile string
}
func (source *Source) checkSignature(bin, sig []byte) (err error) {
@ -130,12 +131,11 @@ func AtomicFileWrite(file string, data []byte) error {
type URLToPrefetch struct {
url string
cacheFile string
when time.Time
}
func NewSource(xTransport *XTransport, urls []string, minisignKeyStr string, cacheFile string, formatStr string, refreshDelay time.Duration) (source *Source, err error) {
source = &Source{urls: urls}
source = &Source{urls: urls, cacheFile: cacheFile}
if formatStr == "v2" {
source.format = SourceFormatV2
} else {
@ -167,7 +167,7 @@ func NewSource(xTransport *XTransport, urls []string, minisignKeyStr string, cac
}
if len(preloadURL) > 0 {
url := preloadURL
source.prefetch = append(source.prefetch, &URLToPrefetch{url: url, cacheFile: cacheFile, when: now.Add(delayTillNextUpdate)})
source.prefetch = append(source.prefetch, &URLToPrefetch{url: url, when: now.Add(delayTillNextUpdate)})
}
if err != nil {
return
@ -188,18 +188,19 @@ func PrefetchSources(xTransport *XTransport, sources []*Source) time.Duration {
for _, urlToPrefetch := range source.prefetch {
if now.After(urlToPrefetch.when) {
dlog.Debugf("Prefetching [%s]", urlToPrefetch.url)
if err := PrefetchSourceURL(xTransport, urlToPrefetch); err != nil {
dlog.Debugf("Prefetching [%s] failed: %s", urlToPrefetch.url, err)
} else {
_, _, delay, err := fetchWithCache(xTransport, urlToPrefetch.url, source.cacheFile, DefaultPrefetchDelay)
if err != nil {
dlog.Debugf("Prefetching [%s] failed: %v", urlToPrefetch.url, err)
continue
}
dlog.Debugf("Prefetching [%s] succeeded. Next refresh scheduled for %v", urlToPrefetch.url, urlToPrefetch.when)
delay := urlToPrefetch.when.Sub(now)
urlToPrefetch.when = now.Add(delay)
if delay >= MinimumPrefetchInterval && (interval == MinimumPrefetchInterval || interval > delay) {
interval = delay
}
}
}
}
}
return interval
}
@ -276,9 +277,3 @@ PartsLoop:
}
return registeredServers, nil
}
func PrefetchSourceURL(xTransport *XTransport, urlToPrefetch *URLToPrefetch) error {
_, _, delayTillNextUpdate, err := fetchWithCache(xTransport, urlToPrefetch.url, urlToPrefetch.cacheFile, DefaultPrefetchDelay)
urlToPrefetch.when = timeNow().Add(delayTillNextUpdate)
return err
}

View File

@ -292,7 +292,7 @@ func prepSourceTestDownload(t *testing.T, d *SourceTestData, e *SourceTestExpect
}
e.Source.urls = append(e.Source.urls, d.server.URL+path)
if state != TestStatePathErr {
e.Source.prefetch = append(e.Source.prefetch, &URLToPrefetch{d.server.URL + path, e.cachePath, e.refresh})
e.Source.prefetch = append(e.Source.prefetch, &URLToPrefetch{d.server.URL + path, e.refresh})
}
}
if e.success {
@ -307,8 +307,8 @@ func setupSourceTestCase(t *testing.T, d *SourceTestData, i int,
e = &SourceTestExpect{
cachePath: filepath.Join(d.tempDir, id),
refresh: d.timeNow,
Source: &Source{urls: []string{}, prefetch: []*URLToPrefetch{}, format: SourceFormatV2, minisignKey: d.key},
}
e.Source = &Source{urls: []string{}, prefetch: []*URLToPrefetch{}, format: SourceFormatV2, minisignKey: d.key, cacheFile: e.cachePath}
if cacheTest != nil {
prepSourceTestCache(t, d, e, d.sources[i], *cacheTest)
i = (i + 1) % len(d.sources) // make the cached and downloaded fixtures different