Skip to content

Commit

Permalink
feat: small cleanups to incoming data handlers (#1282)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Oct 14, 2023
1 parent 68be89c commit 4f4bd9f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/zeroconf/_dns.pxd
Expand Up @@ -24,7 +24,7 @@ cdef class DNSEntry:

cdef public str key
cdef public str name
cdef public object type
cdef public cython.uint type
cdef public object class_
cdef public object unique

Expand Down
4 changes: 2 additions & 2 deletions src/zeroconf/_handlers/query_handler.pxd
Expand Up @@ -15,7 +15,7 @@ cdef cython.uint _ONE_SECOND, _TYPE_PTR, _TYPE_ANY, _TYPE_A, _TYPE_AAAA, _TYPE_S
cdef str _SERVICE_TYPE_ENUMERATION_NAME
cdef cython.set _RESPOND_IMMEDIATE_TYPES
cdef cython.set _ADDRESS_RECORD_TYPES
cdef object IPVersion
cdef object IPVersion, _IPVersion_ALL
cdef object _TYPE_PTR, _CLASS_IN, _DNS_OTHER_TTL

cdef class _QueryResponse:
Expand Down Expand Up @@ -59,7 +59,7 @@ cdef class QueryHandler:
cdef _add_pointer_answers(self, str lower_name, cython.dict answer_set, DNSRRSet known_answers)

@cython.locals(service=ServiceInfo, dns_address=DNSAddress)
cdef _add_address_answers(self, str lower_name, cython.dict answer_set, DNSRRSet known_answers, object type_)
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 cython.dict _answer_question(self, DNSQuestion question, DNSRRSet known_answers)
Expand Down
6 changes: 4 additions & 2 deletions src/zeroconf/_handlers/query_handler.py
Expand Up @@ -47,6 +47,8 @@

_RESPOND_IMMEDIATE_TYPES = {_TYPE_NSEC, _TYPE_SRV, *_ADDRESS_RECORD_TYPES}

_IPVersion_ALL = IPVersion.All

_int = int


Expand Down Expand Up @@ -202,7 +204,7 @@ def _add_address_answers(
answers: List[DNSAddress] = []
additionals: Set[DNSRecord] = set()
seen_types: Set[int] = set()
for dns_address in service._dns_addresses(None, IPVersion.All):
for dns_address in service._dns_addresses(None, _IPVersion_ALL):
seen_types.add(dns_address.type)
if dns_address.type != type_:
additionals.add(dns_address)
Expand Down Expand Up @@ -269,7 +271,7 @@ def async_response( # pylint: disable=unused-argument
questions = msg.questions
now = msg.now
for msg in msgs:
if not msg.is_probe():
if msg.is_probe() is False:
answers.extend(msg.answers())
else:
is_probe = True
Expand Down
13 changes: 10 additions & 3 deletions src/zeroconf/_handlers/record_manager.pxd
Expand Up @@ -2,11 +2,14 @@
import cython

from .._cache cimport DNSCache
from .._dns cimport DNSRecord
from .._dns cimport DNSQuestion, DNSRecord
from .._protocol.incoming cimport DNSIncoming
from .._updates cimport RecordUpdateListener
from .._utils.time cimport current_time_millis


cdef cython.float _DNS_PTR_MIN_TTL
cdef cython.uint _TYPE_PTR
cdef object _ADDRESS_RECORD_TYPES
cdef object RecordUpdate
cdef bint TYPE_CHECKING
Expand All @@ -26,11 +29,15 @@ cdef class RecordManager:
@cython.locals(
cache=DNSCache,
record=DNSRecord,
answers=cython.list,
maybe_entry=DNSRecord,
now_float=cython.float
)
cpdef async_updates_from_response(self, DNSIncoming msg)

cpdef async_add_listener(self, object listener, object question)
cpdef async_add_listener(self, RecordUpdateListener listener, object question)

cpdef async_remove_listener(self, object listener)
cpdef async_remove_listener(self, RecordUpdateListener listener)

@cython.locals(question=DNSQuestion, record=DNSRecord)
cdef _async_update_matching_records(self, RecordUpdateListener listener, cython.list questions)
9 changes: 4 additions & 5 deletions src/zeroconf/_handlers/record_manager.py
Expand Up @@ -106,14 +106,14 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:
)
record.set_created_ttl(record.created, _DNS_PTR_MIN_TTL)

if record.unique: # https://tools.ietf.org/html/rfc6762#section-10.2
if record.unique is True: # https://tools.ietf.org/html/rfc6762#section-10.2
unique_types.add((record.name, record_type, record.class_))

if TYPE_CHECKING:
record = cast(_UniqueRecordsType, record)

maybe_entry = cache.async_get_unique(record)
if not record.is_expired(now_float):
if record.is_expired(now_float) is False:
if maybe_entry is not None:
maybe_entry.reset_ttl(record)
else:
Expand All @@ -129,7 +129,7 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:
removes.add(record)

if unique_types:
cache.async_mark_unique_records_older_than_1s_to_expire(unique_types, answers, now)
cache.async_mark_unique_records_older_than_1s_to_expire(unique_types, answers, now_float)

if updates:
self.async_updates(now, updates)
Expand All @@ -151,7 +151,7 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:
new = False
if other_adds or address_adds:
new = cache.async_add_records(address_adds)
if cache.async_add_records(other_adds):
if cache.async_add_records(other_adds) is True:
new = True
# Removes are processed last since
# ServiceInfo could generate an un-needed query
Expand Down Expand Up @@ -182,7 +182,6 @@ def async_add_listener(
return

questions = [question] if isinstance(question, DNSQuestion) else question
assert self.zc.loop is not None
self._async_update_matching_records(listener, questions)

def _async_update_matching_records(
Expand Down

0 comments on commit 4f4bd9f

Please sign in to comment.