commonware_p2p/authenticated/lookup/actors/spawner/
ingress.rs

1use crate::authenticated::{lookup::actors::tracker::Reservation, Mailbox};
2use commonware_cryptography::PublicKey;
3use commonware_runtime::{Sink, Stream};
4use commonware_stream::{Receiver, Sender};
5use commonware_utils::channels::fallible::AsyncFallibleExt;
6
7/// Messages that can be processed by the spawner actor.
8pub enum Message<Si: Sink, St: Stream, P: PublicKey> {
9    /// Notify the spawner to create a new task for the given peer.
10    Spawn {
11        /// The peer's public key.
12        peer: P,
13        /// The connection to the peer.
14        connection: (Sender<Si>, Receiver<St>),
15        /// The reservation for the peer.
16        reservation: Reservation<P>,
17    },
18}
19
20impl<Si: Sink, St: Stream, P: PublicKey> Mailbox<Message<Si, St, P>> {
21    /// Send a message to the actor to spawn a new task for the given peer.
22    ///
23    /// This may fail during shutdown if the spawner has already exited,
24    /// which is harmless since no new connections need to be spawned.
25    pub async fn spawn(
26        &mut self,
27        connection: (Sender<Si>, Receiver<St>),
28        reservation: Reservation<P>,
29    ) {
30        self.0
31            .send_lossy(Message::Spawn {
32                peer: reservation.metadata().public_key().clone(),
33                connection,
34                reservation,
35            })
36            .await;
37    }
38}