Avoid churning zeroconf instances in the host resolver when created in the reconnect logic (#729)

This commit is contained in:
J. Nick Koston 2023-11-26 10:05:24 -06:00 committed by GitHub
parent 991214ef52
commit aded2d28b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View File

@ -58,6 +58,7 @@ async def _async_zeroconf_get_service_info(
timeout: float,
) -> AsyncServiceInfo:
# Use or create zeroconf instance, ensure it's an AsyncZeroconf
had_instance = zeroconf_manager.has_instance
try:
zc = zeroconf_manager.get_async_zeroconf().zeroconf
except Exception as exc:
@ -73,7 +74,8 @@ async def _async_zeroconf_get_service_info(
f"Error resolving mDNS {service_name} via mDNS: {exc}"
) from exc
finally:
await zeroconf_manager.async_close()
if not had_instance:
await zeroconf_manager.async_close()
return info

View File

@ -26,6 +26,11 @@ class ZeroconfManager:
if zeroconf is not None:
self.set_instance(zeroconf)
@property
def has_instance(self) -> bool:
"""Return True if a Zeroconf instance is set."""
return self._aiozc is not None
def set_instance(self, zc: AsyncZeroconf | Zeroconf) -> None:
"""Set the AsyncZeroconf instance."""
if self._aiozc: