Trait TetherResolver

Source
pub trait TetherResolver: Unpin {
    type Error;

    // Required method
    fn disconnected(
        &mut self,
        context: &Context,
        state: &State<Self::Error>,
    ) -> impl Future<Output = bool> + Send;
}
Expand description

Represents a type which drives reconnects

Since the disconnected method asynchronous, and is invoked when the underlying stream disconnects, calling asynchronous functions like tokio::time::sleep from within the body, work.

§Example

A very simple implementation may look something like the following:

pub struct RetryResolver;

impl TetherResolver for RetryResolver {
    type Error = std::io::Error;

    async fn disconnected(&mut self, context: &Context, state: &State<Self::Error>) -> bool {
        tracing::warn!(?state, "Disconnected from server");
        if context.reconnect_count() >= 5 {
            return false;
        }

        tokio::time::sleep(Duration::from_secs(10)).await;
        true
    }
}

Required Associated Types§

Required Methods§

Source

fn disconnected( &mut self, context: &Context, state: &State<Self::Error>, ) -> impl Future<Output = bool> + Send

Invoked by Tether when an error/disconnect is encountered.

Returning true will result in a reconnect being attempted via <T as TetherIo>::reconnect, returning false will result in the error being returned from the originating call.

§Note

The State will describe the type of the underlying error. It can either be State::Eof, in which case the end of file was reached, or an error. This information can be leveraged in this function to determine whether to attempt to reconnect.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§