@@ -36,9 +36,9 @@ func fetchFromCache(cacheFile string) ([]byte, error) {
3636 return ioutil .ReadFile (cacheFile )
3737}
3838
39- func fetchWithCache (url string , cacheFile string , refreshDelay time.Duration ) (in string , cached bool , delayTillNextUpdate time.Duration , err error ) {
39+ func fetchWithCache (url string , cacheFile string , refreshDelay time.Duration ) (in string , cached bool , fromBackup bool , delayTillNextUpdate time.Duration , err error ) {
4040 var bin []byte
41- cached , usableCache , hotCache := false , false , false
41+ cached , fromBackup , usableCache , hotCache := false , false , false , false
4242 delayTillNextUpdate = refreshDelay
4343 fi , err := os .Stat (cacheFile )
4444 var elapsed time.Duration
@@ -62,7 +62,7 @@ func fetchWithCache(url string, cacheFile string, refreshDelay time.Duration) (i
6262 var resp * http.Response
6363 dlog .Infof ("Loading source information from URL [%s]" , url )
6464 resp , err = http .Get (url )
65- if resp .StatusCode < 200 || resp .StatusCode > 299 {
65+ if err == nil && resp != nil && ( resp .StatusCode < 200 || resp .StatusCode > 299 ) {
6666 err = fmt .Errorf ("Webserver returned code %d" , resp .StatusCode )
6767 }
6868 if err != nil {
@@ -74,6 +74,7 @@ func fetchWithCache(url string, cacheFile string, refreshDelay time.Duration) (i
7474 if err != nil {
7575 return
7676 }
77+ fromBackup = true
7778 } else {
7879 bin , err = ioutil .ReadAll (resp .Body )
7980 resp .Body .Close ()
@@ -85,6 +86,7 @@ func fetchWithCache(url string, cacheFile string, refreshDelay time.Duration) (i
8586 if err != nil {
8687 return
8788 }
89+ fromBackup = true
8890 }
8991 }
9092 }
@@ -119,12 +121,12 @@ func NewSource(url string, minisignKeyStr string, cacheFile string, formatStr st
119121 URLToPrefetch {url : url , cacheFile : cacheFile , when : when },
120122 URLToPrefetch {url : sigURL , cacheFile : cacheFile , when : when },
121123 }
122- in , cached , delayTillNextUpdate , err := fetchWithCache (url , cacheFile , refreshDelay )
124+ in , cached , fromBackup , delayTillNextUpdate , err := fetchWithCache (url , cacheFile , refreshDelay )
123125 if err != nil {
124126 return source , urlsToPrefetch , err
125127 }
126128 sigCacheFile := cacheFile + ".minisig"
127- sigStr , sigCached , sigDelayTillNextUpdate , err := fetchWithCache (sigURL , sigCacheFile , refreshDelay )
129+ sigStr , sigCached , sigFromBackup , sigDelayTillNextUpdate , err := fetchWithCache (sigURL , sigCacheFile , refreshDelay )
128130 if err != nil {
129131 return source , urlsToPrefetch , err
130132 }
@@ -159,7 +161,9 @@ func NewSource(url string, minisignKeyStr string, cacheFile string, formatStr st
159161 delayTillNextUpdate = SourcesUpdateDelayAfterFailure
160162 }
161163 when = time .Now ().Add (delayTillNextUpdate )
162- urlsToPrefetch = []URLToPrefetch {}
164+ if ! fromBackup && ! sigFromBackup {
165+ urlsToPrefetch = []URLToPrefetch {}
166+ }
163167 return source , urlsToPrefetch , nil
164168}
165169
@@ -210,7 +214,7 @@ func PrefetchSourceURLs(urlsToPrefetch []URLToPrefetch) {
210214 }
211215 dlog .Infof ("Prefetching %d source URLs" , len (urlsToPrefetch ))
212216 for _ , urlToPrefetch := range urlsToPrefetch {
213- if _ , _ , _ , err := fetchWithCache (urlToPrefetch .url , urlToPrefetch .cacheFile , time .Duration (0 )); err != nil {
217+ if _ , _ , _ , _ , err := fetchWithCache (urlToPrefetch .url , urlToPrefetch .cacheFile , time .Duration (0 )); err != nil {
214218 dlog .Debugf ("[%s]: %s" , urlToPrefetch .url , err )
215219 }
216220 }
0 commit comments