* "refresh filters" HTTP handler: return the old synchronous behaviour

This was broken by "* dnsfilter: major refactoring"
This commit is contained in:
Simon Zolin 2019-10-10 17:12:32 +03:00
parent 3fbbda5102
commit c9d7bc3069
2 changed files with 29 additions and 16 deletions

View File

@ -177,8 +177,14 @@ func handleFilteringSetRules(w http.ResponseWriter, r *http.Request) {
} }
func handleFilteringRefresh(w http.ResponseWriter, r *http.Request) { func handleFilteringRefresh(w http.ResponseWriter, r *http.Request) {
beginRefreshFilters() config.controlLock.Unlock()
fmt.Fprintf(w, "OK 0 filters updated\n") nUpdated, err := refreshFilters()
config.controlLock.Lock()
if err != nil {
httpError(w, http.StatusInternalServerError, "%s", err)
return
}
fmt.Fprintf(w, "OK %d filters updated\n", nUpdated)
} }
type filterJSON struct { type filterJSON struct {

View File

@ -9,6 +9,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/AdguardTeam/AdGuardHome/dnsfilter" "github.com/AdguardTeam/AdGuardHome/dnsfilter"
@ -19,7 +20,8 @@ import (
var ( var (
nextFilterID = time.Now().Unix() // semi-stable way to generate an unique ID nextFilterID = time.Now().Unix() // semi-stable way to generate an unique ID
filterTitleRegexp = regexp.MustCompile(`^! Title: +(.*)$`) filterTitleRegexp = regexp.MustCompile(`^! Title: +(.*)$`)
forceRefresh bool refreshStatus uint32 // 0:none; 1:in progress
refreshLock sync.Mutex
) )
func initFiltering() { func initFiltering() {
@ -175,25 +177,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() {
nextRefresh := int64(0)
for { for {
if forceRefresh { if config.DNS.FiltersUpdateIntervalHours != 0 && refreshStatus != 0 {
_ = refreshFiltersIfNecessary(true) refreshStatus = 1
forceRefresh = false refreshLock.Lock()
}
if config.DNS.FiltersUpdateIntervalHours != 0 && nextRefresh <= time.Now().Unix() {
_ = refreshFiltersIfNecessary(false) _ = refreshFiltersIfNecessary(false)
nextRefresh = time.Now().Add(1 * time.Hour).Unix() refreshLock.Unlock()
refreshStatus = 0
} }
time.Sleep(1 * time.Second) time.Sleep(1 * time.Hour)
} }
} }
// Schedule the procedure to refresh filters // Refresh filters
func beginRefreshFilters() { func refreshFilters() (int, error) {
forceRefresh = true if refreshStatus != 0 { // we could use atomic cmpxchg here, but it's not really required
log.Debug("Filters: schedule update") return 0, fmt.Errorf("Filters update procedure is already running")
}
refreshStatus = 1
refreshLock.Lock()
nUpdated := refreshFiltersIfNecessary(true)
refreshLock.Unlock()
refreshStatus = 0
return nUpdated, nil
} }
// Checks filters updates if necessary // Checks filters updates if necessary