Skip to content

Commit 7f2b7e4

Browse files
committed
feat: improve response handler performance
1 parent 938fe21 commit 7f2b7e4

7 files changed

Lines changed: 31 additions & 8 deletions

File tree

build_ext.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def build(setup_kwargs: Any) -> None:
4141
"src/zeroconf/_services/browser.py",
4242
"src/zeroconf/_services/info.py",
4343
"src/zeroconf/_services/registry.py",
44+
"src/zeroconf/_transport.py",
4445
"src/zeroconf/_updates.py",
4546
"src/zeroconf/_utils/ipaddress.py",
4647
"src/zeroconf/_utils/time.py",

src/zeroconf/_handlers/query_handler.pxd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ from .._history cimport QuestionHistory
77
from .._protocol.incoming cimport DNSIncoming
88
from .._services.info cimport ServiceInfo
99
from .._services.registry cimport ServiceRegistry
10+
from .._transport cimport _WrappedTransport
1011
from .answers cimport (
1112
QuestionAnswers,
1213
construct_outgoing_multicast_answers,
@@ -115,6 +116,6 @@ cdef class QueryHandler:
115116
list packets,
116117
object addr,
117118
object port,
118-
object transport,
119+
_WrappedTransport transport,
119120
tuple v6_flow_scope
120121
)

src/zeroconf/_listener.pxd

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ from ._handlers.query_handler cimport QueryHandler
55
from ._handlers.record_manager cimport RecordManager
66
from ._protocol.incoming cimport DNSIncoming
77
from ._services.registry cimport ServiceRegistry
8+
from ._transport cimport _WrappedTransport
89
from ._utils.time cimport current_time_millis, millis_to_seconds
910

1011

1112
cdef object log
1213
cdef object DEBUG_ENABLED
14+
cdef object randint
1315
cdef bint TYPE_CHECKING
1416

1517
cdef cython.uint _MAX_MSG_ABSOLUTE
@@ -26,7 +28,7 @@ cdef class AsyncListener:
2628
cdef public cython.bytes data
2729
cdef public double last_time
2830
cdef public DNSIncoming last_message
29-
cdef public object transport
31+
cdef public _WrappedTransport transport
3032
cdef public object sock_description
3133
cdef public cython.dict _deferred
3234
cdef public cython.dict _timers
@@ -45,15 +47,15 @@ cdef class AsyncListener:
4547
DNSIncoming msg,
4648
object addr,
4749
object port,
48-
object transport,
50+
_WrappedTransport transport,
4951
tuple v6_flow_scope
5052
)
5153

52-
cpdef _respond_query(
54+
cpdef void _respond_query(
5355
self,
5456
object msg,
5557
object addr,
5658
object port,
57-
object transport,
59+
_WrappedTransport transport,
5860
tuple v6_flow_scope
5961
)

src/zeroconf/_listener.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
DEBUG_ENABLED = partial(log.isEnabledFor, logging.DEBUG)
4747

4848

49+
randint = random.randint
50+
51+
4952
class AsyncListener:
5053
"""A Listener is used by this module to listen on the multicast
5154
group to which DNS messages are sent, allowing the implementation
@@ -193,7 +196,7 @@ def handle_query_or_defer(
193196
) -> None:
194197
"""Deal with incoming query packets. Provides a response if
195198
possible."""
196-
if not msg.truncated:
199+
if not msg.is_truncated():
197200
self._respond_query(msg, addr, port, transport, v6_flow_scope)
198201
return
199202

@@ -203,7 +206,7 @@ def handle_query_or_defer(
203206
if incoming.data == msg.data:
204207
return
205208
deferred.append(msg)
206-
delay = millis_to_seconds(random.randint(*_TC_DELAY_RANDOM_INTERVAL)) # noqa: S311
209+
delay = millis_to_seconds(randint(*_TC_DELAY_RANDOM_INTERVAL))
207210
loop = self.zc.loop
208211
assert loop is not None
209212
self._cancel_any_timers_for_addr(addr)

src/zeroconf/_protocol/incoming.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ cdef class DNSIncoming:
7474

7575
cpdef bint is_response(self)
7676

77+
cpdef bint is_truncated(self)
78+
7779
@cython.locals(
7880
off="unsigned int",
7981
label_idx="unsigned int",

src/zeroconf/_protocol/incoming.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,18 @@ def is_response(self) -> bool:
136136
"""Returns true if this is a response."""
137137
return (self.flags & _FLAGS_QR_MASK) == _FLAGS_QR_RESPONSE
138138

139+
def is_truncated(self) -> bool:
140+
"""Returns true if this is truncated."""
141+
return (self.flags & _FLAGS_TC) == _FLAGS_TC
142+
139143
def has_qu_question(self) -> bool:
140144
"""Returns true if any question is a QU question."""
141145
return self._has_qu_question
142146

143147
@property
144148
def truncated(self) -> bool:
145149
"""Returns true if this is a truncated."""
146-
return (self.flags & _FLAGS_TC) == _FLAGS_TC
150+
return self.is_truncated()
147151

148152
@property
149153
def questions(self) -> List[DNSQuestion]:

src/zeroconf/_transport.pxd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
import cython
3+
4+
cdef class _WrappedTransport:
5+
6+
cdef public object transport
7+
cdef public bint is_ipv6
8+
cdef public object sock
9+
cdef public int fileno
10+
cdef public tuple sock_name

0 commit comments

Comments
 (0)