alloy_pubsub/
connect.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use crate::{handle::ConnectionHandle, service::PubSubService, PubSubFrontend};
use alloy_transport::{impl_future, TransportResult};

/// Configuration objects that contain connection details for a backend.
///
/// Implementers should contain configuration options for the underlying
/// transport.
pub trait PubSubConnect: Sized + Send + Sync + 'static {
    /// Returns `true` if the transport connects to a local resource.
    fn is_local(&self) -> bool;

    /// Spawn the backend, returning a handle to it.
    ///
    /// This function MUST create a long-lived task containing a
    /// [`ConnectionInterface`], and return the corresponding handle.
    ///
    /// [`ConnectionInterface`]: crate::ConnectionInterface
    fn connect(&self) -> impl_future!(<Output = TransportResult<ConnectionHandle>>);

    /// Attempt to reconnect the transport.
    ///
    /// Override this to add custom reconnection logic to your connector. This
    /// will be used by the internal pubsub connection managers in the event the
    /// connection fails.
    fn try_reconnect(&self) -> impl_future!(<Output = TransportResult<ConnectionHandle>>) {
        self.connect()
    }

    /// Convert the configuration object into a service with a running backend.
    fn into_service(self) -> impl_future!(<Output = TransportResult<PubSubFrontend>>) {
        PubSubService::connect(self)
    }
}