Skip to content

Commit c22747d

Browse files
committed
Redesign DNS cache
2 parents 1b8634c + 1f9c227 commit c22747d

2 files changed

Lines changed: 58 additions & 51 deletions

File tree

internal/dns/cache.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package dns
2+
3+
import (
4+
"log/slog"
5+
"time"
6+
7+
"github.com/TecharoHQ/anubis/lib/store"
8+
9+
_ "github.com/TecharoHQ/anubis/lib/store/all"
10+
)
11+
12+
type DnsCache struct {
13+
forward store.JSON[[]string]
14+
reverse store.JSON[[]string]
15+
forwardTTL time.Duration
16+
reverseTTL time.Duration
17+
}
18+
19+
func NewDNSCache(forwardTTL int, reverseTTL int, backend store.Interface) *DnsCache {
20+
return &DnsCache{
21+
forward: store.JSON[[]string]{
22+
Underlying: backend,
23+
Prefix: "forwardDNS",
24+
},
25+
reverse: store.JSON[[]string]{
26+
Underlying: backend,
27+
Prefix: "reverseDNS",
28+
},
29+
forwardTTL: time.Duration(forwardTTL) * time.Second,
30+
reverseTTL: time.Duration(reverseTTL) * time.Second,
31+
}
32+
}
33+
34+
func (d *Dns) getCachedForward(host string) ([]string, bool) {
35+
if cached, err := d.cache.forward.Get(d.ctx, host); err == nil {
36+
slog.Debug("DNS: forward cache hit", "name", host, "ips", cached)
37+
return cached, true
38+
}
39+
slog.Debug("DNS: forward cache miss", "name", host)
40+
return nil, false
41+
}
42+
43+
func (d *Dns) getCachedReverse(addr string) ([]string, bool) {
44+
if cached, err := d.cache.reverse.Get(d.ctx, addr); err == nil {
45+
slog.Debug("DNS: reverse cache hit", "addr", addr, "names", cached)
46+
return cached, true
47+
}
48+
slog.Debug("DNS: reverse cache miss", "addr", addr)
49+
return nil, false
50+
}
51+
52+
func (d *Dns) forwardCachePut(host string, entries []string) {
53+
d.cache.forward.Set(d.ctx, host, entries, d.cache.forwardTTL)
54+
}
55+
56+
func (d *Dns) reverseCachePut(addr string, entries []string) {
57+
d.cache.reverse.Set(d.ctx, addr, entries, d.cache.reverseTTL)
58+
}

internal/dns/dns.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,25 @@ import (
1010
"regexp"
1111
"slices"
1212
"strings"
13-
"time"
14-
15-
"github.com/TecharoHQ/anubis/lib/store"
1613
)
1714

1815
var (
1916
DNSLookupAddr = net.LookupAddr
2017
DNSLookupHost = net.LookupHost
2118
)
2219

23-
type DnsCache struct {
24-
forward store.JSON[[]string]
25-
reverse store.JSON[[]string]
26-
forwardTTL time.Duration
27-
reverseTTL time.Duration
28-
}
29-
3020
type Dns struct {
3121
cache *DnsCache
3222
ctx context.Context
3323
}
3424

35-
func NewDNSCache(forwardTTL int, reverseTTL int, backend store.Interface) *DnsCache {
36-
return &DnsCache{
37-
forward: store.JSON[[]string]{
38-
Underlying: backend,
39-
Prefix: "forwardDNS",
40-
},
41-
reverse: store.JSON[[]string]{
42-
Underlying: backend,
43-
Prefix: "reverseDNS",
44-
},
45-
forwardTTL: time.Duration(forwardTTL) * time.Second,
46-
reverseTTL: time.Duration(reverseTTL) * time.Second,
47-
}
48-
}
49-
5025
func New(ctx context.Context, cache *DnsCache) *Dns {
5126
return &Dns{
5227
cache: cache,
5328
ctx: ctx,
5429
}
5530
}
5631

57-
func (d *Dns) getCachedForward(host string) ([]string, bool) {
58-
if cached, err := d.cache.forward.Get(d.ctx, host); err == nil {
59-
slog.Debug("DNS: forward cache hit", "name", host, "ips", cached)
60-
return cached, true
61-
}
62-
slog.Debug("DNS: forward cache miss", "name", host)
63-
return nil, false
64-
}
65-
66-
func (d *Dns) getCachedReverse(addr string) ([]string, bool) {
67-
if cached, err := d.cache.reverse.Get(d.ctx, addr); err == nil {
68-
slog.Debug("DNS: reverse cache hit", "addr", addr, "names", cached)
69-
return cached, true
70-
}
71-
slog.Debug("DNS: reverse cache miss", "addr", addr)
72-
return nil, false
73-
}
74-
75-
func (d *Dns) forwardCachePut(host string, entries []string) {
76-
d.cache.forward.Set(d.ctx, host, entries, d.cache.forwardTTL)
77-
}
78-
79-
func (d *Dns) reverseCachePut(addr string, entries []string) {
80-
d.cache.reverse.Set(d.ctx, addr, entries, d.cache.reverseTTL)
81-
}
82-
8332
// ReverseDNS performs a reverse DNS lookup for the given IP address and trims the trailing dot from the results.
8433
func (d *Dns) ReverseDNS(addr string) ([]string, error) {
8534
slog.Debug("DNS: performing reverse lookup", "addr", addr)

0 commit comments

Comments
 (0)