mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
added dedicated test file and moved timed_cache to tools
This commit is contained in:
@ -88,7 +88,7 @@ class GossipSub(IPubsubRouter, Service):
|
||||
degree: int,
|
||||
degree_low: int,
|
||||
degree_high: int,
|
||||
time_to_live: int,
|
||||
time_to_live: int = 60,
|
||||
gossip_window: int = 3,
|
||||
gossip_history: int = 5,
|
||||
heartbeat_initial_delay: float = 0.1,
|
||||
|
||||
@ -11,11 +11,11 @@ import hashlib
|
||||
import logging
|
||||
import time
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Callable,
|
||||
NamedTuple,
|
||||
cast,
|
||||
)
|
||||
from typing import Any # noqa: F401
|
||||
|
||||
import base58
|
||||
import trio
|
||||
@ -26,6 +26,7 @@ from libp2p.abc import (
|
||||
IPubsub,
|
||||
ISubscriptionAPI,
|
||||
)
|
||||
from libp2p.abc import IPubsubRouter # noqa: F401
|
||||
from libp2p.crypto.keys import (
|
||||
PrivateKey,
|
||||
)
|
||||
@ -53,12 +54,12 @@ 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,
|
||||
)
|
||||
from libp2p.tools.timed_cache.last_seen_cache import (
|
||||
LastSeenCache,
|
||||
)
|
||||
from libp2p.utils import (
|
||||
encode_varint_prefixed,
|
||||
read_varint_prefixed_bytes,
|
||||
@ -78,12 +79,6 @@ from .validators import (
|
||||
signature_validator,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Any # noqa: F401
|
||||
|
||||
from .abc import IPubsubRouter # noqa: F401
|
||||
|
||||
|
||||
# Ref: https://github.com/libp2p/go-libp2p-pubsub/blob/40e1c94708658b155f30cf99e4574f384756d83c/topic.go#L97 # noqa: E501
|
||||
SUBSCRIPTION_CHANNEL_SIZE = 32
|
||||
|
||||
@ -137,6 +132,7 @@ class Pubsub(Service, IPubsub):
|
||||
router: IPubsubRouter,
|
||||
cache_size: int = None,
|
||||
seen_ttl: int = 120,
|
||||
sweep_interval: int = 60,
|
||||
strict_signing: bool = True,
|
||||
msg_id_constructor: Callable[
|
||||
[rpc_pb2.Message], bytes
|
||||
@ -188,7 +184,7 @@ class Pubsub(Service, IPubsub):
|
||||
else:
|
||||
self.sign_key = None
|
||||
|
||||
self.seen_messages = LastSeenCache(seen_ttl)
|
||||
self.seen_messages = LastSeenCache(seen_ttl, sweep_interval)
|
||||
|
||||
# Map of topics we are subscribed to blocking queues
|
||||
# for when the given topic receives a message
|
||||
|
||||
@ -424,7 +424,6 @@ class GossipsubFactory(factory.Factory):
|
||||
degree = GOSSIPSUB_PARAMS.degree
|
||||
degree_low = GOSSIPSUB_PARAMS.degree_low
|
||||
degree_high = GOSSIPSUB_PARAMS.degree_high
|
||||
time_to_live = GOSSIPSUB_PARAMS.time_to_live
|
||||
gossip_window = GOSSIPSUB_PARAMS.gossip_window
|
||||
gossip_history = GOSSIPSUB_PARAMS.gossip_history
|
||||
heartbeat_initial_delay = GOSSIPSUB_PARAMS.heartbeat_initial_delay
|
||||
@ -448,6 +447,7 @@ class PubsubFactory(factory.Factory):
|
||||
router: IPubsubRouter,
|
||||
cache_size: int,
|
||||
seen_ttl: int,
|
||||
sweep_interval: int,
|
||||
strict_signing: bool,
|
||||
msg_id_constructor: Callable[[rpc_pb2.Message], bytes] = None,
|
||||
) -> AsyncIterator[Pubsub]:
|
||||
@ -456,6 +456,7 @@ class PubsubFactory(factory.Factory):
|
||||
router=router,
|
||||
cache_size=cache_size,
|
||||
seen_ttl=seen_ttl,
|
||||
sweep_interval=sweep_interval,
|
||||
strict_signing=strict_signing,
|
||||
msg_id_constructor=msg_id_constructor,
|
||||
)
|
||||
@ -470,7 +471,8 @@ class PubsubFactory(factory.Factory):
|
||||
number: int,
|
||||
routers: Sequence[IPubsubRouter],
|
||||
cache_size: int = None,
|
||||
seen_ttl: int = None,
|
||||
seen_ttl: int = 120,
|
||||
sweep_interval: int = 60,
|
||||
strict_signing: bool = False,
|
||||
security_protocol: TProtocol = None,
|
||||
muxer_opt: TMuxerOptions = None,
|
||||
@ -488,6 +490,7 @@ class PubsubFactory(factory.Factory):
|
||||
router,
|
||||
cache_size,
|
||||
seen_ttl,
|
||||
sweep_interval,
|
||||
strict_signing,
|
||||
msg_id_constructor,
|
||||
)
|
||||
@ -503,6 +506,7 @@ class PubsubFactory(factory.Factory):
|
||||
number: int,
|
||||
cache_size: int = None,
|
||||
seen_ttl: int = 120,
|
||||
sweep_interval: int = 60,
|
||||
strict_signing: bool = False,
|
||||
protocols: Sequence[TProtocol] = None,
|
||||
security_protocol: TProtocol = None,
|
||||
@ -520,6 +524,7 @@ class PubsubFactory(factory.Factory):
|
||||
floodsubs,
|
||||
cache_size,
|
||||
seen_ttl,
|
||||
sweep_interval,
|
||||
strict_signing,
|
||||
security_protocol=security_protocol,
|
||||
muxer_opt=muxer_opt,
|
||||
@ -567,7 +572,6 @@ class PubsubFactory(factory.Factory):
|
||||
degree=degree,
|
||||
degree_low=degree_low,
|
||||
degree_high=degree_high,
|
||||
time_to_live=time_to_live,
|
||||
gossip_window=gossip_window,
|
||||
heartbeat_interval=heartbeat_interval,
|
||||
)
|
||||
|
||||
@ -1,21 +1,24 @@
|
||||
from abc import (
|
||||
ABC,
|
||||
abstractmethod,
|
||||
)
|
||||
import threading
|
||||
import time
|
||||
|
||||
|
||||
class TimedCache:
|
||||
class BaseTimedCache(ABC):
|
||||
"""Base class for Timed Cache with cleanup mechanism."""
|
||||
|
||||
cache: dict[bytes, int]
|
||||
|
||||
SWEEP_INTERVAL = 60 # 1-minute interval between each sweep
|
||||
|
||||
def __init__(self, ttl: int) -> None:
|
||||
def __init__(self, ttl: int, sweep_interval: int = 60) -> None:
|
||||
"""
|
||||
Initialize a new TimedCache with a time-to-live for cache entries
|
||||
Initialize a new BaseTimedCache with a time-to-live for cache entries
|
||||
|
||||
:param ttl: no of seconds as time-to-live for each cache entry
|
||||
"""
|
||||
self.ttl = ttl
|
||||
self.sweep_interval = sweep_interval
|
||||
self.lock = threading.Lock()
|
||||
self.cache = {}
|
||||
self._stop_event = threading.Event()
|
||||
@ -23,7 +26,7 @@ class TimedCache:
|
||||
self._thread.start()
|
||||
|
||||
def _background_cleanup(self) -> None:
|
||||
while not self._stop_event.wait(self.SWEEP_INTERVAL):
|
||||
while not self._stop_event.wait(self.sweep_interval):
|
||||
self._sweep()
|
||||
|
||||
def _sweep(self) -> None:
|
||||
@ -42,10 +45,10 @@ class TimedCache:
|
||||
def length(self) -> int:
|
||||
return len(self.cache)
|
||||
|
||||
@abstractmethod
|
||||
def add(self, key: bytes) -> bool:
|
||||
"""To be implemented in subclasses."""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def has(self, key: bytes) -> bool:
|
||||
"""To be implemented in subclasses."""
|
||||
raise NotImplementedError
|
||||
@ -1,11 +1,11 @@
|
||||
import time
|
||||
|
||||
from .basic_time_cache import (
|
||||
TimedCache,
|
||||
from .base_timed_cache import (
|
||||
BaseTimedCache,
|
||||
)
|
||||
|
||||
|
||||
class FirstSeenCache(TimedCache):
|
||||
class FirstSeenCache(BaseTimedCache):
|
||||
"""Cache where expiry is set only when first added."""
|
||||
|
||||
def add(self, key: bytes) -> bool:
|
||||
@ -1,11 +1,11 @@
|
||||
import time
|
||||
|
||||
from .basic_time_cache import (
|
||||
TimedCache,
|
||||
from .base_timed_cache import (
|
||||
BaseTimedCache,
|
||||
)
|
||||
|
||||
|
||||
class LastSeenCache(TimedCache):
|
||||
class LastSeenCache(BaseTimedCache):
|
||||
"""Cache where expiry is updated on every access."""
|
||||
|
||||
def add(self, key: bytes) -> bool:
|
||||
Reference in New Issue
Block a user