Skip to content
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

feat: speed up generating answers #1262

Merged
merged 1 commit into from
Sep 10, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def build(setup_kwargs: Any) -> None:
"src/zeroconf/_listener.py",
"src/zeroconf/_protocol/incoming.py",
"src/zeroconf/_protocol/outgoing.py",
"src/zeroconf/_handlers/answers.py",
"src/zeroconf/_handlers/record_manager.py",
"src/zeroconf/_handlers/query_handler.py",
"src/zeroconf/_services/registry.py",
Expand Down
16 changes: 16 additions & 0 deletions src/zeroconf/_handlers/answers.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

import cython

from .._protocol.outgoing cimport DNSOutgoing


cdef object _FLAGS_QR_RESPONSE_AA
cdef object NAME_GETTER

cpdef construct_outgoing_multicast_answers(cython.dict answers)

cpdef construct_outgoing_unicast_answers(
cython.dict answers, object ucast_source, cython.list questions, object id_
)

cdef _add_answers_additionals(DNSOutgoing out, cython.dict answers)
10 changes: 7 additions & 3 deletions src/zeroconf/_handlers/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@

_AnswerWithAdditionalsType = Dict[DNSRecord, Set[DNSRecord]]

int_ = int


MULTICAST_DELAY_RANDOM_INTERVAL = (20, 120)

NAME_GETTER = attrgetter('name')

_FLAGS_QR_RESPONSE_AA = _FLAGS_QR_RESPONSE | _FLAGS_AA


class QuestionAnswers(NamedTuple):
ucast: _AnswerWithAdditionalsType
Expand All @@ -52,16 +56,16 @@ class AnswerGroup(NamedTuple):

def construct_outgoing_multicast_answers(answers: _AnswerWithAdditionalsType) -> DNSOutgoing:
"""Add answers and additionals to a DNSOutgoing."""
out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, multicast=True)
out = DNSOutgoing(_FLAGS_QR_RESPONSE_AA, True)
_add_answers_additionals(out, answers)
return out


def construct_outgoing_unicast_answers(
answers: _AnswerWithAdditionalsType, ucast_source: bool, questions: List[DNSQuestion], id_: int
answers: _AnswerWithAdditionalsType, ucast_source: bool, questions: List[DNSQuestion], id_: int_
) -> DNSOutgoing:
"""Add answers and additionals to a DNSOutgoing."""
out = DNSOutgoing(_FLAGS_QR_RESPONSE | _FLAGS_AA, multicast=False, id_=id_)
out = DNSOutgoing(_FLAGS_QR_RESPONSE_AA, False, id_)
# Adding the questions back when the source is legacy unicast behavior
if ucast_source:
for question in questions:
Expand Down