Skip to content

Commit 4f4bd9f

Browse files
authored
feat: small cleanups to incoming data handlers (#1282)
1 parent 68be89c commit 4f4bd9f

5 files changed

Lines changed: 21 additions & 13 deletions

File tree

src/zeroconf/_dns.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ cdef class DNSEntry:
2424

2525
cdef public str key
2626
cdef public str name
27-
cdef public object type
27+
cdef public cython.uint type
2828
cdef public object class_
2929
cdef public object unique
3030

src/zeroconf/_handlers/query_handler.pxd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cdef cython.uint _ONE_SECOND, _TYPE_PTR, _TYPE_ANY, _TYPE_A, _TYPE_AAAA, _TYPE_S
1515
cdef str _SERVICE_TYPE_ENUMERATION_NAME
1616
cdef cython.set _RESPOND_IMMEDIATE_TYPES
1717
cdef cython.set _ADDRESS_RECORD_TYPES
18-
cdef object IPVersion
18+
cdef object IPVersion, _IPVersion_ALL
1919
cdef object _TYPE_PTR, _CLASS_IN, _DNS_OTHER_TTL
2020

2121
cdef class _QueryResponse:
@@ -59,7 +59,7 @@ cdef class QueryHandler:
5959
cdef _add_pointer_answers(self, str lower_name, cython.dict answer_set, DNSRRSet known_answers)
6060

6161
@cython.locals(service=ServiceInfo, dns_address=DNSAddress)
62-
cdef _add_address_answers(self, str lower_name, cython.dict answer_set, DNSRRSet known_answers, object type_)
62+
cdef _add_address_answers(self, str lower_name, cython.dict answer_set, DNSRRSet known_answers, cython.uint type_)
6363

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

src/zeroconf/_handlers/query_handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747

4848
_RESPOND_IMMEDIATE_TYPES = {_TYPE_NSEC, _TYPE_SRV, *_ADDRESS_RECORD_TYPES}
4949

50+
_IPVersion_ALL = IPVersion.All
51+
5052
_int = int
5153

5254

@@ -202,7 +204,7 @@ def _add_address_answers(
202204
answers: List[DNSAddress] = []
203205
additionals: Set[DNSRecord] = set()
204206
seen_types: Set[int] = set()
205-
for dns_address in service._dns_addresses(None, IPVersion.All):
207+
for dns_address in service._dns_addresses(None, _IPVersion_ALL):
206208
seen_types.add(dns_address.type)
207209
if dns_address.type != type_:
208210
additionals.add(dns_address)
@@ -269,7 +271,7 @@ def async_response( # pylint: disable=unused-argument
269271
questions = msg.questions
270272
now = msg.now
271273
for msg in msgs:
272-
if not msg.is_probe():
274+
if msg.is_probe() is False:
273275
answers.extend(msg.answers())
274276
else:
275277
is_probe = True

src/zeroconf/_handlers/record_manager.pxd

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
import cython
33

44
from .._cache cimport DNSCache
5-
from .._dns cimport DNSRecord
5+
from .._dns cimport DNSQuestion, DNSRecord
66
from .._protocol.incoming cimport DNSIncoming
7+
from .._updates cimport RecordUpdateListener
8+
from .._utils.time cimport current_time_millis
79

810

911
cdef cython.float _DNS_PTR_MIN_TTL
12+
cdef cython.uint _TYPE_PTR
1013
cdef object _ADDRESS_RECORD_TYPES
1114
cdef object RecordUpdate
1215
cdef bint TYPE_CHECKING
@@ -26,11 +29,15 @@ cdef class RecordManager:
2629
@cython.locals(
2730
cache=DNSCache,
2831
record=DNSRecord,
32+
answers=cython.list,
2933
maybe_entry=DNSRecord,
3034
now_float=cython.float
3135
)
3236
cpdef async_updates_from_response(self, DNSIncoming msg)
3337

34-
cpdef async_add_listener(self, object listener, object question)
38+
cpdef async_add_listener(self, RecordUpdateListener listener, object question)
3539

36-
cpdef async_remove_listener(self, object listener)
40+
cpdef async_remove_listener(self, RecordUpdateListener listener)
41+
42+
@cython.locals(question=DNSQuestion, record=DNSRecord)
43+
cdef _async_update_matching_records(self, RecordUpdateListener listener, cython.list questions)

src/zeroconf/_handlers/record_manager.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:
106106
)
107107
record.set_created_ttl(record.created, _DNS_PTR_MIN_TTL)
108108

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

112112
if TYPE_CHECKING:
113113
record = cast(_UniqueRecordsType, record)
114114

115115
maybe_entry = cache.async_get_unique(record)
116-
if not record.is_expired(now_float):
116+
if record.is_expired(now_float) is False:
117117
if maybe_entry is not None:
118118
maybe_entry.reset_ttl(record)
119119
else:
@@ -129,7 +129,7 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:
129129
removes.add(record)
130130

131131
if unique_types:
132-
cache.async_mark_unique_records_older_than_1s_to_expire(unique_types, answers, now)
132+
cache.async_mark_unique_records_older_than_1s_to_expire(unique_types, answers, now_float)
133133

134134
if updates:
135135
self.async_updates(now, updates)
@@ -151,7 +151,7 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:
151151
new = False
152152
if other_adds or address_adds:
153153
new = cache.async_add_records(address_adds)
154-
if cache.async_add_records(other_adds):
154+
if cache.async_add_records(other_adds) is True:
155155
new = True
156156
# Removes are processed last since
157157
# ServiceInfo could generate an un-needed query
@@ -182,7 +182,6 @@ def async_add_listener(
182182
return
183183

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

188187
def _async_update_matching_records(

0 commit comments

Comments
 (0)