-
Notifications
You must be signed in to change notification settings - Fork 228
fix: bound DNSCache record count to prevent unbounded LAN-driven growth #1718
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
+374
−31
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c24fa39
fix: bound DNSCache record count to prevent unbounded LAN-driven growth
bdraco 51fbc27
test: cover stale-heap-entry branch in _async_evict_oldest
bdraco 931358a
fix: re-resolve store after eviction and rebuild stale-prefix heap
bdraco 7c6be63
fix: bound _expire_heap growth from re-adds and DRY test helpers
bdraco 052b1e2
test: cover empty-heap exit branch in _async_evict_oldest
bdraco a2a2d34
test: pin counter drift, restructure at-cap bench, trim docstring
bdraco 38b1517
test: pin DNSNsec flood is bounded by _MAX_CACHE_RECORDS
bdraco e5b509a
test: DNSNsec at-cap eviction + unbounded benchmark pool
bdraco 39ab755
chore: drop stale comment from _async_evict_oldest fall-through
bdraco 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
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
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
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,68 @@ | ||
| """Benchmark for the DNSCache record-count bound + overflow eviction.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterator | ||
| from itertools import count | ||
|
|
||
| from pytest_codspeed import BenchmarkFixture | ||
|
|
||
| from zeroconf import DNSAddress, DNSCache, current_time_millis | ||
| from zeroconf.const import _CLASS_IN, _MAX_CACHE_RECORDS, _TYPE_A | ||
|
|
||
|
|
||
| def _make_records(count_: int, now: float, prefix: str = "bench") -> list[DNSAddress]: | ||
| return [ | ||
| DNSAddress( | ||
| f"{prefix}-{i}.local.", | ||
| _TYPE_A, | ||
| _CLASS_IN, | ||
| 120, | ||
| bytes(((i >> 24) & 0xFF, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF)), | ||
| created=now + i, | ||
| ) | ||
| for i in range(count_) | ||
| ] | ||
|
|
||
|
|
||
| def _unbounded_records(now: float, prefix: str = "evict") -> Iterator[DNSAddress]: | ||
| """Unbounded generator of unique-name DNSAddress records.""" | ||
| for i in count(): | ||
| yield DNSAddress( | ||
| f"{prefix}-{i}.local.", | ||
| _TYPE_A, | ||
| _CLASS_IN, | ||
| 120, | ||
| bytes(((i >> 24) & 0xFF, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF)), | ||
| created=now + i, | ||
| ) | ||
|
|
||
|
|
||
| def test_cache_add_below_cap(benchmark: BenchmarkFixture) -> None: | ||
| """Adding records while the cache is well below the cap (no eviction).""" | ||
| now = current_time_millis() | ||
| records = _make_records(1000, now) | ||
|
|
||
| @benchmark | ||
| def _add() -> None: | ||
| cache = DNSCache() | ||
| cache.async_add_records(records) | ||
|
|
||
|
|
||
| def test_cache_add_at_cap_evicts(benchmark: BenchmarkFixture) -> None: | ||
| """Steady-state add at the cap: every measured insert forces one eviction. | ||
|
|
||
| Pre-fills the cache to ``_MAX_CACHE_RECORDS`` outside the timed body so | ||
| only the eviction-path adds are measured. Each benchmark iteration | ||
| pulls one fresh unique record from an unbounded generator, keeping the | ||
| cache permanently at the cap. The generator avoids the iteration-count | ||
| cap that a pre-built pool would impose for very fast operations. | ||
| """ | ||
| now = current_time_millis() | ||
| cache = DNSCache() | ||
| cache.async_add_records(_make_records(_MAX_CACHE_RECORDS, now, prefix="fill")) | ||
| pool = _unbounded_records(now + _MAX_CACHE_RECORDS) | ||
|
|
||
| @benchmark | ||
| def _evict_one() -> None: | ||
| cache.async_add_records([next(pool)]) |
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.