-
-
Notifications
You must be signed in to change notification settings - Fork 925
[client] Implement DNS query caching in DNSForwarder #4574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
815e6fd
[client] Implement DNS query caching in DNSForwarder
hakansa dd1ee59
Fix cache get method to check for non-empty address slices
hakansa a928540
Normalize domain names in cache to ensure consistent casing and trail…
hakansa 3a40f69
Refactor cache methods to use slices.Clone for address slices
hakansa bdda31d
cache nxdomain result
hakansa 2eefe14
Enhance DNSForwarder to handle NXDOMAIN responses when cache is empty
hakansa 7d69d65
Add cache unset method and remove stale cache entries in DNSForwarder
hakansa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package dnsfwd | ||
|
|
||
| import ( | ||
| "net/netip" | ||
| "slices" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/miekg/dns" | ||
| ) | ||
|
|
||
| type cache struct { | ||
| mu sync.RWMutex | ||
| records map[string]*cacheEntry | ||
| } | ||
|
|
||
| type cacheEntry struct { | ||
| ip4Addrs []netip.Addr | ||
| ip6Addrs []netip.Addr | ||
| } | ||
|
|
||
| func newCache() *cache { | ||
| return &cache{ | ||
| records: make(map[string]*cacheEntry), | ||
| } | ||
| } | ||
|
|
||
| func (c *cache) get(domain string, reqType uint16) ([]netip.Addr, bool) { | ||
| c.mu.RLock() | ||
| defer c.mu.RUnlock() | ||
|
|
||
| entry, exists := c.records[normalizeDomain(domain)] | ||
| if !exists { | ||
| return nil, false | ||
| } | ||
|
|
||
| switch reqType { | ||
| case dns.TypeA: | ||
| return slices.Clone(entry.ip4Addrs), true | ||
| case dns.TypeAAAA: | ||
| return slices.Clone(entry.ip6Addrs), true | ||
| default: | ||
| return nil, false | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func (c *cache) set(domain string, reqType uint16, addrs []netip.Addr) { | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
| norm := normalizeDomain(domain) | ||
| entry, exists := c.records[norm] | ||
| if !exists { | ||
| entry = &cacheEntry{} | ||
| c.records[norm] = entry | ||
| } | ||
|
|
||
| switch reqType { | ||
| case dns.TypeA: | ||
| entry.ip4Addrs = slices.Clone(addrs) | ||
| case dns.TypeAAAA: | ||
| entry.ip6Addrs = slices.Clone(addrs) | ||
| } | ||
| } | ||
|
|
||
| // unset removes cached entries for the given domain and request type. | ||
| func (c *cache) unset(domain string) { | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
| delete(c.records, normalizeDomain(domain)) | ||
| } | ||
|
|
||
| // normalizeDomain converts an input domain into a canonical form used as cache key: | ||
| // lowercase and fully-qualified (with trailing dot). | ||
| func normalizeDomain(domain string) string { | ||
| // dns.Fqdn ensures trailing dot; ToLower for consistent casing | ||
| return dns.Fqdn(strings.ToLower(domain)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package dnsfwd | ||
|
|
||
| import ( | ||
| "net/netip" | ||
| "testing" | ||
| ) | ||
|
|
||
| func mustAddr(t *testing.T, s string) netip.Addr { | ||
| t.Helper() | ||
| a, err := netip.ParseAddr(s) | ||
| if err != nil { | ||
| t.Fatalf("parse addr %s: %v", s, err) | ||
| } | ||
| return a | ||
| } | ||
|
|
||
| func TestCacheNormalization(t *testing.T) { | ||
| c := newCache() | ||
|
|
||
| // Mixed case, without trailing dot | ||
| domainInput := "ExAmPlE.CoM" | ||
| ipv4 := []netip.Addr{mustAddr(t, "1.2.3.4")} | ||
| c.set(domainInput, 1 /* dns.TypeA */, ipv4) | ||
|
|
||
| // Lookup with lower, with trailing dot | ||
| if got, ok := c.get("example.com.", 1); !ok || len(got) != 1 || got[0].String() != "1.2.3.4" { | ||
| t.Fatalf("expected cached IPv4 result via normalized key, got=%v ok=%v", got, ok) | ||
| } | ||
|
|
||
| // Lookup with different casing again | ||
| if got, ok := c.get("EXAMPLE.COM", 1); !ok || len(got) != 1 || got[0].String() != "1.2.3.4" { | ||
| t.Fatalf("expected cached IPv4 result via different casing, got=%v ok=%v", got, ok) | ||
| } | ||
| } | ||
|
|
||
| func TestCacheSeparateTypes(t *testing.T) { | ||
| c := newCache() | ||
|
|
||
| domain := "test.local" | ||
| ipv4 := []netip.Addr{mustAddr(t, "10.0.0.1")} | ||
| ipv6 := []netip.Addr{mustAddr(t, "2001:db8::1")} | ||
|
|
||
| c.set(domain, 1 /* A */, ipv4) | ||
| c.set(domain, 28 /* AAAA */, ipv6) | ||
|
|
||
| got4, ok4 := c.get(domain, 1) | ||
| if !ok4 || len(got4) != 1 || got4[0] != ipv4[0] { | ||
| t.Fatalf("expected A record from cache, got=%v ok=%v", got4, ok4) | ||
| } | ||
|
|
||
| got6, ok6 := c.get(domain, 28) | ||
| if !ok6 || len(got6) != 1 || got6[0] != ipv6[0] { | ||
| t.Fatalf("expected AAAA record from cache, got=%v ok=%v", got6, ok6) | ||
| } | ||
| } | ||
|
|
||
| func TestCacheCloneOnGetAndSet(t *testing.T) { | ||
| c := newCache() | ||
| domain := "clone.test" | ||
|
|
||
| src := []netip.Addr{mustAddr(t, "8.8.8.8")} | ||
| c.set(domain, 1, src) | ||
|
|
||
| // Mutate source slice; cache should be unaffected | ||
| src[0] = mustAddr(t, "9.9.9.9") | ||
|
|
||
| got, ok := c.get(domain, 1) | ||
| if !ok || len(got) != 1 || got[0].String() != "8.8.8.8" { | ||
| t.Fatalf("expected cached value to be independent of source slice, got=%v ok=%v", got, ok) | ||
| } | ||
|
|
||
| // Mutate returned slice; internal cache should remain unchanged | ||
| got[0] = mustAddr(t, "4.4.4.4") | ||
| got2, ok2 := c.get(domain, 1) | ||
| if !ok2 || len(got2) != 1 || got2[0].String() != "8.8.8.8" { | ||
| t.Fatalf("expected returned slice to be a clone, got=%v ok=%v", got2, ok2) | ||
| } | ||
| } | ||
|
|
||
| func TestCacheMiss(t *testing.T) { | ||
| c := newCache() | ||
| if got, ok := c.get("missing.example", 1); ok || got != nil { | ||
| t.Fatalf("expected cache miss, got=%v ok=%v", got, ok) | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.