Skip to content

Commit d325abd

Browse files
committed
Count a capped expand's children as stopAdmitting counts its remainder (#393)
A recursive scan that has reached -max-queries counted every child of a parent found after the cap as skipped: expand's capped branch added all n children to skipped without checking -exclude or the visited set, so excluded children were misreported as skipped and, with multi-label entries, children already reached from another parent were counted twice. #361 made stopAdmitting count the queued remainder exactly; a late parent now goes through the same split by queueing a temporary expansion and stopping admission again, so excluded children are excluded and visited ones are not counted twice.
1 parent 43579aa commit d325abd

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎internal/scan/dispatcher.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,14 @@ func (d *dispatcher) expand(parent job) {
465465
case d.cfg.scope.subtreeExcluded(parent.domain):
466466
d.excluded += d.n
467467
case d.capped:
468-
d.skipped += d.n
468+
// A parent found after the budget stopped admission: queue it and
469+
// stop admission again, so its children are counted as the queued
470+
// remainder is (#393): children -exclude rules out are excluded,
471+
// not skipped, and children the visited set would drop are not
472+
// counted twice.
473+
d.frontier = append(d.frontier, expansion{parent: parent.domain, depth: parent.depth + 1})
474+
d.remaining += d.n
475+
d.stopAdmitting()
469476
default:
470477
d.frontier = append(d.frontier, expansion{parent: parent.domain, depth: parent.depth + 1})
471478
d.remaining += d.n

‎internal/scan/dispatcher_test.go‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,3 +532,57 @@ func TestCappedExpandOfExcludedSubtree(t *testing.T) {
532532
t.Errorf("excluded %d skipped %d, want the 5 children excluded and skipped unchanged at %d", d.excluded, d.skipped, skipped)
533533
}
534534
}
535+
536+
// TestCappedExpandCountsExcludedChildren covers #393: a parent of a recursive
537+
// scan that is found after a budget stopped admission has the children -exclude
538+
// rules out counted as excluded, not skipped, as stopAdmitting counts them.
539+
func TestCappedExpandCountsExcludedChildren(t *testing.T) {
540+
entries := makeEntries(10)
541+
d := testDispatcher(Config{Entries: entries, Recursive: true, MaxQueries: 5,
542+
Exclude: []string{entries[9] + ".p0.example.com"}}, 2)
543+
if got := len(drain(d)); got != 5 {
544+
t.Fatalf("admitted %d, want 5", got)
545+
}
546+
if d.skipped != 5 || d.excluded != 0 {
547+
t.Fatalf("before the late expand: skipped %d excluded %d, want 5 and 0", d.skipped, d.excluded)
548+
}
549+
d.expand(job{domain: "p0.example.com", depth: 1})
550+
// p0's ten children: entries[9].p0 is excluded, the other nine are skipped.
551+
if d.excluded != 1 || d.skipped != 14 || d.admitted+d.skipped+d.excluded != 20 {
552+
t.Errorf("after the late expand: admitted %d skipped %d excluded %d, want 5, 14 and 1 (20 candidates)",
553+
d.admitted, d.skipped, d.excluded)
554+
}
555+
}
556+
557+
// TestCappedExpandCountsRecursiveRepeatsOnce covers #393: a parent of a
558+
// recursive scan found after a budget stopped admission does not have the
559+
// children another parent already reached counted again, so admitted, skipped
560+
// and excluded still add up to the candidates the scan with no budget
561+
// generates, and an excluded child is excluded whichever parent reaches it.
562+
func TestCappedExpandCountsRecursiveRepeatsOnce(t *testing.T) {
563+
for _, exclude := range [][]string{nil, {"x.a.b.example.com"}} {
564+
cfg := Config{Entries: []string{"x", "x.a", "a.b", "b", "y"}, Recursive: true, MaxQueries: 4, Exclude: exclude}
565+
// The same scan with no budget, expanding both parents.
566+
full := cfg
567+
full.MaxQueries = 0
568+
all := testDispatcher(full, 3)
569+
roots := len(drain(all))
570+
all.expand(job{domain: "b.example.com", depth: 1})
571+
all.expand(job{domain: "a.b.example.com", depth: 1})
572+
candidates := roots + len(drain(all)) + all.excluded
573+
574+
d := testDispatcher(cfg, 3)
575+
admitted := len(drain(d))
576+
// Both parents resolve after the cap: b first, a.b second, so
577+
// x.a.b.example.com is reached from both and counted once.
578+
d.expand(job{domain: "b.example.com", depth: 1})
579+
d.expand(job{domain: "a.b.example.com", depth: 1})
580+
if admitted != 4 || !d.capped {
581+
t.Fatalf("%v: admitted %d capped %v, want 4 and true", exclude, admitted, d.capped)
582+
}
583+
if d.skipped != candidates-admitted-all.excluded || d.excluded != all.excluded || admitted+d.skipped+d.excluded != candidates {
584+
t.Errorf("%v: skipped %d excluded %d, want %d and %d (of %d candidates)",
585+
exclude, d.skipped, d.excluded, candidates-admitted-all.excluded, all.excluded, candidates)
586+
}
587+
}
588+
}

0 commit comments

Comments
 (0)