Skip to content

Commit

Permalink
refactor(p2p): implement PeerConnections
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco committed Nov 25, 2024
1 parent 4da0fb9 commit 8fbf968
Show file tree
Hide file tree
Showing 26 changed files with 609 additions and 406 deletions.
10 changes: 5 additions & 5 deletions hathor/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ class Metrics:
# Variables to store the last block when we updated the RocksDB storage metrics
last_txstorage_data_block: Optional[int] = None

# Peers connected
connected_peers: int = 0
# Peers ready
ready_peers: int = 0
# Peers handshaking
handshaking_peers: int = 0
# Peers connecting
Expand Down Expand Up @@ -200,7 +200,7 @@ def handle_publish(self, key: HathorEvents, args: EventArguments) -> None:
):
peers_connection_metrics: PeerConnectionsMetrics = data["peers_count"]

self.connected_peers = peers_connection_metrics.connected_peers_count
self.ready_peers = peers_connection_metrics.ready_peers_count
self.connecting_peers = peers_connection_metrics.connecting_peers_count
self.handshaking_peers = peers_connection_metrics.handshaking_peers_count
self.known_peers = peers_connection_metrics.known_peers_count
Expand Down Expand Up @@ -247,14 +247,14 @@ def collect_peer_connection_metrics(self) -> None:
"""
self.peer_connection_metrics.clear()

for connection in self.connections.connections:
for connection in self.connections.get_connected_peers():
if not connection._peer:
# A connection without peer will not be able to communicate
# So we can just discard it for the sake of the metrics
continue

metric = PeerConnectionMetrics(
connection_string=str(connection.entrypoint) if connection.entrypoint else "",
connection_string=str(connection.addr),
peer_id=str(connection.peer.id),
network=settings.NETWORK_NAME,
received_messages=connection.metrics.received_messages,
Expand Down
8 changes: 4 additions & 4 deletions hathor/p2p/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from hathor.conf.settings import HathorSettings
from hathor.p2p.manager import ConnectionsManager
from hathor.p2p.peer import PrivatePeer
from hathor.p2p.peer_endpoint import PeerAddress
from hathor.p2p.protocol import HathorLineReceiver


Expand All @@ -41,15 +42,14 @@ def __init__(
self.use_ssl = use_ssl

def buildProtocol(self, addr: IAddress) -> HathorLineReceiver:
p = HathorLineReceiver(
return HathorLineReceiver(
addr=PeerAddress.from_address(addr),
my_peer=self.my_peer,
p2p_manager=self.p2p_manager,
use_ssl=self.use_ssl,
inbound=self.inbound,
settings=self._settings
settings=self._settings,
)
p.factory = self
return p


class HathorServerFactory(_HathorLineReceiverFactory, protocol.ServerFactory):
Expand Down
Loading

0 comments on commit 8fbf968

Please sign in to comment.