Skip to content

Commit 27e50ff

Browse files
authored
Improve performance of query scheduler (#1043)
1 parent 95ee5dc commit 27e50ff

3 files changed

Lines changed: 16 additions & 17 deletions

File tree

tests/test_asyncio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
923923
# Increase simulated time shift by 1/4 of the TTL in seconds
924924
time_offset += expected_ttl / 4
925925
now = _new_current_time_millis()
926-
browser.reschedule_type(type_, now)
926+
browser.reschedule_type(type_, now, now)
927927
sleep_count += 1
928928
await asyncio.wait_for(got_query.wait(), 1)
929929
got_query.clear()

zeroconf/_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def _answer_question(
326326
self._add_address_answers(question.name, answer_set, known_answers, now, type_)
327327

328328
if type_ in (_TYPE_SRV, _TYPE_TXT, _TYPE_ANY):
329-
service = self.registry.async_get_info_name(question.name) # type: ignore
329+
service = self.registry.async_get_info_name(question.name)
330330
if service is not None:
331331
if type_ in (_TYPE_SRV, _TYPE_ANY):
332332
# Add recommended additional answers according to

zeroconf/_services/browser.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def _async_process_record_update(
363363
elif record.is_expired(now):
364364
self._enqueue_callback(ServiceStateChange.Removed, type_, name)
365365
else:
366-
self.reschedule_type(type_, record.get_expiration_time(_EXPIRE_REFRESH_TIME_PERCENT))
366+
self.reschedule_type(type_, now, record.get_expiration_time(_EXPIRE_REFRESH_TIME_PERCENT))
367367
return
368368

369369
# If its expired or already exists in the cache it cannot be updated.
@@ -431,9 +431,8 @@ def _async_cancel(self) -> None:
431431
self._cancel_send_timer()
432432
self.zc.async_remove_listener(self)
433433

434-
def _generate_ready_queries(self, first_request: bool) -> List[DNSOutgoing]:
434+
def _generate_ready_queries(self, first_request: bool, now: float) -> List[DNSOutgoing]:
435435
"""Generate the service browser query for any type that is due."""
436-
now = current_time_millis()
437436
ready_types = self.query_scheduler.process_ready_types(now)
438437
if not ready_types:
439438
return []
@@ -448,40 +447,40 @@ def _generate_ready_queries(self, first_request: bool) -> List[DNSOutgoing]:
448447
async def _async_start_query_sender(self) -> None:
449448
"""Start scheduling queries."""
450449
await self.zc.async_wait_for_start()
451-
self._async_send_ready_queries()
452-
self._async_schedule_next()
450+
self._async_send_ready_queries_schedule_next()
453451

454452
def _cancel_send_timer(self) -> None:
455453
"""Cancel the next send."""
456454
if self._next_send_timer:
457455
self._next_send_timer.cancel()
458456

459-
def reschedule_type(self, type_: str, next_time: float) -> None:
457+
def reschedule_type(self, type_: str, now: float, next_time: float) -> None:
460458
"""Reschedule a type to be refreshed in the future."""
461459
if self.query_scheduler.reschedule_type(type_, next_time):
462460
self._cancel_send_timer()
463-
self._async_schedule_next()
464-
self._async_send_ready_queries()
461+
self._async_schedule_next(now)
462+
self._async_send_ready_queries(now)
465463

466-
def _async_send_ready_queries(self) -> None:
464+
def _async_send_ready_queries(self, now: float) -> None:
467465
"""Send any ready queries."""
468-
outs = self._generate_ready_queries(self._first_request)
466+
outs = self._generate_ready_queries(self._first_request, now)
469467
if outs:
470468
self._first_request = False
471469
for out in outs:
472470
self.zc.async_send(out, addr=self.addr, port=self.port)
473471

474472
def _async_send_ready_queries_schedule_next(self) -> None:
475-
"""Send ready queries and schedule next one."""
473+
"""Send ready queries and schedule next one checking for done first."""
476474
if self.done or self.zc.done:
477475
return
478-
self._async_send_ready_queries()
479-
self._async_schedule_next()
476+
now = current_time_millis()
477+
self._async_send_ready_queries(now)
478+
self._async_schedule_next(now)
480479

481-
def _async_schedule_next(self) -> None:
480+
def _async_schedule_next(self, now: float) -> None:
482481
"""Scheule the next time."""
483482
assert self.zc.loop is not None
484-
delay = millis_to_seconds(self.query_scheduler.millis_to_wait(current_time_millis()))
483+
delay = millis_to_seconds(self.query_scheduler.millis_to_wait(now))
485484
self._next_send_timer = self.zc.loop.call_later(delay, self._async_send_ready_queries_schedule_next)
486485

487486

0 commit comments

Comments
 (0)