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)
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,

48
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)

Loading…
Cancel
Save