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
Read from DNS cache if within TTL #677
Merged
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import collections | ||
import logging | ||
import random | ||
import time | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -31,7 +32,7 @@ | |
|
||
|
||
_Server = collections.namedtuple( | ||
"_Server", "priority weight host port" | ||
"_Server", "priority weight host port expires" | ||
) | ||
|
||
|
||
|
@@ -92,7 +93,8 @@ def __init__(self, reactor, service, domain, protocol="tcp", | |
host=domain, | ||
port=default_port, | ||
priority=0, | ||
weight=0 | ||
weight=0, | ||
expires=0, | ||
) | ||
else: | ||
self.default_server = None | ||
|
@@ -154,6 +156,12 @@ def connect(self, protocolFactory): | |
|
||
@defer.inlineCallbacks | ||
def resolve_service(service_name, dns_client=client, cache=SERVER_CACHE): | ||
cache_entry = cache.get(service_name, None) | ||
if cache_entry: | ||
if all(s.expires > int(time.time()) for s in cache_entry): | ||
servers = list(cache_entry) | ||
defer.returnValue(servers) | ||
|
||
servers = [] | ||
|
||
try: | ||
|
@@ -173,27 +181,26 @@ def resolve_service(service_name, dns_client=client, cache=SERVER_CACHE): | |
continue | ||
|
||
payload = answer.payload | ||
|
||
host = str(payload.target) | ||
srv_ttl = answer.ttl | ||
|
||
try: | ||
answers, _, _ = yield dns_client.lookupAddress(host) | ||
except DNSNameError: | ||
continue | ||
|
||
ips = [ | ||
answer.payload.dottedQuad() | ||
for answer in answers | ||
if answer.type == dns.A and answer.payload | ||
] | ||
|
||
for ip in ips: | ||
servers.append(_Server( | ||
host=ip, | ||
port=int(payload.port), | ||
priority=int(payload.priority), | ||
weight=int(payload.weight) | ||
)) | ||
for answer in answers: | ||
if answer.type == dns.A and answer.payload: | ||
ip = answer.payload.dottedQuad() | ||
host_ttl = min(srv_ttl, answer.ttl) | ||
|
||
servers.append(_Server( | ||
host=ip, | ||
port=int(payload.port), | ||
priority=int(payload.priority), | ||
weight=int(payload.weight), | ||
expires=int(time.time()) + host_ttl, | ||
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. As above wrt to passing in the current time, or using a mockable clock for getting the current time. |
||
)) | ||
|
||
servers.sort() | ||
cache[service_name] = list(servers) | ||
|
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 |
---|---|---|
|
@@ -69,8 +69,11 @@ def test_from_cache(self): | |
|
||
service_name = "test_service.examle.com" | ||
|
||
entry = Mock(spec_set=["expires"]) | ||
entry.expires = 999999999 | ||
|
||
cache = { | ||
service_name: [object()] | ||
service_name: [entry] | ||
} | ||
|
||
servers = yield resolve_service( | ||
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. Now that resolve_service takes a clock, it might be worth using a mock clock here? |
||
|
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.
Hmm, I tend to prefer to pass in the current time to these things to make testing easier.