feat: speed up processing incoming records (#1216) · python-zeroconf/python-zeroconf@aff625d

@@ -408,6 +408,7 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:

408408

removes: Set[DNSRecord] = set()

409409

now = msg.now

410410

unique_types: Set[Tuple[str, int, int]] = set()

411+

cache = self.cache

411412412413

for record in msg.answers:

413414

# Protect zeroconf from records that can cause denial of service.

@@ -416,7 +417,9 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:

416417

# ServiceBrowsers generating excessive queries refresh queries.

417418

# Apple uses a 15s minimum TTL, however we do not have the same

418419

# level of rate limit and safe guards so we use 1/4 of the recommended value.

419-

if record.ttl and record.type == _TYPE_PTR and record.ttl < _DNS_PTR_MIN_TTL:

420+

record_type = record.type

421+

record_ttl = record.ttl

422+

if record_ttl and record_type == _TYPE_PTR and record_ttl < _DNS_PTR_MIN_TTL:

420423

log.debug(

421424

"Increasing effective ttl of %s to minimum of %s to protect against excessive refreshes.",

422425

record,

@@ -425,12 +428,12 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:

425428

record.set_created_ttl(record.created, _DNS_PTR_MIN_TTL)

426429427430

if record.unique: # https://tools.ietf.org/html/rfc6762#section-10.2

428-

unique_types.add((record.name, record.type, record.class_))

431+

unique_types.add((record.name, record_type, record.class_))

429432430433

if TYPE_CHECKING:

431434

record = cast(_UniqueRecordsType, record)

432435433-

maybe_entry = self.cache.async_get_unique(record)

436+

maybe_entry = cache.async_get_unique(record)

434437

if not record.is_expired(now):

435438

if maybe_entry is not None:

436439

maybe_entry.reset_ttl(record)

@@ -447,7 +450,7 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:

447450

removes.add(record)

448451449452

if unique_types:

450-

self.cache.async_mark_unique_records_older_than_1s_to_expire(unique_types, msg.answers, now)

453+

cache.async_mark_unique_records_older_than_1s_to_expire(unique_types, msg.answers, now)

451454452455

if updates:

453456

self.async_updates(now, updates)

@@ -468,12 +471,12 @@ def async_updates_from_response(self, msg: DNSIncoming) -> None:

468471

# processsed.

469472

new = False

470473

if other_adds or address_adds:

471-

new = self.cache.async_add_records(itertools.chain(address_adds, other_adds))

474+

new = cache.async_add_records(itertools.chain(address_adds, other_adds))

472475

# Removes are processed last since

473476

# ServiceInfo could generate an un-needed query

474477

# because the data was not yet populated.

475478

if removes:

476-

self.cache.async_remove_records(removes)

479+

cache.async_remove_records(removes)

477480

if updates:

478481

self.async_updates_complete(new)

479482