Skip to content

Commit 8c382ee

Browse files
jpbedebdraco
andauthored
fix: increase check time and add random wait to avoid service collisions (#1611)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent 02cc054 commit 8c382ee

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

‎src/zeroconf/_core.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import asyncio
2626
import logging
27+
import random
2728
import sys
2829
import threading
2930
from collections.abc import Awaitable
@@ -544,6 +545,11 @@ async def async_check_service(
544545
instance_name = instance_name_from_service_info(info, strict=strict)
545546
if cooperating_responders:
546547
return
548+
549+
# Wait a random amount of time up avoid collisions and avoid
550+
# a thundering herd when multiple services are started on the network
551+
await self.async_wait(random.randint(150, 250)) # noqa: S311
552+
547553
next_instance_number = 2
548554
next_time = now = current_time_millis()
549555
i = 0

‎src/zeroconf/const.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# Some timing constants
2929

3030
_UNREGISTER_TIME = 125 # ms
31-
_CHECK_TIME = 175 # ms
31+
_CHECK_TIME = 500 # ms
3232
_REGISTER_TIME = 225 # ms
3333
_LISTENER_TIME = 200 # ms
3434
_BROWSER_TIME = 10000 # ms

‎tests/test_asyncio.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,8 @@ def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
10811081
# The rest of the startup questions should have
10821082
# known answers
10831083
for answer_list in answers[1:-2]:
1084-
assert len(answer_list) == 1
1084+
# Allow 0 or 1 answers due to random delays and timing
1085+
assert len(answer_list) <= 1
10851086
# Once the TTL is reached, the last question should have no known answers
10861087
assert len(answers[-1]) == 0
10871088

0 commit comments

Comments
 (0)