8000 chore: migrate TTL to only accept int (#1577) · python-zeroconf/python-zeroconf@5a72fd4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5a72fd4

Browse files
authored
chore: migrate TTL to only accept int (#1577)
1 parent 66b673c commit 5a72fd4

File tree

8 files changed

+26
-28
lines changed

8 files changed

+26
-28
lines changed

src/zeroconf/_cache.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ cdef class DNSCache:
8383
self,
8484
DNSRecord record,
8585
double now,
86-
cython.float ttl
86+
unsigned int ttl
8787
)

src/zeroconf/_cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def async_mark_unique_records_older_than_1s_to_expire(
317317
# Expire in 1s
318318
self._async_set_created_ttl(record, now, 1)
319319

320-
def _async_set_created_ttl(self, record: DNSRecord, now: _float, ttl: _float) -> None:
320+
def _async_set_created_ttl(self, record: DNSRecord, now: _float, ttl: _int) -> None:
321321
"""Set the created time and ttl of a record."""
322322
# It would be better if we made a copy instead of mutating the record
323323
# in place, but records currently don't have a copy method.

src/zeroconf/_dns.pxd

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ cdef class DNSQuestion(DNSEntry):
4444

4545
cdef class DNSRecord(DNSEntry):
4646

47-
cdef public cython.float ttl
47+
cdef public unsigned int ttl
4848
cdef public double created
4949

50-
cdef _fast_init_record(self, str name, cython.uint type_, cython.uint class_, cython.float ttl, double created)
50+
cdef _fast_init_record(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, double created)
5151

5252
cdef bint _suppressed_by_answer(self, DNSRecord answer)
5353

@@ -66,15 +66,15 @@ cdef class DNSRecord(DNSEntry):
6666

6767
cpdef bint is_recent(self, double now)
6868

69-
cdef _set_created_ttl(self, double now, cython.float ttl)
69+
cdef _set_created_ttl(self, double now, unsigned int ttl)
7070

7171
cdef class DNSAddress(DNSRecord):
7272

7373
cdef public cython.int _hash
7474
cdef public bytes address
7575
cdef public object scope_id
7676

77-
cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, cython.float ttl, bytes address, object scope_id, double created)
77+
cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, bytes address, object scope_id, double created)
7878

7979
cdef bint _eq(self, DNSAddress other)
8080

@@ -87,7 +87,7 @@ cdef class DNSHinfo(DNSRecord):
8787
cdef public str cpu
8888
cdef public str os
8989

90-
cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, cython.float ttl, str cpu, str os, double created)
90+
cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, str cpu, str os, double created)
9191

9292
cdef bint _eq(self, DNSHinfo other)
9393

@@ -99,7 +99,7 @@ cdef class DNSPointer(DNSRecord):
9999
cdef public str alias
100100
cdef public str alias_key
101101

102-
cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, cython.float ttl, str alias, double created)
102+
cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, str alias, double created)
103103

104104
cdef bint _eq(self, DNSPointer other)
105105

@@ -110,7 +110,7 @@ cdef class DNSText(DNSRecord):
110110
cdef public cython.int _hash
111111
cdef public bytes text
112112

113-
cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, cython.float ttl, bytes text, double created)
113+
cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, bytes text, double created)
114114

115115
cdef bint _eq(self, DNSText other)
116116

@@ -125,7 +125,7 @@ cdef class DNSService(DNSRecord):
125125
cdef public str server
126126
cdef public str server_key
127127

128-
cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, cython.float ttl, cython.uint priority, cython.uint weight, cython.uint port, str server, double created)
128+
cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, cython.uint priority, cython.uint weight, cython.uint port, str server, double created)
129129

130130
cdef bint _eq(self, DNSService other)
131131

@@ -137,7 +137,7 @@ cdef class DNSNsec(DNSRecord):
137137
cdef public str next_name
138138
cdef public cython.list rdtypes
139139

140-
cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, cython.float ttl, str next_name, cython.list rdtypes, double created)
140+
cdef _fast_init(self, str name, cython.uint type_, cython.uint class_, unsigned int ttl, str next_name, cython.list rdtypes, double created)
141141

142142
cdef bint _eq(self, DNSNsec other)
143143

src/zeroconf/_dns.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,17 @@ class DNSRecord(DNSEntry):
166166

167167
__slots__ = ("created", "ttl")
168168

169-
# TODO: Switch to just int ttl
170169
def __init__(
171170
self,
172171
name: str,
173172
type_: int,
174173
class_: int,
175-
ttl: float | int,
174+
ttl: _int,
176175
created: float | None = None,
177176
) -> None:
178177
self._fast_init_record(name, type_, class_, ttl, created or current_time_millis())
179178

180-
def _fast_init_record(self, name: str, type_: _int, class_: _int, ttl: _float, created: _float) -> None:
179+
def _fast_init_record(self, name: str, type_: _int, class_: _int, ttl: _int, created: _float) -> None:
181180
"""Fast init for reuse."""
182181
self._fast_init_entry(name, type_, class_)
183182
self.ttl = ttl
@@ -227,7 +226,7 @@ def is_recent(self, now: _float) -> bool:
227226
"""Returns true if the record more than one quarter of its TTL remaining."""
228227
return self.created + (_RECENT_TIME_MS * self.ttl) > now
229228

