Trait Resolver

Source
pub trait Resolver: Unpin {
    // Required method
    fn disconnected(&mut self, context: &Context, state: &State) -> PinFut<bool>;

    // Provided methods
    fn unreachable(&mut self, context: &Context, state: &State) -> PinFut<bool> { ... }
    fn established(&mut self, context: &Context) -> PinFut<()> { ... }
    fn reconnected(&mut self, _context: &Context) -> PinFut<()> { ... }
}
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.

§Return Type

The return types of the methods are PinFut. This has the requirement that the returned future be ’static (cannot hold references to self, or any of the arguments). However, you are still free to mutate data outside of the returned future.

Additionally, this method is invoked each time the I/O fails to establish a connection so writing futures which do not reference their environment is a little easier than it may seem.

§Example

A very simple implementation may look something like the following:

pub struct RetryResolver(bool);

impl Resolver for RetryResolver {
    fn disconnected(&mut self, context: &Context, state: &State) -> PinFut<bool> {
        println!("WARN: Disconnected from server {:?}", state);
        self.0 = true;

        if context.current_reconnect_attempts() >= 5 || context.total_reconnect_attempts() >= 50 {
            return Box::pin(async move {false});
        }

        Box::pin(async move {
            tokio::time::sleep(Duration::from_secs(10)).await;
            true
        })
    }
}

Required Methods§

Source

fn disconnected(&mut self, context: &Context, state: &State) -> PinFut<bool>

Invoked by Tether when an error/disconnect is encountered.

Returning true will result in a reconnect being attempted via <T as Io>::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.

Provided Methods§

Source

fn unreachable(&mut self, context: &Context, state: &State) -> PinFut<bool>

Invoked within Tether::connect if the initial connection attempt fails

Source

fn established(&mut self, context: &Context) -> PinFut<()>

Invoked within Tether::connect if the initial connection attempt succeeds

Source

fn reconnected(&mut self, _context: &Context) -> PinFut<()>

Invoked by Tether when the underlying I/O connection has been re-established

Implementors§