Skip to content

Commit 1bff201

Browse files
committed
fix: avoid querying to refresh types in the ServiceBrowser when they are seen after scheduling
The ServiceBrowser query scheduling was sub-optimial because it would reschedule to query for a type when 75% of the ttl was expired, but if the type was seen before the timer expired we would send the query anyways which created more network traffic fixes #821
1 parent 963d022 commit 1bff201

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎src/zeroconf/_services/browser.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
cast,
4040
)
4141

42+
from .._cache import DNSCache
4243
from .._dns import DNSPointer, DNSQuestion, DNSQuestionType, DNSRecord
4344
from .._logger import log
4445
from .._protocol.outgoing import DNSOutgoing
@@ -198,6 +199,7 @@ class QueryScheduler:
198199

199200
def __init__(
200201
self,
202+
cache: DNSCache,
201203
types: Set[str],
202204
delay: int,
203205
first_random_delay_interval: Tuple[int, int],
@@ -207,6 +209,7 @@ def __init__(
207209
self._next_time: Dict[str, float] = {}
208210
self._first_random_delay_interval = first_random_delay_interval
209211
self._delay: Dict[str, float] = {check_type_: delay for check_type_ in self._types}
212+
self._cache = cache
210213

211214
def start(self, now: float) -> None:
212215
"""Start the scheduler."""
@@ -251,6 +254,18 @@ def process_ready_types(self, now: float) -> List[str]:
251254
if due > now:
252255
continue
253256

257+
# If there are no stale PTR records because the record came in after
258+
# we scheduled the query, we can reschedule the query to happen later.
259+
next_refresh_time_by_record = [
260+
record.get_expiration_time(_EXPIRE_REFRESH_TIME_PERCENT)
261+
for record in self._cache.get_all_by_details(type_, _TYPE_PTR, _CLASS_IN)
262+
]
263+
if next_refresh_time_by_record:
264+
recalculated_next_time = min(next_refresh_time_by_record)
265+
if recalculated_next_time > now:
266+
self._next_time[type_] = recalculated_next_time
267+
continue
268+
254269
ready_types.append(type_)
255270
self._next_time[type_] = now + self._delay[type_]
256271
self._delay[type_] = min(_BROWSER_BACKOFF_LIMIT * 1000, self._delay[type_] * 2)
@@ -301,7 +316,7 @@ def __init__(
301316
self.question_type = question_type
302317
self._pending_handlers: Dict[Tuple[str, str], ServiceStateChange] = {}
303318
self._service_state_changed = Signal()
304-
self.query_scheduler = QueryScheduler(self.types, delay, _FIRST_QUERY_DELAY_RANDOM_INTERVAL)
319+
self.query_scheduler = QueryScheduler(zc.cache, self.types, delay, _FIRST_QUERY_DELAY_RANDOM_INTERVAL)
305320
self.done = False
306321
self._first_request: bool = True
307322
self._next_send_timer: Optional[asyncio.TimerHandle] = None

‎tests/services/test_browser.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
current_time_millis,
2727
millis_to_seconds,
2828
)
29+
from zeroconf._cache import DNSCache
2930
from zeroconf._services import ServiceStateChange
3031
from zeroconf._services.browser import ServiceBrowser
3132
from zeroconf._services.info import ServiceInfo
@@ -993,7 +994,8 @@ async def test_generate_service_query_suppress_duplicate_questions():
993994
async def test_query_scheduler():
994995
delay = const._BROWSER_TIME
995996
types_ = {"_hap._tcp.local.", "_http._tcp.local."}
996-
query_scheduler = _services_browser.QueryScheduler(types_, delay, (0, 0))
997+
cache = DNSCache()
998+
query_scheduler = _services_browser.QueryScheduler(cache, types_, delay, (0, 0))
997999

9981000
now = current_time_millis()
9991001
query_scheduler.start(now)

0 commit comments

Comments
 (0)