Skip to content

Commit

Permalink
feat: speed up responding to queries (#1266)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Sep 11, 2023
1 parent f38cf55 commit 24a0a00
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/zeroconf/_dns.pxd
Expand Up @@ -136,3 +136,5 @@ cdef class DNSRRSet:
record_sets=cython.list,
)
cdef cython.dict _get_lookup(self)

cpdef cython.set lookup_set(self)
6 changes: 5 additions & 1 deletion src/zeroconf/_dns.py
Expand Up @@ -22,7 +22,7 @@

import enum
import socket
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union, cast
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Union, cast

from ._exceptions import AbstractMethodException
from ._utils.net import _is_v6_address
Expand Down Expand Up @@ -533,6 +533,10 @@ def lookup(self) -> Dict[DNSRecord, float]:
"""Return the lookup table."""
return self._get_lookup()

def lookup_set(self) -> Set[DNSRecord]:
"""Return the lookup table as aset."""
return set(self._get_lookup())

def _get_lookup(self) -> Dict[DNSRecord, float]:
"""Return the lookup table, building it if needed."""
if self._lookup is None:
Expand Down
4 changes: 2 additions & 2 deletions src/zeroconf/_handlers/query_handler.pxd
Expand Up @@ -59,7 +59,7 @@ cdef class QueryHandler:
cdef _add_address_answers(self, str lower_name, cython.dict answer_set, DNSRRSet known_answers, cython.uint type_)

@cython.locals(question_lower_name=str, type_=cython.uint, service=ServiceInfo)
cdef _answer_question(self, DNSQuestion question, DNSRRSet known_answers)
cdef cython.dict _answer_question(self, DNSQuestion question, DNSRRSet known_answers)

@cython.locals(
msg=DNSIncoming,
Expand All @@ -68,4 +68,4 @@ cdef class QueryHandler:
known_answers=DNSRRSet,
known_answers_set=cython.set,
)
cpdef async_response(self, cython.list msgs, object unicast_source)
cpdef async_response(self, cython.list msgs, cython.bint unicast_source)
6 changes: 3 additions & 3 deletions src/zeroconf/_handlers/query_handler.py
Expand Up @@ -268,12 +268,12 @@ def async_response( # pylint: disable=unused-argument

for msg in msgs:
for question in msg.questions:
if not question.unicast:
if not question.unique: # unique and unicast are the same flag
if not known_answers_set: # pragma: no branch
known_answers_set = set(known_answers.lookup)
known_answers_set = known_answers.lookup_set()
self.question_history.add_question_at_time(question, msg.now, known_answers_set)
answer_set = self._answer_question(question, known_answers)
if not ucast_source and question.unicast:
if not ucast_source and question.unique: # unique and unicast are the same flag
query_res.add_qu_question_response(answer_set)
continue
if ucast_source:
Expand Down

0 comments on commit 24a0a00

Please sign in to comment.