From c9f3e337be8f005cc667d1cfd505f8cbca97cf20 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Fri, 21 Oct 2022 14:49:53 +0300 Subject: [PATCH] filtering: imp docs --- internal/filtering/filter.go | 17 +++++++++-------- internal/filtering/filter_test.go | 5 +++-- internal/filtering/filtering.go | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/internal/filtering/filter.go b/internal/filtering/filter.go index 13cddd2429d..070ce73ce12 100644 --- a/internal/filtering/filter.go +++ b/internal/filtering/filter.go @@ -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, @@ -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 } @@ -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 @@ -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 } diff --git a/internal/filtering/filter_test.go b/internal/filtering/filter_test.go index eee3ed0493d..99014bd0fa7 100644 --- a/internal/filtering/filter_test.go +++ b/internal/filtering/filter_test.go @@ -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() diff --git a/internal/filtering/filtering.go b/internal/filtering/filtering.go index 56aaec26553..a1de94cd4a5 100644 --- a/internal/filtering/filtering.go +++ b/internal/filtering/filtering.go @@ -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 }