Improve performance of dhcp on high activity networks (#105884)

* Improve performance of dhcp on high activity networks

Most of the overhead was constructing IPAddress objects
which is solved with https://github.com/bdraco/cached-ipaddress

* fix test

* fix: bump

* bump again

* lets do it on the correct branch
This commit is contained in:
J. Nick Koston 2023-12-21 09:36:09 -10:00 committed by GitHub
parent f9c096687f
commit 54f460b7c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 3 deletions

View file

@ -9,7 +9,6 @@ from dataclasses import dataclass
from datetime import timedelta
from fnmatch import translate
from functools import lru_cache
from ipaddress import ip_address as make_ip_address
import logging
import os
import re
@ -22,6 +21,7 @@ from aiodiscover.discovery import (
IP_ADDRESS as DISCOVERY_IP_ADDRESS,
MAC_ADDRESS as DISCOVERY_MAC_ADDRESS,
)
from cached_ipaddress import cached_ip_addresses
from scapy.config import conf
from scapy.error import Scapy_Exception
@ -153,7 +153,10 @@ class WatcherBase(ABC):
self, ip_address: str, hostname: str, mac_address: str
) -> None:
"""Process a client."""
made_ip_address = make_ip_address(ip_address)
if (made_ip_address := cached_ip_addresses(ip_address)) is None:
# Ignore invalid addresses
_LOGGER.debug("Ignoring invalid IP Address: %s", ip_address)
return
if (
made_ip_address.is_link_local

View file

@ -7,5 +7,9 @@
"iot_class": "local_push",
"loggers": ["aiodiscover", "dnspython", "pyroute2", "scapy"],
"quality_scale": "internal",
"requirements": ["scapy==2.5.0", "aiodiscover==1.6.0"]
"requirements": [
"scapy==2.5.0",
"aiodiscover==1.6.0",
"cached_ipaddress==0.3.0"
]
}

View file

@ -16,6 +16,7 @@ bleak==0.21.1
bluetooth-adapters==0.16.2
bluetooth-auto-recovery==1.2.3
bluetooth-data-tools==1.19.0
cached_ipaddress==0.3.0
certifi>=2021.5.30
ciso8601==2.3.0
cryptography==41.0.7

View file

@ -608,6 +608,9 @@ btsmarthub-devicelist==0.2.3
# homeassistant.components.buienradar
buienradar==1.0.5
# homeassistant.components.dhcp
cached_ipaddress==0.3.0
# homeassistant.components.caldav
caldav==1.3.8

View file

@ -507,6 +507,9 @@ bthome-ble==3.3.1
# homeassistant.components.buienradar
buienradar==1.0.5
# homeassistant.components.dhcp
cached_ipaddress==0.3.0
# homeassistant.components.caldav
caldav==1.3.8

View file

@ -828,6 +828,36 @@ async def test_device_tracker_hostname_and_macaddress_after_start_hostname_missi
assert len(mock_init.mock_calls) == 0
async def test_device_tracker_invalid_ip_address(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test an invalid ip address."""
with patch.object(hass.config_entries.flow, "async_init") as mock_init:
device_tracker_watcher = dhcp.DeviceTrackerWatcher(
hass,
{},
[{"domain": "mock-domain", "hostname": "connect", "macaddress": "B8B7F1*"}],
)
await device_tracker_watcher.async_start()
await hass.async_block_till_done()
hass.states.async_set(
"device_tracker.august_connect",
STATE_HOME,
{
ATTR_IP: "invalid",
ATTR_SOURCE_TYPE: SourceType.ROUTER,
ATTR_MAC: "B8:B7:F1:6D:B5:33",
},
)
await hass.async_block_till_done()
await device_tracker_watcher.async_stop()
await hass.async_block_till_done()
assert "Ignoring invalid IP Address: invalid" in caplog.text
assert len(mock_init.mock_calls) == 0
async def test_device_tracker_ignore_self_assigned_ips_before_start(
hass: HomeAssistant,
) -> None: