* refactor: move code to loadFilters()

This commit is contained in:
Simon Zolin 2019-03-15 19:41:45 +03:00
parent b4732c83c5
commit 61b1a30aa1
2 changed files with 22 additions and 19 deletions

20
app.go
View file

@ -12,7 +12,6 @@ import (
"strconv"
"sync"
"syscall"
"time"
"github.com/AdguardTeam/golibs/log"
"github.com/gobuffalo/packr"
@ -101,24 +100,7 @@ func run(args options) {
config.BindPort = args.bindPort
}
// Load filters from the disk
// And if any filter has zero ID, assign a new one
for i := range config.Filters {
filter := &config.Filters[i] // otherwise we're operating on a copy
if filter.ID == 0 {
filter.ID = assignUniqueFilterID()
}
err = filter.load()
if err != nil {
// This is okay for the first start, the filter will be loaded later
log.Debug("Couldn't load filter %d contents due to %s", filter.ID, err)
// clear LastUpdated so it gets fetched right away
}
if len(filter.Rules) == 0 {
filter.LastUpdated = time.Time{}
}
}
loadFilters()
// Save the updated config
err = config.write()

View file

@ -44,6 +44,27 @@ func userFilter() filter {
}
}
// Load filters from the disk
// And if any filter has zero ID, assign a new one
func loadFilters() {
for i := range config.Filters {
filter := &config.Filters[i] // otherwise we're operating on a copy
if filter.ID == 0 {
filter.ID = assignUniqueFilterID()
}
err := filter.load()
if err != nil {
// This is okay for the first start, the filter will be loaded later
log.Debug("Couldn't load filter %d contents due to %s", filter.ID, err)
// clear LastUpdated so it gets fetched right away
}
if len(filter.Rules) == 0 {
filter.LastUpdated = time.Time{}
}
}
}
func deduplicateFilters() {
// Deduplicate filters
i := 0 // output index, used for deletion later