Skip to main content

karyon_p2p/peer/
mod.rs

1mod connection;
2mod peer_id;
3
4use std::{
5    collections::HashSet,
6    sync::{Arc, Weak},
7    time::Duration,
8};
9
10use async_channel::{Receiver, Sender};
11use log::{error, trace};
12
13use karyon_core::{
14    async_runtime::Executor,
15    async_util::{TaskGroup, TaskResult, timeout},
16};
17
18use crate::{
19    Config, Result,
20    conn_direction::ConnDirection,
21    conn_queue::QueuedConn,
22    endpoint::Endpoint,
23    peer_pool::PeerPool,
24    protocol::{PeerConn, ProtocolEvent, ProtocolID},
25};
26
27pub use peer_id::PeerID;
28
29use connection::Wire;
30
31/// A connected peer. Holds a `Wire` that hides the wire shape
32/// (single framed pipe vs. per-protocol streams).
33pub struct Peer {
34    id: PeerID,
35    /// Unique per connection in the pool. A peer that reconnects or whose
36    /// connection is replaced gets a new one, while `id` stays the same.
37    conn_id: u64,
38    peer_pool: Weak<PeerPool>,
39
40    direction: ConnDirection,
41    remote_endpoint: Endpoint,
42
43    connection: Arc<dyn Wire>,
44    /// Wakes the `ConnQueue::handle` call that queued this connection, so
45    /// the listener/connector task waiting on it can end.
46    conn_closed_tx: Sender<Result<()>>,
47
48    negotiated_protocols: HashSet<ProtocolID>,
49    /// Peer tasks send the reason the peer must stop. `run` waits on
50    /// `stop_reason_rx` and returns it.
51    stop_reason_tx: Sender<Result<()>>,
52    stop_reason_rx: Receiver<Result<()>>,
53    config: Arc<Config>,
54    executor: Executor,
55    task_group: TaskGroup,
56}
57
58impl Peer {
59    /// Builds the peer and its wire. Does not wait for the remote, so the
60    /// pool can call it while holding its peers lock.
61    pub(crate) fn new(
62        peer_pool: Arc<PeerPool>,
63        queued: QueuedConn,
64        id: PeerID,
65        conn_id: u64,
66        negotiated_protocols: HashSet<ProtocolID>,
67    ) -> Result<Arc<Self>> {
68        let config = peer_pool.config.clone();
69        let executor = peer_pool.executor.clone();
70        let task_group = TaskGroup::with_executor(executor.clone());
71        let (stop_reason_tx, stop_reason_rx) = async_channel::bounded::<Result<()>>(1);
72
73        let remote_endpoint = queued.remote_endpoint.clone();
74        let direction = queued.direction.clone();
75        let conn_closed_tx = queued.conn_closed_tx.clone();
76
77        let connection = connection::from_queued(
78            queued,
79            &negotiated_protocols,
80            &task_group,
81            stop_reason_tx.clone(),
82        )?;
83
84        let peer_pool_weak = Arc::downgrade(&peer_pool);
85        Ok(Arc::new(Peer {
86            id,
87            conn_id,
88            peer_pool: peer_pool_weak,
89            direction,
90            remote_endpoint,
91            connection,
92            conn_closed_tx,
93            negotiated_protocols,
94            stop_reason_tx,
95            stop_reason_rx,
96            config,
97            executor,
98            task_group,
99        }))
100    }
101
102    pub async fn send(&self, proto_id: ProtocolID, msg: Vec<u8>) -> Result<()> {
103        self.connection.send(&proto_id, msg).await
104    }
105
106    pub async fn recv(&self, proto_id: &ProtocolID) -> Result<ProtocolEvent> {
107        self.connection.recv(proto_id).await
108    }
109
110    pub async fn broadcast(&self, proto_id: &ProtocolID, msg: Vec<u8>) {
111        self.peer_pool().broadcast(proto_id, msg).await;
112    }
113
114    pub fn id(&self) -> &PeerID {
115        &self.id
116    }
117
118    /// Identifies this connection to the peer, see the field.
119    pub(crate) fn conn_id(&self) -> u64 {
120        self.conn_id
121    }
122
123    pub fn config(&self) -> Arc<Config> {
124        self.config.clone()
125    }
126
127    pub fn executor(&self) -> Executor {
128        self.executor.clone()
129    }
130
131    pub fn remote_endpoint(&self) -> &Endpoint {
132        &self.remote_endpoint
133    }
134
135    pub fn is_inbound(&self) -> bool {
136        matches!(self.direction, ConnDirection::Inbound)
137    }
138
139    pub fn direction(&self) -> &ConnDirection {
140        &self.direction
141    }
142
143    pub fn negotiated_protocols(&self) -> &HashSet<ProtocolID> {
144        &self.negotiated_protocols
145    }
146
147    pub(crate) async fn run(self: Arc<Self>) -> Result<()> {
148        // QUIC opens its protocol streams here. A peer that does not open
149        // them in time is stopped, like on any other connection error.
150        let setup_timeout = Duration::from_secs(self.config.handshake_timeout);
151        let setup = self
152            .connection
153            .setup(&self.task_group, self.stop_reason_tx.clone());
154        timeout(setup_timeout, setup).await??;
155
156        self.run_connect_protocols().await;
157
158        self.stop_reason_rx.recv().await?
159    }
160
161    pub(crate) async fn shutdown(self: &Arc<Self>) -> Result<()> {
162        trace!("peer {} shutting down", self.id);
163
164        let _ = self.connection.shutdown().await;
165        let _ = self.stop_reason_tx.try_send(Ok(()));
166
167        let _ = self.conn_closed_tx.send(Ok(())).await;
168        self.task_group.cancel().await;
169        Ok(())
170    }
171
172    async fn run_connect_protocols(self: &Arc<Self>) {
173        for (proto_id, constructor) in self.peer_pool().protocols.read().await.iter() {
174            if !self.negotiated_protocols.contains(proto_id) {
175                trace!("peer {} skip protocol {proto_id} (not negotiated)", self.id);
176                continue;
177            }
178            trace!("peer {} run protocol {proto_id}", self.id);
179
180            let peer_conn = PeerConn::new(self.clone(), proto_id.clone());
181            let protocol = constructor(peer_conn);
182
183            let on_failure = {
184                let this = self.clone();
185                let proto_id = proto_id.clone();
186                |result: TaskResult<Result<()>>| async move {
187                    if let TaskResult::Completed(res) = result {
188                        if res.is_err() {
189                            error!("protocol {proto_id} stopped");
190                        }
191                        let _ = this.stop_reason_tx.try_send(res);
192                    }
193                }
194            };
195
196            self.task_group.spawn_then(protocol.start(), on_failure);
197        }
198    }
199
200    fn peer_pool(&self) -> Arc<PeerPool> {
201        self.peer_pool.upgrade().unwrap()
202    }
203}