2020 USA
2121"""
2222
23- from typing import Dict , Set , Tuple
23+ from typing import Dict , List , Set , Tuple
2424
2525from ._dns import DNSQuestion , DNSRecord
2626from .const import _DUPLICATE_QUESTION_INTERVAL
2727
2828# The QuestionHistory is used to implement Duplicate Question Suppression
2929# https://datatracker.ietf.org/doc/html/rfc6762#section-7.3
3030
31+ _float = float
32+
3133
3234class QuestionHistory :
35+ """Remember questions and known answers."""
36+
3337 def __init__ (self ) -> None :
38+ """Init a new QuestionHistory."""
3439 self ._history : Dict [DNSQuestion , Tuple [float , Set [DNSRecord ]]] = {}
3540
36- def add_question_at_time (self , question : DNSQuestion , now : float , known_answers : Set [DNSRecord ]) -> None :
41+ def add_question_at_time (self , question : DNSQuestion , now : _float , known_answers : Set [DNSRecord ]) -> None :
3742 """Remember a question with known answers."""
3843 self ._history [question ] = (now , known_answers )
3944
40- def suppresses (self , question : DNSQuestion , now : float , known_answers : Set [DNSRecord ]) -> bool :
45+ def suppresses (self , question : DNSQuestion , now : _float , known_answers : Set [DNSRecord ]) -> bool :
4146 """Check to see if a question should be suppressed.
4247
4348 https://datatracker.ietf.org/doc/html/rfc6762#section-7.3
@@ -59,12 +64,16 @@ def suppresses(self, question: DNSQuestion, now: float, known_answers: Set[DNSRe
5964 return False
6065 return True
6166
62- def async_expire (self , now : float ) -> None :
67+ def async_expire (self , now : _float ) -> None :
6368 """Expire the history of old questions."""
64- removes = [
65- question
66- for question , now_known_answers in self . _history . items ()
67- if now - now_known_answers [ 0 ] > _DUPLICATE_QUESTION_INTERVAL
68- ]
69+ removes : List [ DNSQuestion ] = []
70+ for question , now_known_answers in self . _history . items ():
71+ than , _ = now_known_answers
72+ if now - than > _DUPLICATE_QUESTION_INTERVAL :
73+ removes . append ( question )
6974 for question in removes :
7075 del self ._history [question ]
76+
77+ def clear (self ) -> None :
78+ """Clear the history."""
79+ self ._history .clear ()
0 commit comments