Skip to content

Commit 9a3f3d2

Browse files
TMHSDigitalclaude
andcommitted
Book per-resolver rate slots per wire query (#236)
The pool reserved len(l.Types) slots of a member's rate cap per lookup, or one for empty types, while the lookup sends sum(queriesPerType): two for the default A and AAAA, three per CNAME. Library callers on default types and anyone with CNAME in -type sent members up to twice the -rate-per-resolver cap. Add queriesFor(types), matching what the global limiter charges, and use it at every reserve call. Fixes #236 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9ca80f1 commit 9a3f3d2

4 files changed

Lines changed: 68 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4444
- An unreadable `-seeds` file no longer leaves a hidden `.<name>.tmp` file beside the `-o` path (kept locked on Windows until exit): seeds are read before the output file is opened (#212).
4545
- On Windows, `-simulate-zone` labs and the test DNS server no longer fail to start about a quarter of the time with "no free UDP+TCP port pair": after a failed TCP bind the server retries on random ports instead of the next OS-assigned one, which stayed inside the same reserved port block (#204).
4646
- A `scan.Config` built directly with `Attempts: 0` now makes one attempt per name instead of passing preflight and then failing every lookup without a query. Scan workers, `scan.Verify` and the wildcard near-miss probes share one resolve path (test hook, simulation, or the trusted resolver), and the `internal/dns` lookup helpers take a `dns.Lookup` value (resolver, timeout, attempts, types, logger) instead of long positional parameter lists, so a swapped attempts/types argument no longer compiles (#163).
47+
- `-rate-per-resolver` (and `pkg/subenum`'s `RatePerResolver`) now holds: each pool lookup books one slot per DNS query it sends, as `-rate` does, instead of one per record type. The default A and AAAA lookup from the library booked one slot for two queries, and a CNAME lookup one for three, so members got up to twice the cap (#236).
4748

4849
### Security
4950
- The GitHub Action checks the downloaded release archive's build provenance with `gh attestation verify` as well as its `checksums.txt`, since an archive and its checksums file could be replaced together. The Pages workflow grants `pages: write` and `id-token: write` only to the job that deploys, not to the whole workflow (#210).

‎internal/dns/pool.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func (p *Pool) checkCanary(ctx context.Context, m *PoolResolver, l Lookup) {
360360
return
361361
}
362362
name := p.canary()
363-
if name == "" || p.reserve(ctx, m, len(l.Types)) != nil {
363+
if name == "" || p.reserve(ctx, m, queriesFor(l.Types)) != nil {
364364
m.mu.Lock()
365365
m.canaryBusy = false
366366
m.mu.Unlock()
@@ -402,7 +402,7 @@ func (p *Pool) Resolve(ctx context.Context, l Lookup, domain string) PoolAnswer
402402
}
403403
last = m
404404
p.checkCanary(ctx, m, l)
405-
if p.reserve(ctx, m, len(l.Types)) != nil {
405+
if p.reserve(ctx, m, queriesFor(l.Types)) != nil {
406406
return PoolAnswer{Outcome: OutcomeCanceled}
407407
}
408408
var recs []Record
@@ -430,7 +430,7 @@ func (p *Pool) secondOpinion(ctx context.Context, m *PoolResolver, l Lookup, dom
430430
return nx
431431
}
432432
other := p.pickHealthy(m)
433-
if other == nil || p.reserve(ctx, other, len(l.Types)) != nil {
433+
if other == nil || p.reserve(ctx, other, queriesFor(l.Types)) != nil {
434434
return nx
435435
}
436436
recs, o := other.lookup(l).Resolve(ctx, domain)

‎internal/dns/pool_test.go‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,55 @@ func TestPoolRotatesAndBenches(t *testing.T) {
6363
}
6464
}
6565

66+
// TestQueriesFor checks that the pool books the queries a lookup really
67+
// sends: default types are two, and CNAME is three (#236).
68+
func TestQueriesFor(t *testing.T) {
69+
for _, tc := range []struct {
70+
types []string
71+
want int
72+
}{
73+
{nil, 2},
74+
{[]string{"A"}, 1},
75+
{[]string{"A", "AAAA"}, 2},
76+
{[]string{"A", "AAAA", "CNAME"}, 5},
77+
{[]string{"NS", "MX", "TXT"}, 3},
78+
} {
79+
if got := queriesFor(tc.types); got != tc.want {
80+
t.Errorf("queriesFor(%v) = %d, want %d", tc.types, got, tc.want)
81+
}
82+
}
83+
}
84+
85+
// TestPoolReservesWireQueries resolves through a capped member on a fake
86+
// clock and checks the slots Resolve books (#236): one per wire query the
87+
// lookup sends, not one per record type, so default types book two and a
88+
// CNAME lookup three.
89+
func TestPoolReservesWireQueries(t *testing.T) {
90+
srv := startTestDNS(t, map[string]testReply{"www.example.com": {A: "192.0.2.10", AAAA: "2001:db8::10"}})
91+
for _, tc := range []struct {
92+
types []string
93+
want int // slots booked by one lookup
94+
}{
95+
{nil, 2},
96+
{[]string{"A", "AAAA", "CNAME"}, 5},
97+
} {
98+
p := NewPool([]string{srv.addr}, 2*time.Second)
99+
p.SetRatePerResolver(10) // 100ms per query
100+
now := time.Unix(0, 0)
101+
p.now = func() time.Time { return now }
102+
p.sleep = func(context.Context, time.Duration) error { return nil }
103+
m := p.members[0]
104+
m.trusted = true // no canary or second opinion: only the lookup books
105+
ans := p.Resolve(context.Background(), Lookup{Timeout: 2 * time.Second, Types: tc.types}, "www.example.com")
106+
if ans.Outcome != OutcomeFound {
107+
t.Fatalf("types %v: outcome %v, want found", tc.types, ans.Outcome)
108+
}
109+
if want := now.Add(time.Duration(tc.want) * 100 * time.Millisecond); !m.free.Equal(want) {
110+
t.Errorf("types %v: member free at %v, want %v (%d slots)", tc.types, m.free.Sub(now), want.Sub(now), tc.want)
111+
}
112+
}
113+
}
114+
66115
// TestPoolRatePerResolver drives the per-resolver cap (#196) on a fake clock:
67116
// lookups go to members with room first, wait for the one that frees up
68117
// soonest when every cap is spent, and count each spent cap as throttled.

‎internal/dns/resolver.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,21 @@ func fqdn(domain string) string {
274274
// dialed (see NewResolver).
275275
var queriesPerType = map[string]int32{"A": 1, "AAAA": 1, "CNAME": 3, "NS": 1, "MX": 1, "TXT": 1}
276276

277+
// queriesFor is how many queries one lookup of types puts on the wire, by
278+
// queriesPerType; empty types means DefaultTypes, as in ResolveTypes. The
279+
// pool's per-resolver cap reserves this many slots, so it matches what the
280+
// global -rate limiter charges (#236).
281+
func queriesFor(types []string) int {
282+
if len(types) == 0 {
283+
types = DefaultTypes
284+
}
285+
n := 0
286+
for _, t := range types {
287+
n += int(queriesPerType[t])
288+
}
289+
return n
290+
}
291+
277292
// Lookup is how one name is looked up: the resolver, the per-type timeout,
278293
// how many attempts a retried lookup makes, the record types, and the verbose
279294
// logger. Naming the fields keeps a swapped attempts/types argument from

0 commit comments

Comments
 (0)