This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Convert the well known resolver to async #8214
Merged
+53
−34
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
import logging | ||
import random | ||
import time | ||
from typing import Tuple | ||
from typing import Callable, Dict, Optional, Tuple | ||
|
||
import attr | ||
|
||
|
@@ -125,7 +125,9 @@ async def get_well_known(self, server_name: bytes) -> WellKnownLookupResult: | |
# requests for the same server in parallel? | ||
try: | ||
with Measure(self._clock, "get_well_known"): | ||
result, cache_period = await self._fetch_well_known(server_name) | ||
result, cache_period = await self._fetch_well_known( | ||
server_name | ||
) # type: Tuple[Optional[bytes], float] | ||
|
||
except _FetchWellKnownFailure as e: | ||
if prev_result and e.temporary: | ||
|
@@ -154,7 +156,7 @@ async def get_well_known(self, server_name: bytes) -> WellKnownLookupResult: | |
|
||
return WellKnownLookupResult(delegated_server=result) | ||
|
||
async def _fetch_well_known(self, server_name: bytes) -> Tuple[bytes, int]: | ||
async def _fetch_well_known(self, server_name: bytes) -> Tuple[bytes, float]: | ||
"""Actually fetch and parse a .well-known, without checking the cache | ||
|
||
Args: | ||
|
@@ -268,18 +270,21 @@ async def _make_well_known_request( | |
await self._clock.sleep(0.5) | ||
|
||
|
||
def _cache_period_from_headers(headers, time_now=time.time): | ||
def _cache_period_from_headers( | ||
headers: Headers, time_now: Callable[[], float] = time.time | ||
) -> Optional[float]: | ||
cache_controls = _parse_cache_control(headers) | ||
|
||
if b"no-store" in cache_controls: | ||
return 0 | ||
|
||
if b"max-age" in cache_controls: | ||
try: | ||
max_age = int(cache_controls[b"max-age"]) | ||
return max_age | ||
except ValueError: | ||
pass | ||
max_age = cache_controls[b"max-age"] | ||
if max_age: | ||
Comment on lines
+282
to
+283
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This extra check was to make mypy happy that we weren't doing |
||
try: | ||
return int(max_age) | ||
except ValueError: | ||
pass | ||
|
||
expires = headers.getRawHeaders(b"expires") | ||
if expires is not None: | ||
|
@@ -295,7 +300,7 @@ def _cache_period_from_headers(headers, time_now=time.time): | |
return None | ||
|
||
|
||
def _parse_cache_control(headers): | ||
def _parse_cache_control(headers: Headers) -> Dict[bytes, Optional[bytes]]: | ||
cache_controls = {} | ||
for hdr in headers.getRawHeaders(b"cache-control", []): | ||
for directive in hdr.split(b","): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come this is
Optional[bytes]
, whereas_fetch_well_known
supposedly returnsbytes
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is because
result
can be set toNone
below, so without it you get:This isn't saying the type of the return value of
_fetch_well_known
, but the types of the variablesresult
andcache_period
.