Skip to content

Commit 76e0b05

Browse files
authored
Make ServiceInfo first question QU (#852)
- We want an immediate response when making a request with ServiceInfo by asking a QU question, most responders will not delay the response and respond right away to our question. This also improves compatibility with split networks as we may not have been able to see the response otherwise. If the responder has not multicast the record recently it may still choose to do so in addition to responding via unicast - Reduces traffic when there are multiple zeroconf instances running on the network running ServiceBrowsers - If we don't get an answer on the first try, we ask a QM question in the event we can't receive a unicast response for some reason - This change puts ServiceInfo inline with ServiceBrowser which also asks the first question as QU since ServiceInfo is commonly called from ServiceBrowser callbacks closes #851
1 parent 8c9d1d8 commit 76e0b05

3 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎tests/services/test_info.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,8 @@ def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT):
682682
zeroconf.close()
683683

684684

685-
def test_asking_qm_questions_are_default():
686-
"""Verify default is QM questions."""
685+
def test_asking_qm_questions():
686+
"""Verify explictly asking QM questions."""
687687
type_ = "_quservice._tcp.local."
688688
zeroconf = r.Zeroconf(interfaces=['127.0.0.1'])
689689

@@ -701,6 +701,6 @@ def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT):
701701

702702
# patch the zeroconf send
703703
with patch.object(zeroconf, "async_send", send):
704-
zeroconf.get_service_info(f"name.{type_}", type_, 500)
704+
zeroconf.get_service_info(f"name.{type_}", type_, 500, question_type=r.DNSQuestionType.QM)
705705
assert first_outgoing.questions[0].unicast == False
706706
zeroconf.close()

‎tests/test_aio.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,3 +776,50 @@ def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT, v6_flow_scope=()):
776776
assert service_removed.is_set()
777777
await browser.async_cancel()
778778
await aiozc.async_close()
779+
780+
781+
@pytest.mark.asyncio
782+
async def test_info_asking_default_is_asking_qm_questions_after_the_first_qu():
783+
"""Verify the service info first question is QU and subsequent ones are QM questions."""
784+
type_ = "_quservice._tcp.local."
785+
aiozc = AsyncZeroconf(interfaces=['127.0.0.1'])
786+
zeroconf_info = aiozc.zeroconf
787+
788+
name = "xxxyyy"
789+
registration_name = "%s.%s" % (name, type_)
790+
791+
desc = {'path': '/~paulsm/'}
792+
info = ServiceInfo(
793+
type_, registration_name, 80, 0, 0, desc, "ash-2.local.", addresses=[socket.inet_aton("10.0.1.2")]
794+
)
795+
796+
zeroconf_info.registry.add(info)
797+
798+
# we are going to patch the zeroconf send to check query transmission
799+
old_send = zeroconf_info.async_send
800+
801+
first_outgoing = None
802+
second_outgoing = None
803+
804+
def send(out, addr=const._MDNS_ADDR, port=const._MDNS_PORT):
805+
"""Sends an outgoing packet."""
806+
nonlocal first_outgoing
807+
nonlocal second_outgoing
808+
if out.questions:
809+
if first_outgoing is not None and second_outgoing is None:
810+
second_outgoing = out
811+
if first_outgoing is None:
812+
first_outgoing = out
813+
old_send(out, addr=addr, port=port)
814+
815+
# patch the zeroconf send
816+
with patch.object(zeroconf_info, "async_send", send):
817+
aiosinfo = AsyncServiceInfo(type_, registration_name)
818+
# Patch _is_complete so we send multiple times
819+
with patch("zeroconf.aio.AsyncServiceInfo._is_complete", False):
820+
await aiosinfo.async_request(aiozc.zeroconf, 1200)
821+
try:
822+
assert first_outgoing.questions[0].unicast == True
823+
assert second_outgoing.questions[0].unicast == False
824+
finally:
825+
await aiozc.async_close()

‎zeroconf/_services/info.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ async def async_request(
438438
if self.load_from_cache(zc):
439439
return True
440440

441+
first_request = True
441442
now = current_time_millis()
442443
delay = _LISTENER_TIME
443444
next_ = now
@@ -449,7 +450,10 @@ async def async_request(
449450
if last <= now:
450451
return False
451452
if next_ <= now:
452-
out = self.generate_request_query(zc, now, question_type)
453+
out = self.generate_request_query(
454+
zc, now, question_type or DNSQuestionType.QU if first_request else DNSQuestionType.QM
455+
)
456+
first_request = False
453457
if not out.questions:
454458
return self.load_from_cache(zc)
455459
zc.async_send(out)

0 commit comments

Comments
 (0)