230-
def _set_created_ttl(self, created: _float, ttl: float | int) -> None:
229+
def _set_created_ttl(self, created: _float, ttl: _int) -> None:
231230
"""Set the created and ttl of a record."""
232231
# It would be better if we made a copy instead of mutating the record
233232
# in place, but records currently don't have a copy method.
@@ -266,7 +265,7 @@ def _fast_init(
266265
name: str,
267266
type_: _int,
268267
class_: _int,
269-
ttl: _float,
268+
ttl: _int,
270269
address: bytes,
271270
scope_id: _int | None,
272271
created: _float,
@@ -327,7 +326,7 @@ def __init__(
327326
self._fast_init(name, type_, class_, ttl, cpu, os, created or current_time_millis())
328327

329328
def _fast_init(
330-
self, name: str, type_: _int, class_: _int, ttl: _float, cpu: str, os: str, created: _float
329+
self, name: str, type_: _int, class_: _int, ttl: _int, cpu: str, os: str, created: _float
331330
) -> None:
332331
"""Fast init for reuse."""
333332
self._fast_init_record(name, type_, class_, ttl, created)
@@ -374,7 +373,7 @@ def __init__(
374373
self._fast_init(name, type_, class_, ttl, alias, created or current_time_millis())
375374

376375
def _fast_init(
377-
self, name: str, type_: _int, class_: _int, ttl: _float, alias: str, created: _float
376+
self, name: str, type_: _int, class_: _int, ttl: _int, alias: str, created: _float
378377
) -> None:
379378
self._fast_init_record(name, type_, class_, ttl, created)
380379
self.alias = alias
@@ -429,7 +428,7 @@ def __init__(
429428
self._fast_init(name, type_, class_, ttl, text, created or current_time_millis())
430429

431430
def _fast_init(
432-
self, name: str, type_: _int, class_: _int, ttl: _float, text: bytes, created: _float
431+
self, name: str, type_: _int, class_: _int, ttl: _int, text: bytes, created: _float
433432
) -> None:
434433
self._fast_init_record(name, type_, class_, ttl, created)
435434
self.text = text
@@ -468,7 +467,7 @@ def __init__(
468467
name: str,
469468
type_: int,
470469
class_: int,
471-
ttl: float | int,
470+
ttl: int,
472471
priority: int,
473472
weight: int,
474473
port: int,
@@ -484,7 +483,7 @@ def _fast_init(
484483
name: str,
485484
type_: _int,
486485
class_: _int,
487-
ttl: _float,
486+
ttl: _int,
488487
priority: _int,
489488
weight: _int,
490489
port: _int,
@@ -539,7 +538,7 @@ def __init__(
539538
name: str,
540539
type_: int,
541540
class_: int,
542-
ttl: int | float,
541+
ttl: _int,
543542
next_name: str,
544543
rdtypes: list[int],
545544
created: float | None = None,
@@ -551,7 +550,7 @@ def _fast_init(
551550
name: str,
552551
type_: _int,
553552
class_: _int,
554-
ttl: _float,
553+
ttl: _int,
555554
next_name: str,
556555
rdtypes: list[_int],
557556
created: _float,

src/zeroconf/_handlers/record_manager.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ from .._updates cimport RecordUpdateListener
88
from .._utils.time cimport current_time_millis
99
from .._record_update cimport RecordUpdate
1010

11-
cdef cython.float _DNS_PTR_MIN_TTL
11+
cdef unsigned int _DNS_PTR_MIN_TTL
1212
cdef cython.uint _TYPE_PTR
1313
cdef object _ADDRESS_RECORD_TYPES
1414
cdef bint TYPE_CHECKING

src/zeroconf/_services/browser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,8 @@ def _schedule_ptr_refresh(
394394
refresh_time_millis: float_,
395395
) -> None:
396396
"""Schedule a query for a pointer."""
397-
ttl = int(pointer.ttl) if isinstance(pointer.ttl, float) else pointer.ttl
398397
scheduled_ptr_query = _ScheduledPTRQuery(
399-
pointer.alias, pointer.name, ttl, expire_time_millis, refresh_time_millis
398+
pointer.alias, pointer.name, pointer.ttl, expire_time_millis, refresh_time_millis
400399
)
401400
self._schedule_ptr_query(scheduled_ptr_query)
402401

src/zeroconf/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
# ServiceBrowsers generating excessive queries refresh queries.
5858
# Apple uses a 15s minimum TTL, however we do not have the same
5959
# level of rate limit and safe guards so we use 1/4 of the recommended value
60-
_DNS_PTR_MIN_TTL = _DNS_OTHER_TTL / 4
60+
_DNS_PTR_MIN_TTL = 1125
6161

6262
_DNS_PACKET_HEADER_LEN = 12
6363

tests/test_protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def test_suppress_answer(self):
196196
"testname2.local.",
197197
const._TYPE_SRV,
198198
const._CLASS_IN | const._CLASS_UNIQUE,
199-
const._DNS_HOST_TTL / 2,
199+
int(const._DNS_HOST_TTL / 2),
200200
0,
201201
0,
202202
80,

0 commit comments

Comments
 (0)
0