Skip to content

Commit 43e5689

Browse files
committed
Schedule a prefetch if we got a set or its signature from a backup cache
This is not pretty, and has to be rewritten for the next beta
1 parent 008d2d9 commit 43e5689

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

dnscrypt-proxy/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func main() {
9999
}
100100

101101
func (app *App) Start(service service.Service) error {
102+
dlog.Noticef("Starting dnscrypt-proxy %s", AppVersion)
102103
proxy := app.proxy
103104
if err := InitPluginsGlobals(&proxy.pluginsGlobals, &proxy); err != nil {
104105
dlog.Fatal(err)
@@ -157,7 +158,7 @@ func (proxy *Proxy) StartProxy() {
157158
}
158159
liveServers, err := proxy.serversInfo.refresh(proxy)
159160
if liveServers > 0 {
160-
dlog.Noticef("dnscrypt-proxy %s is ready - live servers: %d", AppVersion, liveServers)
161+
dlog.Noticef("dnscrypt-proxy is ready - live servers: %d", liveServers)
161162
daemon.SdNotify(false, "READY=1")
162163
PrefetchSourceURLs(proxy.urlsToPrefetch)
163164
} else if err != nil {

dnscrypt-proxy/sources.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)