diff --git a/shadowsocks/lru_cache.py b/shadowsocks/lru_cache.py index 2a161df..9017f81 100644 --- a/shadowsocks/lru_cache.py +++ b/shadowsocks/lru_cache.py @@ -107,10 +107,10 @@ class LRUCache(collections.MutableMapping): logging.debug('%d keys swept' % c) return c < SWEEP_MAX_ITEMS - def clear(self): + def clear(self, keep): now = time.time() c = 0 - while c < SWEEP_MAX_ITEMS: + while len(self._keys_to_last_time) > keep: if len(self._keys_to_last_time) == 0: break for key in self._keys_to_last_time: diff --git a/shadowsocks/udprelay.py b/shadowsocks/udprelay.py index e4d515a..a4587b8 100644 --- a/shadowsocks/udprelay.py +++ b/shadowsocks/udprelay.py @@ -1138,6 +1138,8 @@ class UDPRelay(object): (common.to_str(server_addr), server_port, r_addr[0], r_addr[1])) + self._cache.clear(256) + if self._is_local: ref_iv = [encrypt.encrypt_new_iv(self._method)] self._protocol.obfs.server_info.iv = ref_iv[0] @@ -1344,7 +1346,7 @@ class UDPRelay(object): logging.debug('UDP close') self._closed = True if not next_tick: - self._cache.clear() + self._cache.clear(0) if self._eventloop: self._eventloop.remove_periodic(self.handle_periodic) self._eventloop.remove(self._server_socket)