Resolver

Trait Resolver 

Source
pub trait Resolver<C> {
    // Required method
    fn disconnected(
        &mut self,
        context: &Context,
        connector: &mut C,
    ) -> PinFut<Action>;

    // Provided methods
    fn unreachable(
        &mut self,
        context: &Context,
        connector: &mut C,
    ) -> 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.

§Unpin

Since the method provides &mut Self, Self must be Unpin

§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<C> Resolver<C> for RetryResolver {
    fn disconnected(&mut self, context: &Context, _: &mut C) -> PinFut<Action> {
        let reason = context.reason();
        println!("WARN: Disconnected from server {:?}", reason);
        self.0 = true;

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

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

Required Methods§

Source

fn disconnected( &mut self, context: &Context, connector: &mut C, ) -> PinFut<Action>

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.

Provided Methods§

Source

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

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

As with Self::disconnected the returned boolean determines whether the initial connection attempt is retried

Defaults to invoking Self::disconnected where Action::Ignore results in a disconnect

Source

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

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

Defaults to invoking Self::reconnected

Source

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

Invoked by Tether whenever the connection to the underlying I/O source has been re-established

Implementors§