From fe8032748e5f707ec889ed1d7fa2d78779bf039b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D1=80=D0=B5=D0=BD=D0=B1=D0=B5=D1=80=D0=B3=20?= =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=BA=20=28dell=20notebook=29?= Date: Mon, 25 Dec 2017 01:10:48 +0500 Subject: [PATCH] Fix DNSCache race-condition (#2620) --- aiohttp/connector.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/aiohttp/connector.py b/aiohttp/connector.py index f51641c4046..4205affaeae 100644 --- a/aiohttp/connector.py +++ b/aiohttp/connector.py @@ -7,7 +7,7 @@ from contextlib import suppress 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 @@ -547,7 +547,6 @@ class _DNSCacheTable: def __init__(self, ttl=None): self._addrs = {} - self._addrs_rr = {} self._timestamps = {} self._ttl = ttl @@ -560,28 +559,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: