Skip to content

Commit eb821d3

Browse files
committed
Share unique DNS query tool instead of rebuild.
Indeed, before this patch, we were constantly rebuilding a new DNS query tool over and over before any HTTP request. This patch fixes that by building one single DNS Query tool for the lifetime of the requester object. Once a requester object is initialized the DNS query tool it generates will be shared accross all possible HTTP Adapters.
1 parent df93bc2 commit eb821d3

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

PyFunceble/query/requests/adapter/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ def __init__(self, *args, **kwargs):
8080
total=kwargs["max_retries"], respect_retry_after_header=False
8181
)
8282

83-
self.dns_query_tool = DNSQueryTool().guess_all_settings()
83+
if "dns_query_tool" in kwargs:
84+
self.dns_query_tool = kwargs["dns_query_tool"]
85+
del kwargs["dns_query_tool"]
86+
else:
87+
self.dns_query_tool = DNSQueryTool()
8488

8589
super().__init__(*args, **kwargs)
8690

PyFunceble/query/requests/requester.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import PyFunceble.facility
6262
import PyFunceble.storage
6363
from PyFunceble.dataset.user_agent import UserAgentDataset
64+
from PyFunceble.query.dns.query_tool import DNSQueryTool
6465
from PyFunceble.query.requests.adapter.http import RequestHTTPAdapter
6566
from PyFunceble.query.requests.adapter.https import RequestHTTPSAdapter
6667

@@ -91,6 +92,7 @@ class Requester:
9192
_max_redirects: int = 60
9293

9394
session: Optional[requests.Session] = None
95+
dns_query_tool: Optional[DNSQueryTool] = None
9496

9597
def __init__(
9698
self,
@@ -99,6 +101,7 @@ def __init__(
99101
verify_certificate: Optional[bool] = None,
100102
timeout: Optional[float] = None,
101103
max_redirects: Optional[int] = None,
104+
dns_query_tool: Optional[DNSQueryTool] = None,
102105
) -> None:
103106
if max_retries is not None:
104107
self.max_retries = max_retries
@@ -116,6 +119,11 @@ def __init__(
116119
if max_redirects is not None:
117120
self.max_redirects = max_redirects
118121

122+
if dns_query_tool is not None:
123+
self.dns_query_tool = dns_query_tool
124+
else:
125+
self.dns_query_tool = DNSQueryTool()
126+
119127
self.session = self.get_session()
120128

121129
warnings.simplefilter("ignore", urllib3.exceptions.InsecureRequestWarning)
@@ -408,11 +416,19 @@ def get_session(self) -> requests.Session:
408416

409417
session.mount(
410418
"https://",
411-
RequestHTTPSAdapter(max_retries=self.max_retries, timeout=self.timeout),
419+
RequestHTTPSAdapter(
420+
max_retries=self.max_retries,
421+
timeout=self.timeout,
422+
dns_query_tool=self.dns_query_tool,
423+
),
412424
)
413425
session.mount(
414426
"http://",
415-
RequestHTTPAdapter(max_retries=self.max_retries, timeout=self.timeout),
427+
RequestHTTPAdapter(
428+
max_retries=self.max_retries,
429+
timeout=self.timeout,
430+
dns_query_tool=self.dns_query_tool,
431+
),
416432
)
417433

418434
custom_headers = {"User-Agent": UserAgentDataset().get_latest()}

0 commit comments

Comments
 (0)