-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy path_history.py
More file actions
70 lines (57 loc) · 2.85 KB
/
_history.py
File metadata and controls
70 lines (57 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
""" Multicast DNS Service Discovery for Python, v0.14-wmcbrine
Copyright 2003 Paul Scott-Murphy, 2014 William McBrine
This module provides a framework for the use of DNS Service Discovery
using IP multicast.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
"""
from typing import Dict, Set, Tuple
from ._dns import DNSQuestion, DNSRecord
from .const import _DUPLICATE_QUESTION_INTERVAL
# The QuestionHistory is used to implement Duplicate Question Suppression
# https://datatracker.ietf.org/doc/html/rfc6762#section-7.3
class QuestionHistory:
def __init__(self) -> None:
self._history: Dict[DNSQuestion, Tuple[float, Set[DNSRecord]]] = {}
def add_question_at_time(self, question: DNSQuestion, now: float, known_answers: Set[DNSRecord]) -> None:
"""Remember a question with known answers."""
self._history[question] = (now, known_answers)
def suppresses(self, question: DNSQuestion, now: float, known_answers: Set[DNSRecord]) -> bool:
"""Check to see if a question should be suppressed.
https://datatracker.ietf.org/doc/html/rfc6762#section-7.3
When multiple queriers on the network are querying
for the same resource records, there is no need for them to all be
repeatedly asking the same question.
"""
previous_question = self._history.get(question)
# There was not previous question in the history
if not previous_question:
return False
than, previous_known_answers = previous_question
# The last question was older than 999ms
if now - than > _DUPLICATE_QUESTION_INTERVAL:
return False
# The last question has more known answers than
# we knew so we have to ask
if previous_known_answers - known_answers:
return False
return True
def async_expire(self, now: float) -> None:
"""Expire the history of old questions."""
removes = [
question
for question, now_known_answers in self._history.items()
if now - now_known_answers[0] > _DUPLICATE_QUESTION_INTERVAL
]
for question in removes:
del self._history[question]