Skip to content

Commit 35600fe

Browse files
committed
feat: speed up processing incoming records
add cython defs for set_created_ttl and reset_ttl
1 parent 2e7fefb commit 35600fe

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/zeroconf/_dns.pxd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ cdef class DNSRecord(DNSEntry):
4444
)
4545
cpdef suppressed_by(self, object msg)
4646

47+
@cython.locals(
48+
remaining=cython.float,
49+
)
50+
cpdef get_remaining_ttl(self, cython.float now)
51+
4752
cpdef get_expiration_time(self, cython.uint percent)
4853

4954
cpdef is_expired(self, cython.float now)

src/zeroconf/_dns.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from ._exceptions import AbstractMethodException
2828
from ._utils.net import _is_v6_address
29-
from ._utils.time import current_time_millis, millis_to_seconds
29+
from ._utils.time import current_time_millis
3030
from .const import _CLASS_MASK, _CLASS_UNIQUE, _CLASSES, _TYPE_ANY, _TYPES
3131

3232
_LEN_BYTE = 1
@@ -193,7 +193,10 @@ def get_expiration_time(self, percent: _int) -> float:
193193
# TODO: Switch to just int here
194194
def get_remaining_ttl(self, now: _float) -> Union[int, float]:
195195
"""Returns the remaining TTL in seconds."""
196-
return max(0, millis_to_seconds((self.created + (_EXPIRE_FULL_TIME_MS * self.ttl)) - now))
196+
remaining = self.created + (_EXPIRE_FULL_TIME_MS * self.ttl) - now
197+
if remaining < 0:
198+
return 0
199+
return remaining / 1000.0
197200

198201
def is_expired(self, now: _float) -> bool:
199202
"""Returns true if this record has expired."""

0 commit comments

Comments
 (0)