Skip to main content

karyon_p2p/
connector.rs

1use std::{marker::PhantomData, sync::Arc, time::Duration};
2
3use log::{error, trace, warn};
4
5use karyon_core::{
6    async_runtime::{
7        Executor,
8        lock::{Semaphore, SemaphoreGuard},
9    },
10    async_util::{Backoff, TaskGroup, TaskResult, timeout},
11    crypto::KeyPair,
12};
13use karyon_net::{ByteBuffer, ClientLayer, Endpoint, FramedConn, codec::Codec, framed, tcp};
14
15use crate::{
16    Error, PeerID, Result,
17    codec::PeerNetMsgCodec,
18    conn_direction::ConnDirection,
19    conn_queue::ConnQueue,
20    monitor::{ConnectionKind, Monitor},
21    tls_config::{peer_id_from_certs, tls_client_config},
22};
23
24#[cfg(feature = "quic")]
25use karyon_net::{StreamMux, quic};
26
27#[cfg(feature = "quic")]
28use crate::tls_config::quic_client_config;
29
30static DNS_NAME: &str = "karyontech.net";
31
32/// Internal dial result. Generic over the codec used to frame the
33/// resulting connection. Carries the optional cert-derived PeerID so
34/// the caller can stamp it on the QueuedConn for the application
35/// handshake to enforce.
36enum DialResult<C: Codec<ByteBuffer> + Default + Clone> {
37    /// TCP or TLS: a framed connection.
38    Channel(FramedConn<C>, Option<PeerID>),
39    /// QUIC: handshake stream + full QUIC connection.
40    #[cfg(feature = "quic")]
41    Quic(FramedConn<C>, quic::QuicConn, Option<PeerID>),
42}
43
44/// Creates outbound connections with other peers. Generic over the
45/// codec applied to the resulting framed stream so the same machinery
46/// (TLS, retries, slots, monitor events) serves both the peer
47/// data-plane (`PeerNetMsgCodec`) and the kademlia lookup-plane
48/// (`KadNetMsgCodec`).
49pub struct Connector<C: Codec<ByteBuffer> + Default + Clone> {
50    key_pair: KeyPair,
51    task_group: TaskGroup,
52    connection_slots: Semaphore,
53    max_retries: usize,
54    /// Max time for one dial: connect plus the TLS or QUIC handshake.
55    dial_timeout: Duration,
56    conn_queue: Option<Arc<ConnQueue>>,
57    monitor: Arc<Monitor>,
58    _codec: PhantomData<C>,
59}
60
61impl<C> Connector<C>
62where
63    C: Codec<ByteBuffer, Error = karyon_net::Error> + Default + Clone + Send + Sync + 'static,
64{
65    /// Create a new Connector without a ConnQueue (used for plain
66    /// `connect` paths like Kademlia lookup). `dial_timeout` is in seconds.
67    pub fn new(
68        key_pair: &KeyPair,
69        max_retries: usize,
70        slots: usize,
71        dial_timeout: u64,
72        monitor: Arc<Monitor>,
73        ex: Executor,
74    ) -> Arc<Self> {
75        Arc::new(Self {
76            key_pair: key_pair.clone(),
77            max_retries,
78            dial_timeout: Duration::from_secs(dial_timeout),
79            task_group: TaskGroup::with_executor(ex),
80            monitor,
81            connection_slots: Semaphore::new(slots),
82            conn_queue: None,
83            _codec: PhantomData,
84        })
85    }
86
87    pub async fn shutdown(&self) {
88        self.task_group.cancel().await;
89    }
90
91    /// Connect and return the framed connection. The slot is held until
92    /// the returned guard is dropped.
93    pub async fn connect(
94        &self,
95        endpoint: &Endpoint,
96        peer_id: &Option<PeerID>,
97    ) -> Result<(FramedConn<C>, SemaphoreGuard)> {
98        let slot = self.acquire_slot().await?;
99        let result = self.connect_internal(endpoint, peer_id).await?;
100        match result {
101            DialResult::Channel(conn, _) => Ok((conn, slot)),
102            #[cfg(feature = "quic")]
103            DialResult::Quic(conn, _, _) => Ok((conn, slot)),
104        }
105    }
106
107    /// Wait for a free connection slot.
108    pub async fn acquire_slot(&self) -> Result<SemaphoreGuard> {
109        Ok(self.connection_slots.acquire().await?)
110    }
111
112    /// Dial with retries. Each try is bounded by `dial_timeout`, so an
113    /// endpoint that accepts and then stays silent can not hang the dial.
114    async fn connect_internal(
115        &self,
116        endpoint: &Endpoint,
117        peer_id: &Option<PeerID>,
118    ) -> Result<DialResult<C>> {
119        let mut retry = 0;
120        let backoff = Backoff::new(500, 2000);
121        while retry < self.max_retries {
122            let dial = async { timeout(self.dial_timeout, self.dial(endpoint, peer_id)).await? };
123            match dial.await {
124                Ok(result) => {
125                    self.monitor
126                        .notify(ConnectionKind::Connected(endpoint.clone()))
127                        .await;
128                    return Ok(result);
129                }
130                Err(err) => {
131                    error!("Failed to connect to {endpoint}: {err}");
132                }
133            }
134
135            self.monitor
136                .notify(ConnectionKind::ConnectRetried(endpoint.clone()))
137                .await;
138
139            backoff.sleep().await;
140            warn!("try to reconnect {endpoint}");
141            retry += 1;
142        }
143
144        self.monitor
145            .notify(ConnectionKind::ConnectFailed(endpoint.clone()))
146            .await;
147
148        Err(Error::Timeout)
149    }
150
151    /// Dial, selecting transport based on endpoint type.
152    async fn dial(&self, endpoint: &Endpoint, peer_id: &Option<PeerID>) -> Result<DialResult<C>> {
153        match endpoint {
154            Endpoint::Tcp(..) => {
155                let stream = tcp::connect(endpoint, Default::default()).await?;
156                let conn = framed(stream, C::default());
157                Ok(DialResult::Channel(conn, None))
158            }
159            Endpoint::Tls(..) => {
160                let tls_config = karyon_net::tls::ClientTlsConfig {
161                    client_config: tls_client_config(&self.key_pair, peer_id.clone())?,
162                    dns_name: DNS_NAME.to_string(),
163                };
164                let stream = tcp::connect(endpoint, Default::default()).await?;
165                let tls_layer = karyon_net::tls::TlsLayer::client(tls_config);
166                let tls_stream = ClientLayer::handshake(&tls_layer, stream).await?;
167                // Extract the peer cert before framing consumes the stream.
168                let vpid = tls_stream
169                    .peer_certificates()
170                    .as_deref()
171                    .and_then(peer_id_from_certs);
172                let conn = framed(tls_stream, C::default());
173                Ok(DialResult::Channel(conn, vpid))
174            }
175            #[cfg(feature = "quic")]
176            Endpoint::Quic(..) => {
177                let rustls_config = quic_client_config(&self.key_pair, peer_id.clone())?;
178                let client_config = quic::ClientQuicConfig::from_rustls(rustls_config, DNS_NAME);
179                let quic_conn = quic::QuicEndpoint::dial(endpoint, client_config).await?;
180
181                let vpid = quic_conn
182                    .peer_certificates()
183                    .as_deref()
184                    .and_then(peer_id_from_certs);
185
186                // First stream for handshake via StreamMux.
187                let stream = quic_conn.open_stream().await?;
188                let conn = framed(stream, C::default());
189
190                Ok(DialResult::Quic(conn, quic_conn, vpid))
191            }
192            _ => Err(Error::UnsupportedEndpoint(endpoint.to_string())),
193        }
194    }
195}
196
197// `connect_and_queue` only lives on the peer-plane Connector since the
198// ConnQueue is part of the data-plane handshake pipeline.
199impl Connector<PeerNetMsgCodec> {
200    /// Create a new Connector with a ConnQueue. `dial_timeout` is in
201    /// seconds.
202    pub fn new_with_queue(
203        key_pair: &KeyPair,
204        max_retries: usize,
205        slots: usize,
206        dial_timeout: u64,
207        conn_queue: Arc<ConnQueue>,
208        monitor: Arc<Monitor>,
209        ex: Executor,
210    ) -> Arc<Self> {
211        Arc::new(Self {
212            key_pair: key_pair.clone(),
213            max_retries,
214            dial_timeout: Duration::from_secs(dial_timeout),
215            task_group: TaskGroup::with_executor(ex),
216            monitor,
217            connection_slots: Semaphore::new(slots),
218            conn_queue: Some(conn_queue),
219            _codec: PhantomData,
220        })
221    }
222
223    /// Connect, queue for handshake, and run until the peer
224    /// disconnects. Disconnect/handshake-failure is observable via
225    /// `PeerPool::register_peer_events`, not via this call. `slot` comes
226    /// from `acquire_slot` and is held until the peer disconnects.
227    pub async fn connect_and_queue(
228        self: &Arc<Self>,
229        endpoint: &Endpoint,
230        peer_id: &Option<PeerID>,
231        slot: SemaphoreGuard,
232    ) -> Result<()> {
233        let dial_result = self.connect_internal(endpoint, peer_id).await?;
234
235        let endpoint = endpoint.clone();
236        let on_disconnect = {
237            let this = self.clone();
238            |res: TaskResult<Result<()>>| async move {
239                if let TaskResult::Completed(Err(err)) = res {
240                    trace!("Outbound connection dropped: {err}");
241                }
242                this.monitor
243                    .notify(ConnectionKind::Disconnected(endpoint.clone()))
244                    .await;
245                drop(slot);
246            }
247        };
248
249        let conn_queue = self
250            .conn_queue
251            .as_ref()
252            .ok_or_else(|| {
253                Error::Config(
254                    "connect_and_queue called on Connector built without ConnQueue".into(),
255                )
256            })?
257            .clone();
258        self.task_group.spawn_then(
259            async move {
260                match dial_result {
261                    DialResult::Channel(conn, vpid) => {
262                        conn_queue.handle(conn, ConnDirection::Outbound, vpid).await
263                    }
264                    #[cfg(feature = "quic")]
265                    DialResult::Quic(conn, quic_conn, vpid) => {
266                        conn_queue
267                            .handle_quic(conn, quic_conn, ConnDirection::Outbound, vpid)
268                            .await
269                    }
270                }
271            },
272            on_disconnect,
273        );
274
275        Ok(())
276    }
277}