From 9c0bab297e9b36c2950ab5393473819e39b18781 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sun, 8 Jun 2014 14:38:09 +0800 Subject: [PATCH] more work --- shadowsocks/asyncdns.py | 4 ++++ shadowsocks/lru_cache.py | 48 ++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/shadowsocks/asyncdns.py b/shadowsocks/asyncdns.py index 426cbf5..259511b 100644 --- a/shadowsocks/asyncdns.py +++ b/shadowsocks/asyncdns.py @@ -298,6 +298,8 @@ class DNSResolver(object): callback((hostname, ip), None) if self._hostname_to_cb.__contains__(hostname): del self._hostname_to_cb[hostname] + if self._hostname_status.__contains__(hostname): + del self._hostname_status[hostname] def handle_events(self, events): for sock, fd, event in events: @@ -328,6 +330,8 @@ class DNSResolver(object): arr.remove(callback) if not arr: del self._hostname_to_cb[hostname] + if self._hostname_status.__contains__(hostname): + del self._hostname_status[hostname] def _send_req(self, hostname, qtype): logging.debug('resolving %s with type %d using server %s', hostname, diff --git a/shadowsocks/lru_cache.py b/shadowsocks/lru_cache.py index ce40d17..a26680c 100644 --- a/shadowsocks/lru_cache.py +++ b/shadowsocks/lru_cache.py @@ -13,52 +13,52 @@ class LRUCache(collections.MutableMapping): def __init__(self, timeout=60, close_callback=None, *args, **kwargs): self.timeout = timeout self.close_callback = close_callback - self.store = {} - self.time_to_keys = collections.defaultdict(list) - self.last_visits = [] + self._store = {} + self._time_to_keys = collections.defaultdict(list) + self._last_visits = [] self.update(dict(*args, **kwargs)) # use the free update to set keys def __getitem__(self, key): - "O(logm)" + # O(logm) t = time.time() - self.time_to_keys[t].append(key) - heapq.heappush(self.last_visits, t) - return self.store[key] + self._time_to_keys[t].append(key) + heapq.heappush(self._last_visits, t) + return self._store[key] def __setitem__(self, key, value): - "O(logm)" + # O(logm) t = time.time() - self.store[key] = value - self.time_to_keys[t].append(key) - heapq.heappush(self.last_visits, t) + self._store[key] = value + self._time_to_keys[t].append(key) + heapq.heappush(self._last_visits, t) def __delitem__(self, key): - "O(1)" - del self.store[key] + # O(1) + del self._store[key] def __iter__(self): - return iter(self.store) + return iter(self._store) def __len__(self): - return len(self.store) + return len(self._store) def sweep(self): - "O(m)" + # O(m) now = time.time() c = 0 - while len(self.last_visits) > 0: - least = self.last_visits[0] + while len(self._last_visits) > 0: + least = self._last_visits[0] if now - least <= self.timeout: break - for key in self.time_to_keys[least]: - heapq.heappop(self.last_visits) - if self.store.__contains__(key): - value = self.store[key] + 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] + del self._store[key] c += 1 - del self.time_to_keys[least] + del self._time_to_keys[least] if c: logging.debug('%d keys swept' % c)