Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/zeroconf/_services/browser.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cdef class _DNSPointerOutgoingBucket:


@cython.locals(answer=DNSPointer)
cpdef _group_ptr_queries_with_known_answers(object now, object multicast, cython.dict question_with_known_answers)
cdef _group_ptr_queries_with_known_answers(object now, object multicast, cython.dict question_with_known_answers)

cdef class QueryScheduler:

Expand Down
29 changes: 21 additions & 8 deletions src/zeroconf/_services/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import random
import threading
import warnings
from functools import partial
from types import TracebackType # noqa # used in type hints
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -111,7 +112,7 @@ def add(self, max_compressed_size: int_, question: DNSQuestion, answers: Set[DNS
self.bytes += max_compressed_size


def _group_ptr_queries_with_known_answers(
def group_ptr_queries_with_known_answers(
now: float_, multicast: bool_, question_with_known_answers: _QuestionWithKnownAnswers
) -> List[DNSOutgoing]:
"""Aggregate queries so that as many known answers as possible fit in the same packet
Expand All @@ -122,6 +123,13 @@ def _group_ptr_queries_with_known_answers(
so we try to keep all the known answers in the same packet as the
questions.
"""
return _group_ptr_queries_with_known_answers(now, multicast, question_with_known_answers)


def _group_ptr_queries_with_known_answers(
now: float_, multicast: bool_, question_with_known_answers: _QuestionWithKnownAnswers
) -> List[DNSOutgoing]:
"""Inner wrapper for group_ptr_queries_with_known_answers."""
# This is the maximum size the query + known answers can be with name compression.
# The actual size of the query + known answers may be a bit smaller since other
# parts may be shared when the final DNSOutgoing packets are constructed. The
Expand Down Expand Up @@ -187,6 +195,17 @@ def generate_service_query(
return _group_ptr_queries_with_known_answers(now, multicast, questions_with_known_answers)


def _on_change_dispatcher(
listener: ServiceListener,
zeroconf: 'Zeroconf',
service_type: str,
name: str,
state_change: ServiceStateChange,
) -> None:
"""Dispatch a service state change to a listener."""
getattr(listener, _ON_CHANGE_DISPATCH[state_change])(zeroconf, service_type, name)


def _service_state_changed_from_listener(listener: ServiceListener) -> Callable[..., None]:
"""Generate a service_state_changed handlers from a listener."""
assert listener is not None
Expand All @@ -196,13 +215,7 @@ def _service_state_changed_from_listener(listener: ServiceListener) -> Callable[
"don't care about the updates), it'll become mandatory." % (listener,),
FutureWarning,
)

def on_change(
zeroconf: 'Zeroconf', service_type: str, name: str, state_change: ServiceStateChange
) -> None:
getattr(listener, _ON_CHANGE_DISPATCH[state_change])(zeroconf, service_type, name)

return on_change
return partial(_on_change_dispatcher, listener)


class QueryScheduler:
Expand Down
2 changes: 1 addition & 1 deletion tests/services/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ def test_group_ptr_queries_with_known_answers():
)
for counter in range(i)
}
outs = _services_browser._group_ptr_queries_with_known_answers(now, True, questions_with_known_answers)
outs = _services_browser.group_ptr_queries_with_known_answers(now, True, questions_with_known_answers)
for out in outs:
packets = out.packets()
# If we generate multiple packets there must
Expand Down