Skip to content
Merged
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
9 changes: 6 additions & 3 deletions src/zeroconf/_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import itertools
import random
from collections import deque
from operator import attrgetter
from typing import (
TYPE_CHECKING,
Dict,
Expand Down Expand Up @@ -71,6 +72,8 @@
_MULTICAST_DELAY_RANDOM_INTERVAL = (20, 120)
_RESPOND_IMMEDIATE_TYPES = {_TYPE_NSEC, _TYPE_SRV, *_ADDRESS_RECORD_TYPES}

NAME_GETTER = attrgetter('name')


class QuestionAnswers(NamedTuple):
ucast: _AnswerWithAdditionalsType
Expand Down Expand Up @@ -109,13 +112,13 @@ def construct_outgoing_unicast_answers(

def _add_answers_additionals(out: DNSOutgoing, answers: _AnswerWithAdditionalsType) -> None:
# Find additionals and suppress any additionals that are already in answers
sending: Set[DNSRecord] = set(answers.keys())
sending: Set[DNSRecord] = set(answers)
# Answers are sorted to group names together to increase the chance
# that similar names will end up in the same packet and can reduce the
# overall size of the outgoing response via name compression
for answer, additionals in sorted(answers.items(), key=lambda kv: kv[0].name):
for answer in sorted(answers, key=NAME_GETTER):
out.add_answer_at_time(answer, 0)
for additional in additionals:
for additional in answers[answer]:
if additional not in sending:
out.add_additional_answer(additional)
sending.add(additional)
Expand Down