Skip to content

Commit

Permalink
Fix DNSCache race-condition (#2620)
Browse files Browse the repository at this point in the history
  • Loading branch information
socketpair committed Dec 24, 2017
1 parent 2c169cb commit c79a81c
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from hashlib import md5, sha1, sha256
from http.cookies import SimpleCookie
from itertools import cycle, islice
from random import shuffle
from time import monotonic
from types import MappingProxyType

Expand Down Expand Up @@ -547,7 +548,6 @@ class _DNSCacheTable:

def __init__(self, ttl=None):
self._addrs = {}
self._addrs_rr = {}
self._timestamps = {}
self._ttl = ttl

Expand All @@ -560,28 +560,24 @@ def addrs(self):

def add(self, host, addrs):
self._addrs[host] = addrs
self._addrs_rr[host] = cycle(addrs)

if self._ttl:
self._timestamps[host] = monotonic()

def remove(self, host):
self._addrs.pop(host, None)
self._addrs_rr.pop(host, None)

if self._ttl:
self._timestamps.pop(host, None)

def clear(self):
self._addrs.clear()
self._addrs_rr.clear()
self._timestamps.clear()

def next_addrs(self, host):
# Return an iterator that will get at maximum as many addrs
# there are for the specific host starting from the last
# not itereated addr.
return islice(self._addrs_rr[host], len(self._addrs[host]))
addrs = self._addrs[host].copy()
shuffle(addrs)
return addrs

def expired(self, host):
if self._ttl is None:
Expand Down

0 comments on commit c79a81c

Please sign in to comment.