Prefetch previously unreachable sources URLs after a server is reachable
Partial fix for #4 Pave the way for regular, background updates as well
This commit is contained in:
parent
c4bd6eb9f0
commit
a85d012a2b
|
@ -159,11 +159,12 @@ func ConfigLoad(proxy *Proxy, svcFlag *string, config_file string) error {
|
||||||
if source.RefreshDelay <= 0 {
|
if source.RefreshDelay <= 0 {
|
||||||
source.RefreshDelay = 24
|
source.RefreshDelay = 24
|
||||||
}
|
}
|
||||||
source, err := NewSource(source.URL, source.MinisignKeyStr, source.CacheFile, source.FormatStr, time.Duration(source.RefreshDelay)*time.Hour)
|
source, sourceUrlsToPrefetch, err := NewSource(source.URL, source.MinisignKeyStr, source.CacheFile, source.FormatStr, time.Duration(source.RefreshDelay)*time.Hour)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dlog.Criticalf("Unable use source [%s]: [%s]", sourceName, err)
|
dlog.Criticalf("Unable use source [%s]: [%s]", sourceName, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
proxy.urlsToPrefetch = append(proxy.urlsToPrefetch, sourceUrlsToPrefetch...)
|
||||||
registeredServers, err := source.Parse()
|
registeredServers, err := source.Parse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dlog.Criticalf("Unable use source [%s]: [%s]", sourceName, err)
|
dlog.Criticalf("Unable use source [%s]: [%s]", sourceName, err)
|
||||||
|
|
|
@ -43,6 +43,7 @@ type Proxy struct {
|
||||||
blockNameFormat string
|
blockNameFormat string
|
||||||
forwardFile string
|
forwardFile string
|
||||||
pluginsGlobals PluginsGlobals
|
pluginsGlobals PluginsGlobals
|
||||||
|
urlsToPrefetch []URLToPrefetch
|
||||||
}
|
}
|
||||||
|
|
||||||
type App struct {
|
type App struct {
|
||||||
|
@ -158,6 +159,7 @@ func (proxy *Proxy) StartProxy() {
|
||||||
if liveServers > 0 {
|
if liveServers > 0 {
|
||||||
dlog.Noticef("dnscrypt-proxy %s is ready - live servers: %d", AppVersion, liveServers)
|
dlog.Noticef("dnscrypt-proxy %s is ready - live servers: %d", AppVersion, liveServers)
|
||||||
daemon.SdNotify(false, "READY=1")
|
daemon.SdNotify(false, "READY=1")
|
||||||
|
PrefetchSourceURLs(proxy.urlsToPrefetch)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
dlog.Error(err)
|
dlog.Error(err)
|
||||||
dlog.Notice("dnscrypt-proxy is waiting for at least one server to be reachable")
|
dlog.Notice("dnscrypt-proxy is waiting for at least one server to be reachable")
|
||||||
|
|
|
@ -21,6 +21,10 @@ const (
|
||||||
SourceFormatV1 = iota
|
SourceFormatV1 = iota
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
SourcesUpdateDelayAfterFailure = time.Duration(1) * time.Minute
|
||||||
|
)
|
||||||
|
|
||||||
type Source struct {
|
type Source struct {
|
||||||
url string
|
url string
|
||||||
format SourceFormat
|
format SourceFormat
|
||||||
|
@ -32,13 +36,16 @@ func fetchFromCache(cacheFile string) ([]byte, error) {
|
||||||
return ioutil.ReadFile(cacheFile)
|
return ioutil.ReadFile(cacheFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchWithCache(url string, cacheFile string, refreshDelay time.Duration) (in string, cached bool, err error) {
|
func fetchWithCache(url string, cacheFile string, refreshDelay time.Duration) (in string, cached bool, delayTillNextUpdate time.Duration, err error) {
|
||||||
var bin []byte
|
var bin []byte
|
||||||
cached, usableCache, hotCache := false, false, false
|
cached, usableCache, hotCache := false, false, false
|
||||||
|
delayTillNextUpdate = refreshDelay
|
||||||
fi, err := os.Stat(cacheFile)
|
fi, err := os.Stat(cacheFile)
|
||||||
|
var elapsed time.Duration
|
||||||
if err == nil {
|
if err == nil {
|
||||||
usableCache = true
|
usableCache = true
|
||||||
elapsed := time.Since(fi.ModTime())
|
dlog.Debugf("Cache file present for [%s]", url)
|
||||||
|
elapsed = time.Since(fi.ModTime())
|
||||||
if elapsed < refreshDelay && elapsed >= 0 {
|
if elapsed < refreshDelay && elapsed >= 0 {
|
||||||
hotCache = true
|
hotCache = true
|
||||||
}
|
}
|
||||||
|
@ -46,7 +53,9 @@ func fetchWithCache(url string, cacheFile string, refreshDelay time.Duration) (i
|
||||||
if hotCache {
|
if hotCache {
|
||||||
bin, err = fetchFromCache(cacheFile)
|
bin, err = fetchFromCache(cacheFile)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
dlog.Debugf("Cache is still fresh for [%s]", url)
|
||||||
cached = true
|
cached = true
|
||||||
|
delayTillNextUpdate = refreshDelay - elapsed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !cached {
|
if !cached {
|
||||||
|
@ -54,7 +63,9 @@ func fetchWithCache(url string, cacheFile string, refreshDelay time.Duration) (i
|
||||||
dlog.Infof("Loading source information from URL [%s]", url)
|
dlog.Infof("Loading source information from URL [%s]", url)
|
||||||
resp, err = http.Get(url)
|
resp, err = http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
delayTillNextUpdate = SourcesUpdateDelayAfterFailure
|
||||||
if usableCache {
|
if usableCache {
|
||||||
|
dlog.Debugf("Falling back to cached version of [%s]", url)
|
||||||
bin, err = fetchFromCache(cacheFile)
|
bin, err = fetchFromCache(cacheFile)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -64,6 +75,7 @@ func fetchWithCache(url string, cacheFile string, refreshDelay time.Duration) (i
|
||||||
bin, err = ioutil.ReadAll(resp.Body)
|
bin, err = ioutil.ReadAll(resp.Body)
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
delayTillNextUpdate = SourcesUpdateDelayAfterFailure
|
||||||
if usableCache {
|
if usableCache {
|
||||||
bin, err = fetchFromCache(cacheFile)
|
bin, err = fetchFromCache(cacheFile)
|
||||||
}
|
}
|
||||||
|
@ -81,47 +93,66 @@ func AtomicFileWrite(file string, data []byte) error {
|
||||||
return safefile.WriteFile(file, data, 0644)
|
return safefile.WriteFile(file, data, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSource(url string, minisignKeyStr string, cacheFile string, formatStr string, refreshDelay time.Duration) (Source, error) {
|
type URLToPrefetch struct {
|
||||||
|
url string
|
||||||
|
cacheFile string
|
||||||
|
when time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSource(url string, minisignKeyStr string, cacheFile string, formatStr string, refreshDelay time.Duration) (Source, []URLToPrefetch, error) {
|
||||||
source := Source{url: url}
|
source := Source{url: url}
|
||||||
if formatStr != "v1" {
|
if formatStr != "v1" {
|
||||||
return source, fmt.Errorf("Unsupported source format: [%s]", formatStr)
|
return source, []URLToPrefetch{}, fmt.Errorf("Unsupported source format: [%s]", formatStr)
|
||||||
}
|
}
|
||||||
source.format = SourceFormatV1
|
source.format = SourceFormatV1
|
||||||
minisignKey, err := minisign.NewPublicKey(minisignKeyStr)
|
minisignKey, err := minisign.NewPublicKey(minisignKeyStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return source, err
|
return source, []URLToPrefetch{}, err
|
||||||
}
|
}
|
||||||
in, cached, err := fetchWithCache(url, cacheFile, refreshDelay)
|
sigURL := url + ".minisig"
|
||||||
|
when := time.Now().Add(SourcesUpdateDelayAfterFailure)
|
||||||
|
urlsToPrefetch := []URLToPrefetch{
|
||||||
|
URLToPrefetch{url: url, cacheFile: cacheFile, when: when},
|
||||||
|
URLToPrefetch{url: sigURL, cacheFile: cacheFile, when: when},
|
||||||
|
}
|
||||||
|
in, cached, delayTillNextUpdate, err := fetchWithCache(url, cacheFile, refreshDelay)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return source, err
|
return source, urlsToPrefetch, err
|
||||||
}
|
}
|
||||||
sigCacheFile := cacheFile + ".minisig"
|
sigCacheFile := cacheFile + ".minisig"
|
||||||
sigURL := url + ".minisig"
|
sigStr, sigCached, sigDelayTillNextUpdate, err := fetchWithCache(sigURL, sigCacheFile, refreshDelay)
|
||||||
sigStr, sigCached, err := fetchWithCache(sigURL, sigCacheFile, refreshDelay)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return source, err
|
return source, urlsToPrefetch, err
|
||||||
}
|
}
|
||||||
signature, err := minisign.DecodeSignature(sigStr)
|
signature, err := minisign.DecodeSignature(sigStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return source, err
|
return source, urlsToPrefetch, err
|
||||||
}
|
}
|
||||||
res, err := minisignKey.Verify([]byte(in), signature)
|
res, err := minisignKey.Verify([]byte(in), signature)
|
||||||
if err != nil || !res {
|
if err != nil || !res {
|
||||||
return source, err
|
return source, urlsToPrefetch, err
|
||||||
}
|
}
|
||||||
if !cached {
|
if !cached {
|
||||||
if err = AtomicFileWrite(cacheFile, []byte(in)); err != nil {
|
if err = AtomicFileWrite(cacheFile, []byte(in)); err != nil {
|
||||||
return source, err
|
return source, urlsToPrefetch, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !sigCached {
|
if !sigCached {
|
||||||
if err = AtomicFileWrite(sigCacheFile, []byte(sigStr)); err != nil {
|
if err = AtomicFileWrite(sigCacheFile, []byte(sigStr)); err != nil {
|
||||||
return source, err
|
return source, urlsToPrefetch, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dlog.Noticef("Source [%s] loaded", url)
|
dlog.Noticef("Source [%s] loaded", url)
|
||||||
source.in = in
|
source.in = in
|
||||||
return source, nil
|
if sigDelayTillNextUpdate < delayTillNextUpdate {
|
||||||
|
delayTillNextUpdate = sigDelayTillNextUpdate
|
||||||
|
}
|
||||||
|
if delayTillNextUpdate < SourcesUpdateDelayAfterFailure {
|
||||||
|
delayTillNextUpdate = SourcesUpdateDelayAfterFailure
|
||||||
|
}
|
||||||
|
when = time.Now().Add(delayTillNextUpdate)
|
||||||
|
urlsToPrefetch = []URLToPrefetch{}
|
||||||
|
return source, urlsToPrefetch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (source *Source) Parse() ([]RegisteredServer, error) {
|
func (source *Source) Parse() ([]RegisteredServer, error) {
|
||||||
|
@ -164,3 +195,15 @@ func (source *Source) Parse() ([]RegisteredServer, error) {
|
||||||
}
|
}
|
||||||
return registeredServers, nil
|
return registeredServers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PrefetchSourceURLs(urlsToPrefetch []URLToPrefetch) {
|
||||||
|
if len(urlsToPrefetch) <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dlog.Infof("Prefetching %d source URLs", len(urlsToPrefetch))
|
||||||
|
for _, urlToPrefetch := range urlsToPrefetch {
|
||||||
|
if _, _, _, err := fetchWithCache(urlToPrefetch.url, urlToPrefetch.cacheFile, time.Duration(0)); err != nil {
|
||||||
|
dlog.Debugf("[%s]: %s", urlToPrefetch.url, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue