Skip to content

Commit 77212ae

Browse files
TMHSDigitalclaude
andcommitted
Bench dead pool members fast and back off repeat benches (#463)
A dead -r member was benched only after 20 lookups in a window with more than half failed, for a fixed 30 s, and its canary failures did not count, so each dead member absorbed about 20 double timeouts every 30 s cycle. Now 3 failures in a row bench a member at once (the window rule stays for intermittent failures), each repeat bench lasts twice the one before up to 4 minutes until the member answers again, and a failed canary check counts as a failed lookup. Late reports from lookups in flight do not re-bench a benched member. Fixes #463 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f0db944 commit 77212ae

5 files changed

Lines changed: 126 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- `-max-duration` is kept under a low `-rate`: workers waiting for a rate slot when the budget ran out still sent their queries afterwards, so a 2 s budget at `-rate 5` ran about 43 s. No slot is handed out past the deadline now, a name's queries are sent all together or not at all, and names that never got a slot are counted as skipped and stay resumable (#460).
2121
- With `-dL` and `-rate`, the next target's `-axfr` zone transfer and `-nsec-walk` no longer run beside the current target's scan, where their queries came on top of `-rate`; only its passive sources are fetched ahead, and the transfer and walk run when that target starts. Without `-rate` nothing changes (#461).
2222
- Takeover candidates and internal addresses found by the `-permute` and `-ptr` passes now count in `takeover_candidates` and `internal_addresses` (in `-stats`, `subenum report` and the Action's `takeovers` and `internal` outputs), as they already did in the results; under `-permute-only` they were always 0. The Action's `found` output and the report's Found column also include `ptr_found` (#458).
23+
- A dead `-r` pool member is benched after 3 failures in a row instead of a window of 20 lookups, each repeat bench doubles (30 s up to 4 min) until it answers again, and a failed canary check counts as a failure, so dead entries in a resolver list stop costing a window of timeouts every 30 s (#463).
2324
- `-hosts` with `-dL` no longer walks the whole host list once per target: the names are sorted by target once, so a list of 200,000 names over 2,000 targets is split in about 11 ms instead of about 9 s of CPU before and between targets (#465).
2425

2526
## [0.12.0] - 2026-11-29

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ This table describes `main`, which can be ahead of the latest release: a release
276276
| `-recursive-w <file>` | the `-w` list | Wordlist of the labels `-recursive` tries under each name it finds, at depth 2 and below (`-` reads stdin); repeatable. A short list here makes deep recursion affordable |
277277
| **Resolvers and pacing** | | |
278278
| `-dns-server <addr>` | `8.8.8.8:53` | DNS server: `ip:port` (UDP, TCP fallback), `tls://host[:853]` (DNS over TLS) or `https://host/path` (DNS over HTTPS); validated on startup |
279-
| `-r <file>` | n/a | Resolver pool: one resolver per line, in any form `-dns-server` takes: `ip`, `ip:port`, `tls://host[:port]` (DNS over TLS) or `https://host/path` (DNS over HTTPS); the forms can be mixed. Lookups rotate over the healthy resolvers (a resolver failing over half its recent lookups is benched for 30s), retries go to a different resolver, and every hit is re-validated against `-dns-server`, whose answer is the one reported. NXDOMAIN answers are checked too: each resolver is periodically asked about a name `-dns-server` confirmed (a canary), and an NXDOMAIN from one that has not passed a canary is confirmed by a second resolver. A resolver contradicted three times is flagged and dropped for the run. Preflight and wildcard checks use `-dns-server` |
279+
| `-r <file>` | n/a | Resolver pool: one resolver per line, in any form `-dns-server` takes: `ip`, `ip:port`, `tls://host[:port]` (DNS over TLS) or `https://host/path` (DNS over HTTPS); the forms can be mixed. Lookups rotate over the healthy resolvers (a resolver failing 3 lookups in a row or over half its recent ones is benched for 30s, longer each time it is benched again), retries go to a different resolver, and every hit is re-validated against `-dns-server`, whose answer is the one reported. NXDOMAIN answers are checked too: each resolver is periodically asked about a name `-dns-server` confirmed (a canary), and an NXDOMAIN from one that has not passed a canary is confirmed by a second resolver. A resolver contradicted three times is flagged and dropped for the run. Preflight and wildcard checks use `-dns-server` |
280280
| `-t <n>` | `100` | Concurrent worker goroutines |
281281
| `-timeout <ms>` | `1000` | DNS timeout in milliseconds, applied to each record-type lookup separately |
282282
| `-attempts <n>` | `1` | DNS resolution attempts per subdomain (1 = no retry) |

‎docs/ARCHITECTURE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ tools/licenses/main.go - THIRD_PARTY_LICENSES for release archives and t
175175

176176
`scan.Run` builds one resolver per scan (unless `Config.Resolver` is set) and reuses it for every lookup; `dns.CloseIdleConnections` releases its pooled connections when the scan ends.
177177
* **Resolver pool (`-r`)**: `dns.NewPoolFromLines` parses the `-r` file (`ip` or `ip:port`, port 53 by default; `tls://host[:port]` for DNS over TLS; `https://host/path` for DNS over HTTPS) and builds a `dns.Pool` over it with the `-rate-per-resolver` cap; the TUI and `pkg/subenum` build their pools with it too. Candidate lookups go round-robin over the healthy members; the `-dns-server` resolver stays the *trusted* resolver and runs the preflight and wildcard probes. Safeguards:
178-
* **Health benching**: over each window of 20 lookups, a member that failed (timeout, refused, other) more than half of them is benched for 30 seconds, then tried again.
178+
* **Health benching**: a member that fails (timeout, refused, other) 3 lookups in a row, or more than half of a window of 20, is benched for 30 seconds, then tried again. Each repeat bench lasts twice the one before, up to 4 minutes, until the member answers again, and a failed canary check counts as a failed lookup, so a dead member stops absorbing a window of timeouts every cycle.
179179
* **Per-resolver rate cap (`-rate-per-resolver`)**: `Pool.SetRatePerResolver` spaces the queries sent to each member like `-rate` does globally. A lookup prefers a member with room, waits for the one that frees up soonest when every cap is spent (before its timeout starts), and books one slot per record type; canaries and second opinions book slots too. Each spent cap a lookup runs into counts as `throttled`. The global `-rate` limiter still applies, so the stricter of the two wins.
180180
* **Hit re-validation**: every pool hit is resolved again on the trusted resolver, and the trusted answer is the one reported (`resolveViaPool` in `internal/scan/runner.go`), so a poisoned member cannot inject names.
181181
* **Canaries and second opinions**: names the trusted resolver confirmed (the apex, then each confirmed hit) become canaries. A member is asked a canary every 25 lookups (before each lookup while it is untrusted); one that has not answered its last canary correctly has its NXDOMAIN answers checked by a second member.

‎internal/dns/pool.go‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dns
22

33
import (
4+
"cmp"
45
"context"
56
"errors"
67
"fmt"
@@ -16,7 +17,13 @@ import (
1617
const (
1718
poolHealthMinLookups = 20 // lookups before a resolver can be benched
1819
poolHealthFailPct = 50 // failure share that benches a resolver
19-
poolBenchFor = 30 * time.Second // how long a benched resolver sits out
20+
poolBenchFor = 30 * time.Second // how long a benched resolver first sits out
21+
// A resolver that fails this many lookups in a row is benched at once,
22+
// without waiting for a window, and each bench after the first lasts
23+
// twice the one before, up to poolBenchMax, until it answers again: a
24+
// dead member stops absorbing a window of timeouts every cycle (#463).
25+
poolBenchStreak = 3
26+
poolBenchMax = 8 * poolBenchFor
2027
)
2128

2229
// Pool honesty checks (#105). A member's NXDOMAIN answers are taken at face
@@ -39,8 +46,10 @@ type PoolResolver struct {
3946

4047
mu sync.Mutex
4148
stats ResolverStats
42-
window int // lookups since the last health reset
43-
windowKO int // failures in that window
49+
window int // lookups since the last health reset
50+
windowKO int // failures in that window
51+
streak int // failures in a row
52+
benchFor time.Duration // the next bench's length; 0 is poolBenchFor
4453
until time.Time
4554
trusted bool // passed its last canary check
4655
sinceCanary int // lookups since the last canary check
@@ -364,19 +373,32 @@ func (p *Pool) report(m *PoolResolver, o Outcome) {
364373
m.window++
365374
m.sinceCanary++
366375
switch o {
367-
case OutcomeFound:
368-
m.stats.Found++
369-
case OutcomeNXDomain:
370-
m.stats.NXDomain++
376+
case OutcomeFound, OutcomeNXDomain:
377+
if o == OutcomeFound {
378+
m.stats.Found++
379+
} else {
380+
m.stats.NXDomain++
381+
}
382+
// It answers: the next bench, if any, starts short again.
383+
m.streak, m.benchFor = 0, 0
371384
default:
372385
m.stats.Failed++
373386
m.windowKO++
387+
m.streak++
388+
}
389+
now := p.now()
390+
if now.Before(m.until) {
391+
return // benched already; lookups in flight report late
392+
}
393+
if m.streak >= poolBenchStreak || m.window >= poolHealthMinLookups && m.windowKO*100 > m.window*poolHealthFailPct {
394+
d := cmp.Or(m.benchFor, poolBenchFor)
395+
m.until = now.Add(d)
396+
m.benchFor = min(2*d, poolBenchMax)
397+
m.stats.Benched++
398+
m.window, m.windowKO, m.streak = 0, 0, 0
399+
return
374400
}
375401
if m.window >= poolHealthMinLookups {
376-
if m.windowKO*100 > m.window*poolHealthFailPct {
377-
m.until = p.now().Add(poolBenchFor)
378-
m.stats.Benched++
379-
}
380402
m.window, m.windowKO = 0, 0
381403
}
382404
}
@@ -410,7 +432,8 @@ func (p *Pool) Disagree(a PoolAnswer, trusted Outcome) {
410432

411433
// checkCanary asks m about a known-existing name when a check is due: always
412434
// while m is untrusted, else every poolCanaryEvery lookups. An NXDOMAIN
413-
// answer is a contradiction. Failed checks change nothing.
435+
// answer is a contradiction. A check that fails counts toward benching m,
436+
// like a failed lookup (#463).
414437
func (p *Pool) checkCanary(ctx context.Context, m *PoolResolver, l Lookup) {
415438
m.mu.Lock()
416439
due := !m.canaryBusy && (!m.trusted || m.sinceCanary >= poolCanaryEvery)
@@ -439,8 +462,12 @@ func (p *Pool) checkCanary(ctx context.Context, m *PoolResolver, l Lookup) {
439462
m.trusted = o == OutcomeFound
440463
}
441464
m.mu.Unlock()
442-
if o == OutcomeNXDomain {
465+
switch o {
466+
case OutcomeNXDomain:
443467
p.contradict(m)
468+
case OutcomeFound, OutcomeCanceled:
469+
default:
470+
p.report(m, o)
444471
}
445472
}
446473

‎internal/dns/pool_test.go‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"strings"
77
"testing"
88
"time"
9+
10+
"github.com/TMHSDigital/subenum/internal/dnstest"
911
)
1012

1113
func TestParseResolverList(t *testing.T) {
@@ -321,3 +323,84 @@ func TestPoolPickPrefersAvoidedHealthyOverBenched(t *testing.T) {
321323
t.Fatalf("all benched: pick = %s, want the soonest-unbenched member", got.Addr)
322324
}
323325
}
326+
327+
// TestPoolBenchesDeadMemberFast covers #463: a member that fails a few
328+
// lookups in a row is benched without waiting for a window of 20, each
329+
// repeat bench lasts twice the one before up to a cap, and a member that
330+
// answers again starts over at the shortest bench.
331+
func TestPoolBenchesDeadMemberFast(t *testing.T) {
332+
p := NewPool([]string{"192.0.2.1:53", "192.0.2.2:53"}, time.Second)
333+
now := time.Unix(0, 0)
334+
p.now = func() time.Time { return now }
335+
dead := p.members[0]
336+
benchedFor := func() time.Duration {
337+
dead.mu.Lock()
338+
defer dead.mu.Unlock()
339+
return dead.until.Sub(now)
340+
}
341+
failStreak := func() {
342+
for range poolBenchStreak {
343+
p.report(dead, OutcomeTimeout)
344+
}
345+
}
346+
347+
failStreak()
348+
if st := p.Stats()[0]; st.Benched != 1 || benchedFor() != poolBenchFor {
349+
t.Fatalf("after %d timeouts: benched %d for %v, want once for %v", poolBenchStreak, st.Benched, benchedFor(), poolBenchFor)
350+
}
351+
// Lookups still in flight when it was benched do not bench it again.
352+
p.report(dead, OutcomeTimeout)
353+
if st := p.Stats()[0]; st.Benched != 1 {
354+
t.Fatalf("a late failure re-benched it: %d benches", st.Benched)
355+
}
356+
357+
want := poolBenchFor
358+
for range 5 {
359+
now = now.Add(benchedFor() + time.Second)
360+
failStreak()
361+
want = min(2*want, poolBenchMax)
362+
if got := benchedFor(); got != want {
363+
t.Fatalf("repeat bench lasts %v, want %v", got, want)
364+
}
365+
}
366+
if st := p.Stats()[0]; st.Benched != 6 {
367+
t.Errorf("benched %d times, want 6", st.Benched)
368+
}
369+
370+
// An answer resets the backoff.
371+
now = now.Add(benchedFor() + time.Second)
372+
p.report(dead, OutcomeNXDomain)
373+
failStreak()
374+
if got := benchedFor(); got != poolBenchFor {
375+
t.Errorf("bench after an answer lasts %v, want %v again", got, poolBenchFor)
376+
}
377+
378+
// Failures that are not in a row still bench through the window.
379+
other := p.members[1]
380+
for i := range poolHealthMinLookups {
381+
o := OutcomeTimeout
382+
if i%3 == 2 {
383+
o = OutcomeFound
384+
}
385+
p.report(other, o)
386+
}
387+
if st := p.Stats()[1]; st.Benched != 1 {
388+
t.Errorf("a member failing two lookups in three: benched %d times, want 1 by the window", st.Benched)
389+
}
390+
}
391+
392+
// TestPoolFailedCanaryCounts covers #463: a member that never answers its
393+
// canary checks is benched by them, since each one fails like a lookup.
394+
func TestPoolFailedCanaryCounts(t *testing.T) {
395+
srv := dnstest.Start(t, func(dnstest.Query) dnstest.Reply { return dnstest.Dropped })
396+
p := NewPool([]string{srv.Addr}, 50*time.Millisecond)
397+
p.AddCanary("www.example.com")
398+
l := Lookup{Timeout: 50 * time.Millisecond, Types: []string{"A"}}
399+
m := p.members[0]
400+
for range poolBenchStreak {
401+
p.checkCanary(context.Background(), m, l)
402+
}
403+
if st := p.Stats()[0]; st.Benched != 1 || st.Failed != poolBenchStreak {
404+
t.Errorf("after %d failed canaries: benched %d, failed %d; want 1 and %d", poolBenchStreak, st.Benched, st.Failed, poolBenchStreak)
405+
}
406+
}

0 commit comments

Comments
 (0)