Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tests/services/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,3 +754,25 @@ def test_request_timeout():
# 3000ms for the default timeout
# 1000ms for loaded systems + schedule overhead
assert (end_time - start_time) < 3000 + 1000


@pytest.mark.asyncio
async def test_we_try_four_times_with_random_delay():
"""Verify we try four times even with the random delay."""
type_ = "_typethatisnothere._tcp.local."
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])

# we are going to patch the zeroconf send to check query transmission
request_count = 0
def async_send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT):
"""Sends an outgoing packet."""
nonlocal request_count
request_count += 1

# patch the zeroconf send
with patch.object(aiozc.zeroconf, "async_send", async_send):
await aiozc.async_get_service_info(f"willnotbefound.{type_}", type_)

await aiozc.async_close()

assert request_count == 4
12 changes: 12 additions & 0 deletions zeroconf/_services/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""

import ipaddress
import random
import socket
from typing import Any, Dict, List, Optional, TYPE_CHECKING, Union, cast

Expand Down Expand Up @@ -52,6 +53,16 @@
)


# https://datatracker.ietf.org/doc/html/rfc6762#section-5.2
# The most common case for calling ServiceInfo is from a
# ServiceBrowser. After the first request we add a few random
# milliseconds to the delay between requests to reduce the chance
# that there are multiple ServiceBrowser callbacks running on
# the network that are firing at the same time when they
# see the same multicast response and decide to refresh
# the A/AAAA/SRV records for a host.
_AVOID_SYNC_DELAY_RANDOM_INTERVAL = (20, 120)

if TYPE_CHECKING:
from .._core import Zeroconf

Expand Down Expand Up @@ -455,6 +466,7 @@ async def async_request(
zc.async_send(out)
next_ = now + delay
delay *= 2
next_ += random.randint(*_AVOID_SYNC_DELAY_RANDOM_INTERVAL)

await zc.async_wait(min(next_, last) - now)
now = current_time_millis()
Expand Down