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 format SourceFormat
in []byte in []byte
minisignKey *minisign.PublicKey minisignKey *minisign.PublicKey
cacheFile string
} }
func (source *Source) checkSignature(bin, sig []byte) (err error) { func (source *Source) checkSignature(bin, sig []byte) (err error) {
@ -130,12 +131,11 @@ func AtomicFileWrite(file string, data []byte) error {
type URLToPrefetch struct { type URLToPrefetch struct {
url string url string
cacheFile string
when time.Time when time.Time
} }
func NewSource(xTransport *XTransport, urls []string, minisignKeyStr string, cacheFile string, formatStr string, refreshDelay time.Duration) (source *Source, err error) { 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" { if formatStr == "v2" {
source.format = SourceFormatV2 source.format = SourceFormatV2
} else { } else {
@ -167,7 +167,7 @@ func NewSource(xTransport *XTransport, urls []string, minisignKeyStr string, cac
} }
if len(preloadURL) > 0 { if len(preloadURL) > 0 {
url := preloadURL 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 { if err != nil {
return return
@ -188,18 +188,19 @@ func PrefetchSources(xTransport *XTransport, sources []*Source) time.Duration {
for _, urlToPrefetch := range source.prefetch { for _, urlToPrefetch := range source.prefetch {
if now.After(urlToPrefetch.when) { if now.After(urlToPrefetch.when) {
dlog.Debugf("Prefetching [%s]", urlToPrefetch.url) dlog.Debugf("Prefetching [%s]", urlToPrefetch.url)
if err := PrefetchSourceURL(xTransport, urlToPrefetch); err != nil { _, _, delay, err := fetchWithCache(xTransport, urlToPrefetch.url, source.cacheFile, DefaultPrefetchDelay)
dlog.Debugf("Prefetching [%s] failed: %s", urlToPrefetch.url, err) if err != nil {
} else { dlog.Debugf("Prefetching [%s] failed: %v", urlToPrefetch.url, err)
continue
}
dlog.Debugf("Prefetching [%s] succeeded. Next refresh scheduled for %v", urlToPrefetch.url, urlToPrefetch.when) 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) { if delay >= MinimumPrefetchInterval && (interval == MinimumPrefetchInterval || interval > delay) {
interval = delay interval = delay
} }
} }
} }
} }
}
return interval return interval
} }
@ -276,9 +277,3 @@ PartsLoop:
} }
return registeredServers, nil 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) e.Source.urls = append(e.Source.urls, d.server.URL+path)
if state != TestStatePathErr { 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 { if e.success {
@ -307,8 +307,8 @@ func setupSourceTestCase(t *testing.T, d *SourceTestData, i int,
e = &SourceTestExpect{ e = &SourceTestExpect{
cachePath: filepath.Join(d.tempDir, id), cachePath: filepath.Join(d.tempDir, id),
refresh: d.timeNow, 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 { if cacheTest != nil {
prepSourceTestCache(t, d, e, d.sources[i], *cacheTest) prepSourceTestCache(t, d, e, d.sources[i], *cacheTest)
i = (i + 1) % len(d.sources) // make the cached and downloaded fixtures different i = (i + 1) % len(d.sources) // make the cached and downloaded fixtures different