- filter: update 'LastUpdated' field and 'last-modified' file time

even when filter's content is up to date

* filters: refactor: don't check 'LastUpdated' inside update()
This commit is contained in:
Simon Zolin 2019-03-15 16:09:43 +03:00
parent b54f540f71
commit d664a9de1d
2 changed files with 13 additions and 10 deletions

View File

@ -569,7 +569,7 @@ func handleFilteringAddURL(w http.ResponseWriter, r *http.Request) {
f.Enabled = true f.Enabled = true
// Download the filter contents // Download the filter contents
ok, err := f.update(true) ok, err := f.update()
if err != nil { if err != nil {
httpError(w, http.StatusBadRequest, "Couldn't fetch filter from url %s: %s", f.URL, err) httpError(w, http.StatusBadRequest, "Couldn't fetch filter from url %s: %s", f.URL, err)
return return

View File

@ -179,7 +179,11 @@ func refreshFiltersIfNecessary(force bool) int {
continue continue
} }
updated, err := filter.update(force) if !force && time.Since(filter.LastUpdated) <= updatePeriod {
continue
}
updated, err := filter.update()
if err != nil { if err != nil {
log.Printf("Failed to update filter %s: %s\n", filter.URL, err) log.Printf("Failed to update filter %s: %s\n", filter.URL, err)
continue continue
@ -193,6 +197,11 @@ func refreshFiltersIfNecessary(force bool) int {
} }
updateCount++ updateCount++
} else {
mtime := time.Now()
os.Chtimes(filter.Path(), mtime, mtime)
filter.LastUpdated = mtime
} }
} }
config.Unlock() config.Unlock()
@ -236,14 +245,8 @@ func parseFilterContents(contents []byte) (int, string, []string) {
return rulesCount, name, lines return rulesCount, name, lines
} }
// Checks for filters updates // Perform upgrade on a filter
// If "force" is true -- does not check the filter's LastUpdated field func (filter *filter) update() (bool, error) {
// Call "save" to persist the filter contents
func (filter *filter) update(force bool) (bool, error) {
if !force && time.Since(filter.LastUpdated) <= updatePeriod {
return false, nil
}
log.Tracef("Downloading update for filter %d from %s", filter.ID, filter.URL) log.Tracef("Downloading update for filter %d from %s", filter.ID, filter.URL)
resp, err := client.Get(filter.URL) resp, err := client.Get(filter.URL)