peer_sender.rs

Overview

The peer_sender.rs file implements the PeerSender struct and its associated asynchronous logic for managing outgoing direct peer-to-peer network communication. It is responsible for establishing and maintaining a connection to a remote peer, sending messages over this connection, and processing received messages and transfer results. The file leverages an abstracted transport layer (NetTransport and NetConnection) to support different network transports and protocols.

The main responsibilities of this file include:

This file interacts closely with modules related to network transport, message handling, metrics collection, and pub-sub connection management. It uses types such as NetMessage, NetTransport, ConnectionWrapper, and NetMetrics to coordinate the message delivery lifecycle.


Structs and Methods

PeerSender<PeerId, Transport>

The core struct representing a peer sender responsible for managing the connection and sending messages to a specific peer.

Type Parameters

Fields

Methods

new
pub(crate) fn new(
    id: PeerId,
    addr: SocketAddr,
    shutdown_rx: tokio::sync::watch::Receiver<bool>,
    transport: Transport,
    metrics: Option<NetMetrics>,
    credential: NetCredential,
    peer_events_tx: tokio::sync::mpsc::UnboundedSender<PeerEvent<PeerId>>,
    incoming_reply_tx: IncomingSender,
) -> Self

Creates a new PeerSender instance with the provided parameters.

run
pub(crate) async fn run(
    &mut self,
    mut outgoing_rx: tokio::sync::mpsc::Receiver<(NetMessage, Instant)>,
    metrics: Option<NetMetrics>,
)

Runs the main asynchronous loop managing connection lifecycle, message sending, and receiving.

connect_to_peer
async fn connect_to_peer(&self) -> anyhow::Result<ConnectionWrapper<Transport::Connection>>

Attempts to establish an outgoing connection to the peer's address, retrying with Fibonacci backoff on failure.

start_transfer_message
fn start_transfer_message(&self, net_message: NetMessage, buffer_duration: Instant)

Initiates an asynchronous task to transfer a given message over the active connection.

handle_transfer_result
async fn handle_transfer_result(
    &mut self,
    transfer_result: (Result<usize, TransportError>, NetMessage, Instant)
) -> bool

Processes the result of a message transfer.


Free Functions

receive_message

async fn receive_message<Connection: NetConnection>(
    connection: Option<Arc<ConnectionWrapper<Connection>>>,
    metrics: Option<NetMetrics>,
    incoming_reply_tx: IncomingSender,
) -> bool

Receives and processes a message from the given connection.


Important Implementation Details and Algorithms


Interactions with Other Parts of the System


Visual Diagram

classDiagram
class PeerSender {
- id: PeerId
- addr: SocketAddr
- connection: Option<Arc<ConnectionWrapper>>
- shutdown_rx: watch::Receiver<bool>
- transport: Transport
- metrics: Option<NetMetrics>
- credential: NetCredential
- peer_events_tx: mpsc::UnboundedSender<PeerEvent>
- transfer_result_tx: mpsc::Sender<(Result<usize, TransportError>, NetMessage, Instant)>
- transfer_result_rx: mpsc::Receiver<(Result<usize, TransportError>, NetMessage, Instant)>
- incoming_reply_tx: IncomingSender
+ new(...)
+ run(outgoing_rx, metrics)
- connect_to_peer() : Result<ConnectionWrapper>
- start_transfer_message(net_message, buffer_duration)
- handle_transfer_result(transfer_result) : bool
}
class receive_message {
<<function>>
+ receive_message(connection, metrics, incoming_reply_tx) : bool
}
PeerSender --> ConnectionWrapper : manages
PeerSender --> NetTransport : uses
PeerSender --> NetMetrics : reports metrics
PeerSender --> PeerEvent : sends events
PeerSender --> NetMessage : sends messages
PeerSender --> IncomingSender : sends incoming messages
PeerSender --> transfer : calls async transfer
receive_message ..> ConnectionWrapper : receives from
receive_message ..> NetMetrics : reports metrics
receive_message ..> IncomingSender : forwards messages

This diagram depicts the main struct PeerSender with its key fields and methods, as well as its interaction with the receive_message function. The relationships illustrate how PeerSender manages connection wrappers, interacts with transport and metrics systems, and coordinates message sending and event reporting.