3.0.ipv8.requestcache
Attributes
Classes
Module Contents
- class 3.0.ipv8.requestcache.NumberCache(request_cache: RequestCache, prefix: str, number: int)
A cache for state information that is uniquely identified by a prefix and a number.
- _logger
- _prefix
- _number
- register_future(future: asyncio.Future, on_timeout: object | None = None) None
Register a future for this Cache that will be canceled when this Cache times out.
- Parameters:
future – the future to register for this instance
on_timeout – The value to which the future is to be set when a timeout occurs. If the value is an instance of Exception, future.set_exception will be called instead of future.set_result
- class 3.0.ipv8.requestcache.RandomNumberCache(request_cache: RequestCache, prefix: str)
Bases:
NumberCacheA cache with a randomly generated number.
- classmethod find_unclaimed_identifier(request_cache: RequestCache, prefix: str) int
Generate a random number for use with this cache.
- class 3.0.ipv8.requestcache.CacheWithName
Bases:
typing_extensions.ProtocolAll caches with a
nameattribute.
- 3.0.ipv8.requestcache.CacheTypeVar
- 3.0.ipv8.requestcache.ACT
- class 3.0.ipv8.requestcache.NumberCacheWithName(request_cache: RequestCache, prefix: str, number: int)
Bases:
NumberCache,CacheWithName,abc.ABCA NumberCache with a
nameattribute.
- class 3.0.ipv8.requestcache.RandomNumberCacheWithName(request_cache: RequestCache, prefix: str)
Bases:
RandomNumberCache,CacheWithName,abc.ABCA RandomNumberCache with a
nameattribute.
- class 3.0.ipv8.requestcache.RequestCache
Bases:
3.0.ipv8.taskmanager.TaskManagerManager for NumberCache caches.
- _logger
- _identifiers: dict[str, NumberCache]
- lock
- _shutdown = False
- _timeout_override: float | None = None
If not None, this specifies the timeout to use instead of the one defined in the
timeout_delayof aNumberCache. This is used internally forpassthrough(), don’t modify this directly!
- _timeout_filters: collections.abc.Iterable[type[NumberCache]] | None = None
If not None, this specifies the
NumberCache(sub)classes to apply the timeout override for. This is used internally forpassthrough(), don’t modify this directly!
- add(cache: ACT) ACT | None
Add CACHE into this RequestCache instance.
Returns CACHE when CACHE.identifier was not yet added, otherwise returns None.
- has(prefix: str, number: int) bool
- has(prefix: type[CacheTypeVar], number: int) bool
Returns True when IDENTIFIER is part of this RequestCache.
- wait_for(prefix: str, number: int, timeout: float | None = None) asyncio.Future[NumberCache]
- wait_for(prefix: type[CacheTypeVar], number: int, timeout: float | None = None) asyncio.Future[CacheTypeVar]
Returns a future that fires if or when the given cache is registered.
- get(prefix: str, number: int) NumberCache | None
- get(prefix: type[CacheTypeVar], number: int) CacheTypeVar | None
Returns the Cache associated with IDENTIFIER when it exists, otherwise returns None.
- pop(prefix: str, number: int) NumberCache
- pop(prefix: type[CacheTypeVar], number: int) CacheTypeVar
Returns the Cache associated with IDENTIFIER, and removes it from this RequestCache, when it exists, otherwise raises a KeyError exception.
- passthrough(cls_filter: type[NumberCache] | None = None, *filters: type[NumberCache], timeout: float = 0.0) collections.abc.Generator
A contextmanager that overwrites the timeout_delay of added NumberCaches in its scope. This can be used to shorten or eliminate timeouts of external code.
— Example 1: Eliminating timeouts, regardless of
cache.timeout_delay—with request_cache.passthrough(): request_cache.add(cache) # This will instantly timeout (once the main thread is yielded). with request_cache.passthrough(): # Any internal call to request_cache.add() will also be instantly timed out. await some_function_that_uses_request_cache()
— Example 2: Modifying timeouts, regardless of
cache.timeout_delay—with request_cache.passthrough(timeout=0.1): request_cache.add(cache) # This will timeout after 0.1 seconds.
— Example 3: Filtering for specific classes —
# Only MyCacheClass, MyOtherCacheClass, YetAnotherCacheClass will have their timeout changed to 4 seconds. with request_cache.passthrough(MyCacheClass, MyOtherCacheClass, YetAnotherCacheClass, timeout=4.0): request_cache.add(cache)
- Parameters:
cls_filter – An optional class filter to specify which classes the timeout override needs to apply to.
filters – Additional class filters to specify which classes the timeout override needs to apply to.
timeout – The timeout in seconds to use for the
NumberCacheinstances this applies to.
- Returns:
A context manager (compatible with
with).
- _on_timeout(cache: NumberCache) None
Called CACHE.timeout_delay seconds after CACHE was added to this RequestCache.
_on_timeout is called for every Cache, except when it has been popped before the timeout expires. When called _on_timeout will CACHE.on_timeout().
- clear() list[asyncio.Future]
Clear the cache, canceling all pending tasks.