Skip to content

Commit c65d869

Browse files
authored
feat: significantly improve efficiency of the ServiceBrowser scheduler (#1335)
1 parent 7de655b commit c65d869

File tree

7 files changed

+1083
-479
lines changed

7 files changed

+1083
-479
lines changed

examples/browser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def on_service_state_change(
6666

6767
zeroconf = Zeroconf(ip_version=ip_version)
6868

69-
services = ["_http._tcp.local.", "_hap._tcp.local."]
69+
services = ["_http._tcp.local.", "_hap._tcp.local.", "_esphomelib._tcp.local.", "_airplay._tcp.local."]
7070
if args.find:
7171
services = list(ZeroconfServiceTypes.find(zc=zeroconf))
7272

src/zeroconf/_services/browser.pxd

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,79 +14,106 @@ cdef bint TYPE_CHECKING
1414
cdef object cached_possible_types
1515
cdef cython.uint _EXPIRE_REFRESH_TIME_PERCENT, _MAX_MSG_TYPICAL, _DNS_PACKET_HEADER_LEN
1616
cdef cython.uint _TYPE_PTR
17+
cdef object _CLASS_IN
1718
cdef object SERVICE_STATE_CHANGE_ADDED, SERVICE_STATE_CHANGE_REMOVED, SERVICE_STATE_CHANGE_UPDATED
1819
cdef cython.set _ADDRESS_RECORD_TYPES
20+
cdef float RESCUE_RECORD_RETRY_TTL_PERCENTAGE
21+
22+
cdef object _MDNS_PORT, _BROWSER_TIME
23+
24+
cdef object QU_QUESTION
25+
26+
cdef object _FLAGS_QR_QUERY
27+
28+
cdef object heappop, heappush
29+
30+
cdef class _ScheduledPTRQuery:
31+
32+
cdef public str alias
33+
cdef public str name
34+
cdef public unsigned int ttl
35+
cdef public bint cancelled
36+
cdef public double expire_time_millis
37+
cdef public double when_millis
1938

2039
cdef class _DNSPointerOutgoingBucket:
2140

22-
cdef public object now
41+
cdef public double now_millis
2342
cdef public DNSOutgoing out
2443
cdef public cython.uint bytes
2544

2645
cpdef add(self, cython.uint max_compressed_size, DNSQuestion question, cython.set answers)
2746

2847
@cython.locals(cache=DNSCache, question_history=QuestionHistory, record=DNSRecord, qu_question=bint)
29-
cpdef generate_service_query(
48+
cpdef list generate_service_query(
3049
object zc,
31-
double now,
32-
list type_,
50+
double now_millis,
51+
set types_,
3352
bint multicast,
3453
object question_type
3554
)
3655

3756
@cython.locals(answer=DNSPointer, query_buckets=list, question=DNSQuestion, max_compressed_size=cython.uint, max_bucket_size=cython.uint, query_bucket=_DNSPointerOutgoingBucket)
38-
cdef _group_ptr_queries_with_known_answers(object now, object multicast, cython.dict question_with_known_answers)
57+
cdef list _group_ptr_queries_with_known_answers(double now_millis, bint multicast, cython.dict question_with_known_answers)
3958

4059
cdef class QueryScheduler:
4160

42-
cdef cython.set _types
43-
cdef cython.dict _next_time
44-
cdef object _first_random_delay_interval
45-
cdef cython.dict _delay
61+
cdef object _zc
62+
cdef set _types
63+
cdef str _addr
64+
cdef int _port
65+
cdef bint _multicast
66+
cdef tuple _first_random_delay_interval
67+
cdef double _min_time_between_queries_millis
68+
cdef object _loop
69+
cdef unsigned int _startup_queries_sent
70+
cdef public dict _next_scheduled_for_alias
71+
cdef public list _query_heap
72+
cdef object _next_run
73+
cdef double _clock_resolution_millis
74+
cdef object _question_type
75+
76+
cpdef void schedule_ptr_first_refresh(self, DNSPointer pointer)
77+
78+
cdef void _schedule_ptr_refresh(self, DNSPointer pointer, double expire_time_millis, double refresh_time_millis)
79+
80+
cdef void _schedule_ptr_query(self, _ScheduledPTRQuery scheduled_query)
4681

47-
cpdef millis_to_wait(self, object now)
82+
@cython.locals(scheduled=_ScheduledPTRQuery)
83+
cpdef void cancel_ptr_refresh(self, DNSPointer pointer)
4884

49-
cpdef reschedule_type(self, object type_, object next_time)
85+
@cython.locals(current=_ScheduledPTRQuery, expire_time=double)
86+
cpdef void reschedule_ptr_first_refresh(self, DNSPointer pointer)
5087

51-
cpdef process_ready_types(self, object now)
88+
@cython.locals(ttl_millis='unsigned int', additional_wait=double, next_query_time=double)
89+
cpdef void schedule_rescue_query(self, _ScheduledPTRQuery query, double now_millis, float additional_percentage)
90+
91+
cpdef void _process_startup_queries(self)
92+
93+
@cython.locals(query=_ScheduledPTRQuery, next_scheduled=_ScheduledPTRQuery, next_when=double)
94+
cpdef void _process_ready_types(self)
95+
96+
cpdef void async_send_ready_queries(self, bint first_request, double now_millis, set ready_types)
5297

5398
cdef class _ServiceBrowserBase(RecordUpdateListener):
5499

55100
cdef public cython.set types
56101
cdef public object zc
57102
cdef DNSCache _cache
58103
cdef object _loop
59-
cdef public object addr
60-
cdef public object port
61-
cdef public object multicast
62-
cdef public object question_type
63104
cdef public cython.dict _pending_handlers
64105
cdef public object _service_state_changed
65106
cdef public QueryScheduler query_scheduler
66107
cdef public bint done
67-
cdef public object _first_request
68-
cdef public object _next_send_timer
69108
cdef public object _query_sender_task
70109

71-
cpdef _generate_ready_queries(self, object first_request, object now)
72-
73110
cpdef void _enqueue_callback(self, object state_change, object type_, object name)
74111

75112
@cython.locals(record_update=RecordUpdate, record=DNSRecord, cache=DNSCache, service=DNSRecord, pointer=DNSPointer)
76113
cpdef void async_update_records(self, object zc, double now, cython.list records)
77114

78115
cpdef cython.list _names_matching_types(self, object types)
79116

80-
cpdef reschedule_type(self, object type_, object now, object next_time)
81-
82117
cpdef _fire_service_state_changed_event(self, cython.tuple event)
83118

84-
cpdef _async_send_ready_queries_schedule_next(self)
85-
86-
cpdef _async_schedule_next(self, object now)
87-
88-
cpdef _async_send_ready_queries(self, object now)
89-
90-
cpdef _cancel_send_timer(self)
91-
92119
cpdef void async_update_records_complete(self)

0 commit comments

Comments
 (0)