Switch names of TimeoutDict and ExpiringDict.

For consistency, as TimeoutQueue has guaranteed deletion after the timeout,
like the new TimeoutDict (ex-ExpiringDict).
This commit is contained in:
Valentin Lorentz 2020-05-10 08:42:25 +02:00
parent d205a9b5f8
commit 759fca5eba
4 changed files with 21 additions and 19 deletions

View File

@ -122,7 +122,7 @@ class Fediverse(callbacks.PluginRegexp):
def __init__(self, irc): def __init__(self, irc):
super().__init__(irc) super().__init__(irc)
self._startHttp() self._startHttp()
self._actor_cache = utils.structures.ExpiringDict(timeout=600) self._actor_cache = utils.structures.TimeoutDict(timeout=600)
def _startHttp(self): def _startHttp(self):
callback = FediverseHttp() callback = FediverseHttp()

View File

@ -58,7 +58,7 @@ from . import conf, ircdb, ircmsgs, ircutils, log, utils, world
from .drivers import Server from .drivers import Server
from .utils.str import rsplit from .utils.str import rsplit
from .utils.iter import chain from .utils.iter import chain
from .utils.structures import smallqueue, RingBuffer, TimeoutDict from .utils.structures import smallqueue, RingBuffer, ExpiringDict
MAX_LINE_SIZE = 512 # Including \r\n, but excluding server_tags MAX_LINE_SIZE = 512 # Including \r\n, but excluding server_tags
@ -557,7 +557,7 @@ class IrcState(IrcCommandDispatcher, log.Firewalled):
# Batches should always finish and be way shorter than 3600s, but # Batches should always finish and be way shorter than 3600s, but
# let's just make sure to avoid leaking memory. # let's just make sure to avoid leaking memory.
self.batches = TimeoutDict(timeout=3600) self.batches = ExpiringDict(timeout=3600)
def reset(self): def reset(self):
"""Resets the state to normal, unconnected state.""" """Resets the state to normal, unconnected state."""

View File

@ -307,6 +307,8 @@ class smallqueue(list):
class TimeoutQueue(object): class TimeoutQueue(object):
"""A queue whose elements are dropped after a certain time."""
__slots__ = ('queue', 'timeout') __slots__ = ('queue', 'timeout')
def __init__(self, timeout, queue=None): def __init__(self, timeout, queue=None):
if queue is None: if queue is None:
@ -458,9 +460,9 @@ class CacheDict(collections.abc.MutableMapping):
return len(self.d) return len(self.d)
class TimeoutDict(collections.abc.MutableMapping): class ExpiringDict(collections.abc.MutableMapping):
"""An efficient dictionary that MAY drop its items when they are too old. """An efficient dictionary that MAY drop its items when they are too old.
For guaranteed expiry, use ExpiringDict. For guaranteed expiry, use TimeoutDict.
Currently, this is implemented by internally alternating two "generation" Currently, this is implemented by internally alternating two "generation"
dicts, which are dropped after a certain time.""" dicts, which are dropped after a certain time."""
@ -478,7 +480,7 @@ class TimeoutDict(collections.abc.MutableMapping):
return (self.__class__, (self.timeout, dict(self))) return (self.__class__, (self.timeout, dict(self)))
def __repr__(self): def __repr__(self):
return 'TimeoutDict(%s, %r)' % (self.timeout, dict(self)) return 'ExpiringDict(%s, %r)' % (self.timeout, dict(self))
def __getitem__(self, key): def __getitem__(self, key):
try: try:
@ -534,11 +536,11 @@ class TimeoutDict(collections.abc.MutableMapping):
return len(set(self.new_gen.keys()) | set(self.old_gen.keys())) return len(set(self.new_gen.keys()) | set(self.old_gen.keys()))
class ExpiringDict: # Don't inherit from MutableMapping: not thread-safe class TimeoutDict: # Don't inherit from MutableMapping: not thread-safe
"""A dictionary that drops its items after they have been in the dict """A dictionary that drops its items after they have been in the dict
for a certain time. for a certain time.
Use TimeoutDict for a more efficient implementation that doesn't require Use ExpiringDict for a more efficient implementation that doesn't require
guaranteed timeout. guaranteed timeout.
""" """
__slots__ = ('_lock', 'd', 'timeout') __slots__ = ('_lock', 'd', 'timeout')
@ -554,7 +556,7 @@ class ExpiringDict: # Don't inherit from MutableMapping: not thread-safe
return (self.__class__, (self.timeout, dict(self))) return (self.__class__, (self.timeout, dict(self)))
def __repr__(self): def __repr__(self):
return 'ExpiringDict(%s, %r)' % (self.timeout, dict(self)) return 'TimeoutDict(%s, %r)' % (self.timeout, dict(self))
def __getitem__(self, key): def __getitem__(self, key):
with self._lock: with self._lock:

