From 4f4bd9ff7c1e575046e5ea213d9b8c91ac7a24a9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 14 Oct 2023 09:29:50 -1000 Subject: [PATCH] feat: small cleanups to incoming data handlers (#1282) --- src/zeroconf/_dns.pxd | 2 +- src/zeroconf/_handlers/query_handler.pxd | 4 ++-- src/zeroconf/_handlers/query_handler.py | 6 ++++-- src/zeroconf/_handlers/record_manager.pxd | 13 ++++++++++--- src/zeroconf/_handlers/record_manager.py | 9 ++++----- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/zeroconf/_dns.pxd b/src/zeroconf/_dns.pxd index ccdcc34f..6785d1a3 100644 --- a/src/zeroconf/_dns.pxd +++ b/src/zeroconf/_dns.pxd @@ -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 diff --git a/src/zeroconf/_handlers/query_handler.pxd b/src/zeroconf/_handlers/query_handler.pxd index a1a4f8a6..ff970d76 100644 --- a/src/zeroconf/_handlers/query_handler.pxd +++ b/src/zeroconf/_handlers/query_handler.pxd @@ -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: @@ -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) diff --git a/src/zeroconf/_handlers/query_handler.py b/src/zeroconf/_handlers/query_handler.py index 776d6a3f..cab11662 100644 --- a/src/zeroconf/_handlers/query_handler.py +++ b/src/zeroconf/_handlers/query_handler.py @@ -47,6 +47,8 @@ _RESPOND_IMMEDIATE_TYPES = {_TYPE_NSEC, _TYPE_SRV, *_ADDRESS_RECORD_TYPES} +_IPVersion_ALL = IPVersion.All + _int = int @@ -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) @@ -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 diff --git a/src/zeroconf/_handlers/record_manager.pxd b/src/zeroconf/_handlers/record_manager.pxd index 89ad5484..8775108b 100644 --- a/src/zeroconf/_handlers/record_manager.pxd +++ b/src/zeroconf/_handlers/record_manager.pxd @@ -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 @@ -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) diff --git a/src/zeroconf/_handlers/record_manager.py b/src/zeroconf/_handlers/record_manager.py index 63572c1e..6fb11f55 100644 --- a/src/zeroconf/_handlers/record_manager.py +++ b/src/zeroconf/_handlers/record_manager.py @@ -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: @@ -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) @@ -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 @@ -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(