Skip to content

Commit af77ca7

Browse files
authored
Adjust AsyncMDNSResolver construction to have a default mDNS timeout (#9)
1 parent 51df0c3 commit af77ca7

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

docs/api.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The only public *aiohttp_asyncmdnsresolver.api* class is :class:`AsyncMDNSResolv
1313
>>> from aiohttp_asyncmdnsresolver.api import AsyncMDNSResolver
1414

1515

16-
.. class:: AsyncMDNSResolver(*args*, *, async_zeroconf, timeout, **kwargs)
16+
.. class:: AsyncMDNSResolver(*args, *, async_zeroconf=None, mdns_timeout=5.0, **kwargs)
1717

1818
This class functions the same as ``aiohttp.resolver.AsyncResolver``,
1919
but with the added ability to resolve mDNS queries.
@@ -22,8 +22,9 @@ The only public *aiohttp_asyncmdnsresolver.api* class is :class:`AsyncMDNSResolv
2222
passed, it will be used to resolve mDNS queries. If not, a new
2323
instance will be created.
2424

25-
:param float timeout: The timeout for the mDNS query in seconds. If not provided
26-
the default timeout is 5 seconds.
25+
:param float mdns_timeout: The timeout for the mDNS query in seconds. If not provided
26+
the default timeout is 5 seconds. If the mdns_timeout is set to 0, the
27+
query will only use the cache and will not perform a new query.
2728

2829
Example::
2930

src/aiohttp_asyncmdnsresolver/_impl.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ def __init__(
7777
self,
7878
*args: Any,
7979
async_zeroconf: AsyncZeroconf | None = None,
80-
timeout: float | None = None,
80+
mdns_timeout: float | None = DEFAULT_TIMEOUT,
8181
**kwargs: Any,
8282
) -> None:
8383
"""Initialize the resolver."""
8484
super().__init__(*args, **kwargs)
85-
self._timeout = timeout or DEFAULT_TIMEOUT
85+
self._mdns_timeout = mdns_timeout
8686
self._aiozc_owner = async_zeroconf is None
8787
self._aiozc = async_zeroconf or AsyncZeroconf()
8888

@@ -106,8 +106,10 @@ async def _resolve_mdns(
106106
if (
107107
info.load_from_cache(self._aiozc.zeroconf)
108108
or (
109-
self._timeout
110-
and await info.async_request(self._aiozc.zeroconf, self._timeout * 1000)
109+
self._mdns_timeout
110+
and await info.async_request(
111+
self._aiozc.zeroconf, self._mdns_timeout * 1000
112+
)
111113
)
112114
) and (addresses := info.ip_addresses_by_version(ip_version)):
113115
return [_to_resolve_result(host, port, address) for address in addresses]

tests/test_impl.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@pytest_asyncio.fixture
1818
async def resolver() -> AsyncGenerator[AsyncMDNSResolver]:
1919
"""Return a resolver."""
20-
resolver = AsyncMDNSResolver(timeout=0.1)
20+
resolver = AsyncMDNSResolver(mdns_timeout=0.1)
2121
yield resolver
2222
await resolver.close()
2323

@@ -26,7 +26,7 @@ async def resolver() -> AsyncGenerator[AsyncMDNSResolver]:
2626
async def custom_resolver() -> AsyncGenerator[AsyncMDNSResolver]:
2727
"""Return a resolver."""
2828
aiozc = AsyncZeroconf()
29-
resolver = AsyncMDNSResolver(timeout=0.1, async_zeroconf=aiozc)
29+
resolver = AsyncMDNSResolver(mdns_timeout=0.1, async_zeroconf=aiozc)
3030
yield resolver
3131
await resolver.close()
3232
await aiozc.async_close()
@@ -198,7 +198,7 @@ async def test_resolve_mdns_passed_in_asynczeroconf(
198198
async def test_create_destroy_resolver() -> None:
199199
"""Test the resolver can be created and destroyed."""
200200
aiozc = AsyncZeroconf()
201-
resolver = AsyncMDNSResolver(timeout=0.1, async_zeroconf=aiozc)
201+
resolver = AsyncMDNSResolver(mdns_timeout=0.1, async_zeroconf=aiozc)
202202
await resolver.close()
203203
await aiozc.async_close()
204204
assert resolver._aiozc is None
@@ -208,7 +208,7 @@ async def test_create_destroy_resolver() -> None:
208208
@pytest.mark.asyncio
209209
async def test_create_destroy_resolver_no_aiozc() -> None:
210210
"""Test the resolver can be created and destroyed."""
211-
resolver = AsyncMDNSResolver(timeout=0.1)
211+
resolver = AsyncMDNSResolver(mdns_timeout=0.1)
212212
await resolver.close()
213213
assert resolver._aiozc is None
214214
assert resolver._aiozc_owner is True

0 commit comments

Comments
 (0)