Skip to content

Commit 8a9ae29

Browse files
authored
Reduce branching in Zeroconf.handle_response (#467)
- Adds `add_records` and `remove_records` to `DNSCache` to permit multiple records to be added or removed in one call - This change is not enough to remove the too-many-branches pylint disable, however when combined with #419 it should no longer be needed
1 parent 7a50402 commit 8a9ae29

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

zeroconf/__init__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,12 +1284,22 @@ def add(self, entry: DNSRecord) -> None:
12841284
if isinstance(entry, DNSService):
12851285
self.service_cache.setdefault(entry.server, []).append(entry)
12861286

1287+
def add_records(self, entries: Iterable[DNSRecord]) -> None:
1288+
"""Add multiple records."""
1289+
for entry in entries:
1290+
self.add(entry)
1291+
12871292
def remove(self, entry: DNSRecord) -> None:
12881293
"""Removes an entry."""
12891294
if isinstance(entry, DNSService):
12901295
DNSCache.remove_key(self.service_cache, entry.server, entry)
12911296
DNSCache.remove_key(self.cache, entry.key, entry)
12921297

1298+
def remove_records(self, entries: Iterable[DNSRecord]) -> None:
1299+
"""Remove multiple records."""
1300+
for entry in entries:
1301+
self.remove(entry)
1302+
12931303
@staticmethod
12941304
def remove_key(cache: dict, key: str, entry: DNSRecord) -> None:
12951305
"""Forgiving remove of a cache key."""
@@ -2940,13 +2950,11 @@ def handle_response(self, msg: DNSIncoming) -> None: # pylint: disable=too-many
29402950
# zc.get_service_info will see the cached value
29412951
# but ONLY after all the record updates have been
29422952
# processsed.
2943-
for record in itertools.chain(address_adds, other_adds):
2944-
self.cache.add(record)
2953+
self.cache.add_records(itertools.chain(address_adds, other_adds))
29452954
# Removes are processed last since
29462955
# ServiceInfo could generate an un-needed query
29472956
# because the data was not yet populated.
2948-
for record in removes:
2949-
self.cache.remove(record)
2957+
self.cache.remove_records(removes)
29502958

29512959
def _answer_service_type_enumeration_query(self, msg: DNSIncoming, out: DNSOutgoing) -> None:
29522960
"""Provide an answer to a service type enumeration query.

0 commit comments

Comments
 (0)