Skip to content

Commit

Permalink
filtering: imp docs
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Oct 21, 2022
1 parent ef8228f commit c9f3e33
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
17 changes: 9 additions & 8 deletions internal/filtering/filter.go
Expand Up @@ -90,7 +90,7 @@ func (d *DNSFilter) filterSetProperties(

filt := &filters[i]
log.Debug(
"set name to %q, url to %s, enabled to %t for filter %s",
"filtering: set name to %q, url to %s, enabled to %t for filter %s",
newList.Name,
newList.URL,
newList.Enabled,
Expand All @@ -109,7 +109,7 @@ func (d *DNSFilter) filterSetProperties(
filt.Name = newList.Name

if filt.URL != newList.URL {
if d.filterExistsIntl(newList.URL) {
if d.filterExistsLocked(newList.URL) {
return false, errFilterExists
}

Expand Down Expand Up @@ -142,19 +142,20 @@ func (d *DNSFilter) filterSetProperties(
return shouldRestart, err
}

// filterExists returns true if a filter with the same url exists in d.
// filterExists returns true if a filter with the same url exists in d. It's
// safe for concurrent use.
func (d *DNSFilter) filterExists(url string) (ok bool) {
d.filtersMu.RLock()
defer d.filtersMu.RUnlock()

r := d.filterExistsIntl(url)
r := d.filterExistsLocked(url)

return r
}

// filterExistsIntl returns true if d contains the filter with the same url.
// For internal use only.
func (d *DNSFilter) filterExistsIntl(url string) (ok bool) {
// filterExistsLocked returns true if d contains the filter with the same url.
// d.filtersMu is expected to be locked.
func (d *DNSFilter) filterExistsLocked(url string) (ok bool) {
for _, f := range d.Filters {
if f.URL == url {
return true
Expand All @@ -177,7 +178,7 @@ func (d *DNSFilter) filterAdd(flt FilterYAML) bool {
defer d.filtersMu.Unlock()

// Check for duplicates
if d.filterExistsIntl(flt.URL) {
if d.filterExistsLocked(flt.URL) {
return false
}

Expand Down
5 changes: 3 additions & 2 deletions internal/filtering/filter_test.go
Expand Up @@ -17,8 +17,9 @@ import (
"github.com/stretchr/testify/require"
)

// serveFiltersLocally is a helper that concurrently listens on a free port
// to respond with fltContent.
// serveFiltersLocally is a helper that concurrently listens on a free port to
// respond with fltContent. It also gracefully closes the listener when the
// test under t finishes.
func serveFiltersLocally(t *testing.T, fltContent []byte) (ipp netip.AddrPort) {
t.Helper()

Expand Down
2 changes: 1 addition & 1 deletion internal/filtering/filtering.go
Expand Up @@ -366,7 +366,7 @@ func (d *DNSFilter) SetFilters(blockFilters, allowFilters []Filter, async bool)

err := d.initFiltering(allowFilters, blockFilters)
if err != nil {
log.Error("can't initialize filtering subsystem: %s", err)
log.Error("filtering: can't initialize filtering subsystem: %s", err)

return err
}
Expand Down

0 comments on commit c9f3e33

Please sign in to comment.