Merge: * filters: use increasing update interval

Close #1246

Squashed commit of the following:

commit d0b40719457c1bc41c2f32b425c95e35916366e9
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Wed Jan 15 13:41:45 2020 +0300

    minor

commit c1b682156c1b71dcfc2febe02e3df0a4d5e0a81b
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Tue Jan 14 20:09:27 2020 +0300

    * filters: increasing update interval
This commit is contained in:
Simon Zolin 2020-01-15 18:41:27 +03:00
parent c23c323b1a
commit 47631105c7
1 changed files with 29 additions and 5 deletions

View File

@ -183,15 +183,30 @@ func assignUniqueFilterID() int64 {
// Sets up a timer that will be checking for filters updates periodically // Sets up a timer that will be checking for filters updates periodically
func periodicallyRefreshFilters() { func periodicallyRefreshFilters() {
const maxInterval = 1 * 60 * 60
intval := 5 // use a dynamically increasing time interval
nUpdated := 0
for { for {
isNetworkErr := false
if config.DNS.FiltersUpdateIntervalHours != 0 && refreshStatus == 0 { if config.DNS.FiltersUpdateIntervalHours != 0 && refreshStatus == 0 {
refreshStatus = 1 refreshStatus = 1
refreshLock.Lock() refreshLock.Lock()
_ = refreshFiltersIfNecessary(false) nUpdated, isNetworkErr = refreshFiltersIfNecessary(false)
refreshLock.Unlock() refreshLock.Unlock()
refreshStatus = 0 refreshStatus = 0
if nUpdated != 0 {
intval = maxInterval
}
} }
time.Sleep(1 * time.Hour)
if isNetworkErr {
intval *= 2
if intval > maxInterval {
intval = maxInterval
}
}
time.Sleep(time.Duration(intval) * time.Second)
} }
} }
@ -203,7 +218,7 @@ func refreshFilters() (int, error) {
refreshStatus = 1 refreshStatus = 1
refreshLock.Lock() refreshLock.Lock()
nUpdated := refreshFiltersIfNecessary(true) nUpdated, _ := refreshFiltersIfNecessary(true)
refreshLock.Unlock() refreshLock.Unlock()
refreshStatus = 0 refreshStatus = 0
return nUpdated, nil return nUpdated, nil
@ -223,7 +238,10 @@ func refreshFilters() (int, error) {
// . Pass new filters to dnsfilter object - it analyzes new data while the old filters are still active // . Pass new filters to dnsfilter object - it analyzes new data while the old filters are still active
// . dnsfilter activates new filters // . dnsfilter activates new filters
// . Remove the old filter files (1.txt.old) // . Remove the old filter files (1.txt.old)
func refreshFiltersIfNecessary(force bool) int { //
// Return the number of updated filters
// Return TRUE - there was a network error and nothing could be updated
func refreshFiltersIfNecessary(force bool) (int, bool) {
var updateFilters []filter var updateFilters []filter
var updateFlags []bool // 'true' if filter data has changed var updateFlags []bool // 'true' if filter data has changed
@ -252,17 +270,23 @@ func refreshFiltersIfNecessary(force bool) int {
} }
config.RUnlock() config.RUnlock()
nfail := 0
for i := range updateFilters { for i := range updateFilters {
uf := &updateFilters[i] uf := &updateFilters[i]
updated, err := uf.update() updated, err := uf.update()
updateFlags = append(updateFlags, updated) updateFlags = append(updateFlags, updated)
if err != nil { if err != nil {
nfail++
log.Printf("Failed to update filter %s: %s\n", uf.URL, err) log.Printf("Failed to update filter %s: %s\n", uf.URL, err)
continue continue
} }
uf.LastUpdated = now uf.LastUpdated = now
} }
if nfail == len(updateFilters) {
return 0, true
}
updateCount := 0 updateCount := 0
for i := range updateFilters { for i := range updateFilters {
uf := &updateFilters[i] uf := &updateFilters[i]
@ -316,7 +340,7 @@ func refreshFiltersIfNecessary(force bool) int {
} }
log.Debug("Filters: update finished") log.Debug("Filters: update finished")
return updateCount return updateCount, false
} }
// Allows printable UTF-8 text with CR, LF, TAB characters // Allows printable UTF-8 text with CR, LF, TAB characters