Skip to content

chore: migrate TTL to only accept int #1577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/zeroconf/_cache.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ cdef class DNSCache:
self,
DNSRecord record,
double now,
cython.float ttl
unsigned int ttl
)
2 changes: 1 addition & 1 deletion src/zeroconf/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def async_mark_unique_records_older_than_1s_to_expire(
# Expire in 1s
self._async_set_created_ttl(record, now, 1)

def _async_set_created_ttl(self, record: DNSRecord, now: _float, ttl: _float) -> None:
def _async_set_created_ttl(self, record: DNSRecord, now: _float, ttl: _int) -> None:
"""Set the created time and ttl of a record."""
# It would be better if we made a copy instead of mutating the record
# in place, but records currently don't have a copy method.
Expand Down
18 changes: 9 additions & 9 deletions src/zeroconf/_dns.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ cdef class DNSQuestion(DNSEntry):

cdef class DNSRecord(DNSEntry):

cdef public cython.float ttl
cdef public unsigned int ttl
cdef public double created

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

cdef bint _suppressed_by_answer(self, DNSRecord answer)

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

cpdef bint is_recent(self, double now)

cdef _set_created_ttl(self, double now, cython.float ttl)
cdef _set_created_ttl(self, double now, unsigned int ttl)

cdef class DNSAddress(DNSRecord):

cdef public cython.int _hash
cdef public bytes address
cdef public object scope_id

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

cdef bint _eq(self, DNSAddress other)

Expand All @@ -87,7 +87,7 @@ cdef class DNSHinfo(DNSRecord):
cdef public str cpu
cdef public str os

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

cdef bint _eq(self, DNSHinfo other)

Expand All @@ -99,7 +99,7 @@ cdef class DNSPointer(DNSRecord):
cdef public str alias
cdef public str alias_key

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

cdef bint _eq(self, DNSPointer other)

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

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

cdef bint _eq(self, DNSText other)

Expand All @@ -125,7 +125,7 @@ cdef class DNSService(DNSRecord):
cdef public str server
cdef public str server_key

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)
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)

cdef bint _eq(self, DNSService other)

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

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

cdef bint _eq(self, DNSNsec other)

Expand Down
23 changes: 11 additions & 12 deletions src/zeroconf/_dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,17 @@ class DNSRecord(DNSEntry):

__slots__ = ("created", "ttl")

# TODO: Switch to just int ttl
def __init__(
self,
name: str,
type_: int,
class_: int,
ttl: float | int,
ttl: _int,
created: float | None = None,
) -> None:
self._fast_init_record(name, type_, class_, ttl, created or current_time_millis())

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

def _set_created_ttl(self, created: _float, ttl: float | int) -> None:
def _set_created_ttl(self, created: _float, ttl: _int) -> None:
"""Set the created and ttl of a record."""
# It would be better if we made a copy instead of mutating the record
# in place, but records currently don't have a copy method.
Expand Down Expand Up @@ -266,7 +265,7 @@ def _fast_init(
name: str,
type_: _int,
class_: _int,
ttl: _float,
ttl: _int,
address: bytes,
scope_id: _int | None,
created: _float,
Expand Down Expand Up @@ -327,7 +326,7 @@ def __init__(
self._fast_init(name, type_, class_, ttl, cpu, os, created or current_time_millis())

def _fast_init(
self, name: str, type_: _int, class_: _int, ttl: _float, cpu: str, os: str, created: _float
self, name: str, type_: _int, class_: _int, ttl: _int, cpu: str, os: str, created: _float
) -> None:
"""Fast init for reuse."""
self._fast_init_record(name, type_, class_, ttl, created)
Expand Down Expand Up @@ -374,7 +373,7 @@ def __init__(
self._fast_init(name, type_, class_, ttl, alias, created or current_time_millis())

def _fast_init(
self, name: str, type_: _int, class_: _int, ttl: _float, alias: str, created: _float
self, name: str, type_: _int, class_: _int, ttl: _int, alias: str, created: _float
) -> None:
self._fast_init_record(name, type_, class_, ttl, created)
self.alias = alias
Expand Down Expand Up @@ -429,7 +428,7 @@ def __init__(
self._fast_init(name, type_, class_, ttl, text, created or current_time_millis())

def _fast_init(
self, name: str, type_: _int, class_: _int, ttl: _float, text: bytes, created: _float
self, name: str, type_: _int, class_: _int, ttl: _int, text: bytes, created: _float
) -> None:
self._fast_init_record(name, type_, class_, ttl, created)
self.text = text
Expand Down Expand Up @@ -468,7 +467,7 @@ def __init__(
name: str,
type_: int,
class_: int,
ttl: float | int,
ttl: int,
priority: int,
weight: int,
port: int,
Expand All @@ -484,7 +483,7 @@ def _fast_init(
name: str,
type_: _int,
class_: _int,
ttl: _float,
ttl: _int,
priority: _int,
weight: _int,
port: _int,
Expand Down Expand Up @@ -539,7 +538,7 @@ def __init__(
name: str,
type_: int,
class_: int,
ttl: int | float,
ttl: _int,
next_name: str,
rdtypes: list[int],
created: float | None = None,
Expand All @@ -551,7 +550,7 @@ def _fast_init(
name: str,
type_: _int,
class_: _int,
ttl: _float,
ttl: _int,
next_name: str,
rdtypes: list[_int],
created: _float,
Expand Down
2 changes: 1 addition & 1 deletion src/zeroconf/_handlers/record_manager.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from .._updates cimport RecordUpdateListener
from .._utils.time cimport current_time_millis
from .._record_update cimport RecordUpdate

cdef cython.float _DNS_PTR_MIN_TTL
cdef unsigned int _DNS_PTR_MIN_TTL
cdef cython.uint _TYPE_PTR
cdef object _ADDRESS_RECORD_TYPES
cdef bint TYPE_CHECKING
Expand Down
3 changes: 1 addition & 2 deletions src/zeroconf/_services/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,8 @@ def _schedule_ptr_refresh(
refresh_time_millis: float_,
) -> None:
"""Schedule a query for a pointer."""
ttl = int(pointer.ttl) if isinstance(pointer.ttl, float) else pointer.ttl
scheduled_ptr_query = _ScheduledPTRQuery(
pointer.alias, pointer.name, ttl, expire_time_millis, refresh_time_millis
pointer.alias, pointer.name, pointer.ttl, expire_time_millis, refresh_time_millis
)
self._schedule_ptr_query(scheduled_ptr_query)

Expand Down
2 changes: 1 addition & 1 deletion src/zeroconf/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
# ServiceBrowsers generating excessive queries refresh queries.
# Apple uses a 15s minimum TTL, however we do not have the same
# level of rate limit and safe guards so we use 1/4 of the recommended value
_DNS_PTR_MIN_TTL = _DNS_OTHER_TTL / 4
_DNS_PTR_MIN_TTL = 1125

_DNS_PACKET_HEADER_LEN = 12

Expand Down
2 changes: 1 addition & 1 deletion tests/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_suppress_answer(self):
"testname2.local.",
const._TYPE_SRV,
const._CLASS_IN | const._CLASS_UNIQUE,
const._DNS_HOST_TTL / 2,
int(const._DNS_HOST_TTL / 2),
0,
0,
80,
Expand Down
Loading