fix: increase check time and add random wait to avoid service collisi… · python-zeroconf/python-zeroconf@8c382ee

3 files changed

lines changed

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

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

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