alloy_pubsub/connect.rs
1use crate::{handle::ConnectionHandle, service::PubSubService, PubSubFrontend};
2use alloy_transport::{impl_future, TransportResult};
3
4/// Configuration objects that contain connection details for a backend.
5///
6/// Implementers should contain configuration options for the underlying
7/// transport.
8#[auto_impl::auto_impl(Box)]
9pub trait PubSubConnect: Sized + Send + Sync + 'static {
10 /// Returns `true` if the transport connects to a local resource.
11 fn is_local(&self) -> bool;
12
13 /// Spawn the backend, returning a handle to it.
14 ///
15 /// This function MUST create a long-lived task containing a
16 /// [`ConnectionInterface`], and return the corresponding handle.
17 ///
18 /// [`ConnectionInterface`]: crate::ConnectionInterface
19 fn connect(&self) -> impl_future!(<Output = TransportResult<ConnectionHandle>>);
20
21 /// Attempt to reconnect the transport.
22 ///
23 /// Override this to add custom reconnection logic to your connector. This
24 /// will be used by the internal pubsub connection managers in the event the
25 /// connection fails.
26 fn try_reconnect(&self) -> impl_future!(<Output = TransportResult<ConnectionHandle>>) {
27 self.connect()
28 }
29
30 /// Convert the configuration object into a service with a running backend.
31 fn into_service(self) -> impl_future!(<Output = TransportResult<PubSubFrontend>>) {
32 PubSubService::connect(self)
33 }
34}