Skip to content

Commit

Permalink
fix duplicated close in LRUCache
Browse files Browse the repository at this point in the history
close #324
  • Loading branch information
clowwindy committed May 19, 2015
1 parent 893d21d commit 082c8a8
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion shadowsocks/lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def sweep(self):
# O(m)
now = time.time()
c = 0
values_closed = list() # list is cheaper to create
while len(self._last_visits) > 0:
least = self._last_visits[0]
if now - least <= self.timeout:
Expand All @@ -83,7 +84,9 @@ def sweep(self):
if key in self._store:
if now - self._keys_to_last_time[key] > self.timeout:
value = self._store[key]
self.close_callback(value)
if value not in values_closed:
self.close_callback(value)
values_closed.append(value)
for key in self._time_to_keys[least]:
self._last_visits.popleft()
if key in self._store:
Expand Down Expand Up @@ -126,5 +129,21 @@ def test():
assert 'a' not in c
assert 'b' not in c

global close_cb_called
close_cb_called = False

def close_cb(t):
global close_cb_called
assert not close_cb_called
close_cb_called = True

c = LRUCache(timeout=0.1, close_callback=close_cb)
c['s'] = 1
c['s']
time.sleep(0.1)
c['s']
time.sleep(0.3)
c.sweep()

if __name__ == '__main__':
test()

0 comments on commit 082c8a8

Please sign in to comment.