Browse Source

more work

auth
clowwindy 11 years ago
parent
commit
9c0bab297e
  1. 4
      shadowsocks/asyncdns.py
  2. 48
      shadowsocks/lru_cache.py

4
shadowsocks/asyncdns.py

@ -298,6 +298,8 @@ class DNSResolver(object):
callback((hostname, ip), None) callback((hostname, ip), None)
if self._hostname_to_cb.__contains__(hostname): if self._hostname_to_cb.__contains__(hostname):
del self._hostname_to_cb[hostname] del self._hostname_to_cb[hostname]
if self._hostname_status.__contains__(hostname):
del self._hostname_status[hostname]
def handle_events(self, events): def handle_events(self, events):
for sock, fd, event in events: for sock, fd, event in events:
@ -328,6 +330,8 @@ class DNSResolver(object):
arr.remove(callback) arr.remove(callback)
if not arr: if not arr:
del self._hostname_to_cb[hostname] del self._hostname_to_cb[hostname]
if self._hostname_status.__contains__(hostname):
del self._hostname_status[hostname]
def _send_req(self, hostname, qtype): def _send_req(self, hostname, qtype):
logging.debug('resolving %s with type %d using server %s', hostname, logging.debug('resolving %s with type %d using server %s', hostname,

48
shadowsocks/lru_cache.py

@ -13,52 +13,52 @@ class LRUCache(collections.MutableMapping):
def __init__(self, timeout=60, close_callback=None, *args, **kwargs): def __init__(self, timeout=60, close_callback=None, *args, **kwargs):
self.timeout = timeout self.timeout = timeout
self.close_callback = close_callback self.close_callback = close_callback
self.store = {} self._store = {}
self.time_to_keys = collections.defaultdict(list) self._time_to_keys = collections.defaultdict(list)
self.last_visits = [] self._last_visits = []
self.update(dict(*args, **kwargs)) # use the free update to set keys self.update(dict(*args, **kwargs)) # use the free update to set keys
def __getitem__(self, key): def __getitem__(self, key):
"O(logm)" # O(logm)
t = time.time() t = time.time()
self.time_to_keys[t].append(key) self._time_to_keys[t].append(key)
heapq.heappush(self.last_visits, t) heapq.heappush(self._last_visits, t)
return self.store[key] return self._store[key]
def __setitem__(self, key, value): def __setitem__(self, key, value):
"O(logm)" # O(logm)
t = time.time() t = time.time()
self.store[key] = value self._store[key] = value
self.time_to_keys[t].append(key) self._time_to_keys[t].append(key)
heapq.heappush(self.last_visits, t) heapq.heappush(self._last_visits, t)
def __delitem__(self, key): def __delitem__(self, key):
"O(1)" # O(1)
del self.store[key] del self._store[key]
def __iter__(self): def __iter__(self):
return iter(self.store) return iter(self._store)
def __len__(self): def __len__(self):
return len(self.store) return len(self._store)
def sweep(self): def sweep(self):
"O(m)" # O(m)
now = time.time() now = time.time()
c = 0 c = 0
while len(self.last_visits) > 0: while len(self._last_visits) > 0:
least = self.last_visits[0] least = self._last_visits[0]
if now - least <= self.timeout: if now - least <= self.timeout:
break break
for key in self.time_to_keys[least]: for key in self._time_to_keys[least]:
heapq.heappop(self.last_visits) heapq.heappop(self._last_visits)
if self.store.__contains__(key): if self._store.__contains__(key):
value = self.store[key] value = self._store[key]
if self.close_callback is not None: if self.close_callback is not None:
self.close_callback(value) self.close_callback(value)
del self.store[key] del self._store[key]
c += 1 c += 1
del self.time_to_keys[least] del self._time_to_keys[least]
if c: if c:
logging.debug('%d keys swept' % c) logging.debug('%d keys swept' % c)

Loading…
Cancel
Save