View File

@ -1166,16 +1166,16 @@ class TestCacheDict(SupyTestCase):
self.assertTrue(i in d) self.assertTrue(i in d)
self.assertTrue(d[i] == i) self.assertTrue(d[i] == i)
class TestTimeoutDict(SupyTestCase): class TestExpiringDict(SupyTestCase):
def testInit(self): def testInit(self):
d = TimeoutDict(10) d = ExpiringDict(10)
self.assertEqual(dict(d), {}) self.assertEqual(dict(d), {})
d['foo'] = 'bar' d['foo'] = 'bar'
d['baz'] = 'qux' d['baz'] = 'qux'
self.assertEqual(dict(d), {'foo': 'bar', 'baz': 'qux'}) self.assertEqual(dict(d), {'foo': 'bar', 'baz': 'qux'})
def testExpire(self): def testExpire(self):
d = TimeoutDict(10) d = ExpiringDict(10)
self.assertEqual(dict(d), {}) self.assertEqual(dict(d), {})
d['foo'] = 'bar' d['foo'] = 'bar'
timeFastForward(11) timeFastForward(11)
@ -1189,8 +1189,8 @@ class TestTimeoutDict(SupyTestCase):
self.assertEqual(dict(d), {'baz': 'qux', 'quux': 42}) self.assertEqual(dict(d), {'baz': 'qux', 'quux': 42})
def testEquality(self): def testEquality(self):
d1 = TimeoutDict(10) d1 = ExpiringDict(10)
d2 = TimeoutDict(10) d2 = ExpiringDict(10)
self.assertEqual(d1, d2) self.assertEqual(d1, d2)
d1['foo'] = 'bar' d1['foo'] = 'bar'
@ -1208,16 +1208,16 @@ class TestTimeoutDict(SupyTestCase):
self.assertEqual(d1, d2) self.assertEqual(d1, d2)
class TestExpiringDict(SupyTestCase): class TestTimeoutDict(SupyTestCase):
def testInit(self): def testInit(self):
d = ExpiringDict(10) d = TimeoutDict(10)
self.assertEqual(dict(d), {}) self.assertEqual(dict(d), {})
d['foo'] = 'bar' d['foo'] = 'bar'
d['baz'] = 'qux' d['baz'] = 'qux'
self.assertEqual(dict(d), {'foo': 'bar', 'baz': 'qux'}) self.assertEqual(dict(d), {'foo': 'bar', 'baz': 'qux'})
def testExpire(self): def testExpire(self):
d = ExpiringDict(10) d = TimeoutDict(10)
self.assertEqual(dict(d), {}) self.assertEqual(dict(d), {})
d['foo'] = 'bar' d['foo'] = 'bar'
timeFastForward(11) timeFastForward(11)
@ -1231,8 +1231,8 @@ class TestExpiringDict(SupyTestCase):
self.assertEqual(dict(d), {'quux': 42}) self.assertEqual(dict(d), {'quux': 42})
def testEquality(self): def testEquality(self):
d1 = ExpiringDict(10) d1 = TimeoutDict(10)
d2 = ExpiringDict(10) d2 = TimeoutDict(10)
self.assertEqual(d1, d2) self.assertEqual(d1, d2)
d1['foo'] = 'bar' d1['foo'] = 'bar'