added : timed_cache sub-module

This commit is contained in:
Mystical
2025-03-04 20:27:09 +05:30
committed by Paul Robinson
parent 0fa8711ca7
commit e5f3e88134
12 changed files with 158 additions and 20 deletions

View File

@ -18,9 +18,6 @@ from typing import (
)
import base58
from lru import (
LRU,
)
import trio
from libp2p.abc import (
@ -56,6 +53,9 @@ from libp2p.network.stream.exceptions import (
from libp2p.peer.id import (
ID,
)
from libp2p.timed_cache.last_seen_cache import (
LastSeenCache,
)
from libp2p.tools.async_service import (
Service,
)
@ -112,7 +112,7 @@ class Pubsub(Service, IPubsub):
peer_receive_channel: trio.MemoryReceiveChannel[ID]
dead_peer_receive_channel: trio.MemoryReceiveChannel[ID]
seen_messages: LRU[bytes, bool]
seen_messages: LastSeenCache
subscribed_topics_send: dict[str, trio.MemorySendChannel[rpc_pb2.Message]]
subscribed_topics_receive: dict[str, TrioSubscriptionAPI]
@ -136,6 +136,7 @@ class Pubsub(Service, IPubsub):
host: IHost,
router: IPubsubRouter,
cache_size: int = None,
seen_ttl: int = 120,
strict_signing: bool = True,
msg_id_constructor: Callable[
[rpc_pb2.Message], bytes
@ -187,7 +188,7 @@ class Pubsub(Service, IPubsub):
else:
self.sign_key = None
self.seen_messages = LRU(self.cache_size)
self.seen_messages = LastSeenCache(seen_ttl)
# Map of topics we are subscribed to blocking queues
# for when the given topic receives a message
@ -662,11 +663,11 @@ class Pubsub(Service, IPubsub):
def _is_msg_seen(self, msg: rpc_pb2.Message) -> bool:
msg_id = self._msg_id_constructor(msg)
return msg_id in self.seen_messages
return self.seen_messages.has(msg_id)
def _mark_msg_seen(self, msg: rpc_pb2.Message) -> None:
msg_id = self._msg_id_constructor(msg)
self.seen_messages[msg_id] = True
self.seen_messages.add(msg_id)
def _is_subscribed_to_msg(self, msg: rpc_pb2.Message) -> bool:
return any(topic in self.topic_ids for topic in msg.topicIDs)