From d4458bceb766d35849bfeac54a3df4281d107b9a Mon Sep 17 00:00:00 2001 From: clowwindy Date: Mon, 23 Jun 2014 21:35:23 +0800 Subject: [PATCH] add timeout support in DNS --- shadowsocks/asyncdns.py | 33 +++++++++++++++++++++++---------- shadowsocks/lru_cache.py | 9 +++++---- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/shadowsocks/asyncdns.py b/shadowsocks/asyncdns.py index f188faa..ce0076d 100644 --- a/shadowsocks/asyncdns.py +++ b/shadowsocks/asyncdns.py @@ -32,7 +32,7 @@ import lru_cache import eventloop -CACHE_SWEEP_INTERVAL = 30 +CACHE_SWEEP_INTERVAL = 10 VALID_HOSTNAME = re.compile("(?!-)[A-Z\d-]{1,63}(? CACHE_SWEEP_INTERVAL: self._cache.sweep() + self._cb_to_hostname.sweep() self._last_time = now def remove_callback(self, callback): diff --git a/shadowsocks/lru_cache.py b/shadowsocks/lru_cache.py index a26680c..1313af5 100644 --- a/shadowsocks/lru_cache.py +++ b/shadowsocks/lru_cache.py @@ -50,13 +50,14 @@ class LRUCache(collections.MutableMapping): least = self._last_visits[0] if now - least <= self.timeout: break + if self.close_callback is not None: + for key in self._time_to_keys[least]: + if self._store.__contains__(key): + value = self._store[key] + self.close_callback(value) for key in self._time_to_keys[least]: heapq.heappop(self._last_visits) if self._store.__contains__(key): - value = self._store[key] - if self.close_callback is not None: - self.close_callback(value) - del self._store[key] c += 1 del self._time_to_keys[least]