From bb916eb70535b88ba78fdb7500e84714f1911259 Mon Sep 17 00:00:00 2001 From: BreakWa11 Date: Mon, 16 May 2016 16:56:10 +0800 Subject: [PATCH] LRUCache add 'clear' method refine close --- shadowsocks/lru_cache.py | 19 +++++++++++++++++++ shadowsocks/tcprelay.py | 2 +- shadowsocks/udprelay.py | 3 +-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/shadowsocks/lru_cache.py b/shadowsocks/lru_cache.py index 32bab66..2a161df 100644 --- a/shadowsocks/lru_cache.py +++ b/shadowsocks/lru_cache.py @@ -107,6 +107,25 @@ class LRUCache(collections.MutableMapping): logging.debug('%d keys swept' % c) return c < SWEEP_MAX_ITEMS + def clear(self): + now = time.time() + c = 0 + while c < SWEEP_MAX_ITEMS: + if len(self._keys_to_last_time) == 0: + break + for key in self._keys_to_last_time: + break + last_t = self._keys_to_last_time[key] + value = self._store[key] + if self.close_callback is not None: + self.close_callback(value) + del self._store[key] + del self._keys_to_last_time[key] + c += 1 + if c: + logging.debug('%d keys swept' % c) + return c < SWEEP_MAX_ITEMS + def test(): c = LRUCache(timeout=0.3) diff --git a/shadowsocks/tcprelay.py b/shadowsocks/tcprelay.py index c77faf5..1398e80 100644 --- a/shadowsocks/tcprelay.py +++ b/shadowsocks/tcprelay.py @@ -497,7 +497,7 @@ class TCPRelayHandler(object): if len(addrs) == 0: raise Exception("getaddrinfo failed for %s:%d" % (ip, port)) af, socktype, proto, canonname, sa = addrs[0] - if self._forbidden_iplist: + if not self._remote_udp and self._forbidden_iplist: if common.to_str(sa[0]) in self._forbidden_iplist: raise Exception('IP %s is in forbidden list, reject' % common.to_str(sa[0])) diff --git a/shadowsocks/udprelay.py b/shadowsocks/udprelay.py index 287d4c8..e4d515a 100644 --- a/shadowsocks/udprelay.py +++ b/shadowsocks/udprelay.py @@ -1344,9 +1344,8 @@ class UDPRelay(object): logging.debug('UDP close') self._closed = True if not next_tick: + self._cache.clear() if self._eventloop: self._eventloop.remove_periodic(self.handle_periodic) self._eventloop.remove(self._server_socket) self._server_socket.close() - for client in list(self._cache.values()): - client.close()