Browse Source

LRUCache add 'clear' method

refine close
dev
BreakWa11 9 years ago
parent
commit
bb916eb705
  1. 19
      shadowsocks/lru_cache.py
  2. 2
      shadowsocks/tcprelay.py
  3. 3
      shadowsocks/udprelay.py

19
shadowsocks/lru_cache.py

@ -107,6 +107,25 @@ class LRUCache(collections.MutableMapping):
logging.debug('%d keys swept' % c) logging.debug('%d keys swept' % c)
return c < SWEEP_MAX_ITEMS 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(): def test():
c = LRUCache(timeout=0.3) c = LRUCache(timeout=0.3)

2
shadowsocks/tcprelay.py

@ -497,7 +497,7 @@ class TCPRelayHandler(object):
if len(addrs) == 0: if len(addrs) == 0:
raise Exception("getaddrinfo failed for %s:%d" % (ip, port)) raise Exception("getaddrinfo failed for %s:%d" % (ip, port))
af, socktype, proto, canonname, sa = addrs[0] 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: if common.to_str(sa[0]) in self._forbidden_iplist:
raise Exception('IP %s is in forbidden list, reject' % raise Exception('IP %s is in forbidden list, reject' %
common.to_str(sa[0])) common.to_str(sa[0]))

3
shadowsocks/udprelay.py

@ -1344,9 +1344,8 @@ class UDPRelay(object):
logging.debug('UDP close') logging.debug('UDP close')
self._closed = True self._closed = True
if not next_tick: if not next_tick:
self._cache.clear()
if self._eventloop: if self._eventloop:
self._eventloop.remove_periodic(self.handle_periodic) self._eventloop.remove_periodic(self.handle_periodic)
self._eventloop.remove(self._server_socket) self._eventloop.remove(self._server_socket)
self._server_socket.close() self._server_socket.close()
for client in list(self._cache.values()):
client.close()

Loading…
Cancel
Save