* filters: refactor: don't check Enabled flag inside filter.update() & filter.load()

This commit is contained in:
Simon Zolin 2019-03-18 12:52:34 +03:00
parent 56271819ea
commit afa54a1339
1 changed files with 10 additions and 13 deletions

View File

@ -52,15 +52,16 @@ func loadFilters() {
if filter.ID == 0 { if filter.ID == 0 {
filter.ID = assignUniqueFilterID() filter.ID = assignUniqueFilterID()
} }
if !filter.Enabled {
// No need to load a filter that is not enabled
continue
}
err := filter.load() err := filter.load()
if err != nil { if err != nil {
// This is okay for the first start, the filter will be loaded later // 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) 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{}
} }
} }
} }
@ -114,6 +115,10 @@ func refreshFiltersIfNecessary(force bool) int {
for i := range config.Filters { for i := range config.Filters {
filter := &config.Filters[i] // otherwise we will be operating on a copy filter := &config.Filters[i] // otherwise we will be operating on a copy
if !filter.Enabled {
continue
}
if filter.ID == 0 { // protect against users modifying the yaml and removing the ID if filter.ID == 0 { // protect against users modifying the yaml and removing the ID
filter.ID = assignUniqueFilterID() filter.ID = assignUniqueFilterID()
} }
@ -188,9 +193,6 @@ func (filter *filter) update(force bool) (bool, error) {
if filter.ID == 0 { // protect against users deleting the ID if filter.ID == 0 { // protect against users deleting the ID
filter.ID = assignUniqueFilterID() filter.ID = assignUniqueFilterID()
} }
if !filter.Enabled {
return false, nil
}
if !force && time.Since(filter.LastUpdated) <= updatePeriod { if !force && time.Since(filter.LastUpdated) <= updatePeriod {
return false, nil return false, nil
} }
@ -258,11 +260,6 @@ func (filter *filter) save() error {
// loads filter contents from the file in dataDir // loads filter contents from the file in dataDir
func (filter *filter) load() error { func (filter *filter) load() error {
if !filter.Enabled {
// No need to load a filter that is not enabled
return nil
}
filterFilePath := filter.Path() filterFilePath := filter.Path()
log.Tracef("Loading filter %d contents to: %s", filter.ID, filterFilePath) log.Tracef("Loading filter %d contents to: %s", filter.ID, filterFilePath)