Skip to main content

ReconnectService

Struct ReconnectService 

Source
pub struct ReconnectService<P, Sf, S, St> { /* private fields */ }
Expand description

Lazily creates a child service and replaces it after policy-approved errors.

Creation and readiness failures can retry within the configured budget using exponential backoff. Successful creation resets backoff, even if the new child’s readiness later fails; successful readiness restores the retry budget. Unused and healthy time do not count toward the backoff time limit. Once recovery starts, its elapsed-time window and retry deadlines survive cancellation.

Call errors are returned without replaying the request. If the policy permits reconnection, an error from the current child invalidates readiness so a later operation can replace it. Shutdown awaits the stored child without reconnecting.

Each child has its own readiness scope. Retired children’s callbacks are released and their notifier subscriptions become inert when the last child handle is dropped, including handles held by outstanding calls.

Clones start disconnected and create their own inner service on the next readiness check. They copy the policy, remaining and configured retry budgets, and backoff settings and interval, but not the connection, elapsed-time window, or a pending retry deadline.

Each instance belongs to the first pipeline that binds it. Sharing an Rc<ReconnectService> within that pipeline shares its connection and readiness; binding the same instance to another pipeline panics, even after the owner is dropped. Clone the service itself for independent connections, or share bindings to one ntex_service::Pipeline for a shared connection.

§Examples

use std::io;
use ntex_service::{Pipeline, fn_factory, fn_service};
use ntex_util::services::reconnect::ReconnectService;

let factory = fn_factory(|_: &()| async {
    Ok::<_, io::Error>(fn_service(|req: u32| async move {
        Ok::<_, io::Error>(req)
    }))
});
let service = ReconnectService::new("upstream", factory).retry(2);
let first = Pipeline::new((), service.clone());
let second = Pipeline::new((), service);

assert_eq!(first.call(1).await?, 1);
assert_eq!(second.call(2).await?, 2);

Implementations§

Source§

impl<Sf, S, St> ReconnectService<DefaultPolicy, Sf, S, St>
where St: 'static, Sf: 'static,

Source

pub fn new<Req>( name: &'static str, factory: Sf, ) -> ReconnectService<DefaultPolicy, Sf, S, St>
where Sf: ServiceFactory<St, Req, Service = S, Error = <S as Service<St, Req>>::Error> + 'static, <Sf as ServiceFactory<St, Req>>::Error: Error, <Sf as ServiceFactory<St, Req>>::InitError: Into<<Sf as ServiceFactory<St, Req>>::Error> + Error, S: Service<St, Req>,

Creates a disconnected service with DefaultPolicy and three retries.

name is used in diagnostics. The factory is retained without being called; binding to a pipeline is also lazy with respect to child creation.

Source§

impl<P, Sf, S, St> ReconnectService<P, Sf, S, St>

Source

pub fn retry(self, retry: usize) -> ReconnectService<P, Sf, S, St>

Sets both the remaining and configured budget for additional attempts.

Zero permits the initial attempt but no retries after creation or readiness errors. Successful child readiness restores this budget; policy rejection or backoff exhaustion can stop retries earlier.

Preserves the current child, pending deadline, backoff, and pipeline owner.

Source

pub fn policy<T, Req>(self, policy: T) -> ReconnectService<T, Sf, S, St>
where T: ReconnectPolicy<<Sf as ServiceFactory<St, Req>>::Error>, Sf: ServiceFactory<St, Req>,

Replaces the error policy without reconnecting or resetting readiness.

The connection, retry state, backoff, pending deadline, and pipeline owner are preserved. Changing policy does not undo an already scheduled retry.

Trait Implementations§

Source§

impl<P, Sf, S, St> Clone for ReconnectService<P, Sf, S, St>
where P: Clone,

Source§

fn clone(&self) -> ReconnectService<P, Sf, S, St>

Creates a disconnected clone with a fresh, unbound readiness scope.

The factory is shared, but the original connection and pipeline ownership are not. No child is created until the clone needs readiness.

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P, Sf, S, St> Debug for ReconnectService<P, Sf, S, St>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats connection metadata without requiring the factory or policy to implement Debug.

Source§

impl<P, Sf, S, St, Req> Service<St, Req> for ReconnectService<P, Sf, S, St>
where P: ReconnectPolicy<<Sf as ServiceFactory<St, Req>>::Error>, Sf: ServiceFactory<St, Req, Service = S, Error = <S as Service<St, Req>>::Error>, <Sf as ServiceFactory<St, Req>>::Error: Error, <Sf as ServiceFactory<St, Req>>::InitError: Into<<Sf as ServiceFactory<St, Req>>::Error> + Error, S: Service<St, Req>, St: 'static,

Source§

fn bind(&self, ctx: BindCtx<'_>)

Registers the owned readiness scope without creating a child.

Repeated binding within the same pipeline shares this scope.

§Panics

Panics if this instance was already bound to another pipeline, even if that pipeline has been dropped.

Source§

async fn ready( &self, ctx: Ctx<'_, ReconnectService<P, Sf, S, St>, St>, ) -> Result<(), <ReconnectService<P, Sf, S, St> as Service<St, Req>>::Error>

Waits for the owned readiness gate, lazily connecting or recovering as needed.

Successful readiness is cached until a subscribed notification invalidates it. Returns creation or readiness errors that cannot be retried.

§Panics

Panics if the caller’s scope has not bound this instance.

Source§

async fn call( &self, req: Req, ctx: Ctx<'_, ReconnectService<P, Sf, S, St>, St>, ) -> Result<<ReconnectService<P, Sf, S, St> as Service<St, Req>>::Res, <ReconnectService<P, Sf, S, St> as Service<St, Req>>::Error>

Dispatches through the owned readiness gate without replaying failed requests.

Returns readiness or child call errors. A policy-approved call error marks the current connection for replacement by a later operation.

§Panics

Panics if the caller’s scope has not bound this instance.

Source§

async fn shutdown(&self, ctx: Ctx<'_, ReconnectService<P, Sf, S, St>, St>)

Awaits shutdown of the stored child, or completes immediately if disconnected.

Does not check readiness, create a child, or reconnect an errored child.

§Panics

Panics if the caller’s scope has not bound this instance.

Source§

type Res = <S as Service<St, Req>>::Res

Response produced by the service.
Source§

type Error = <S as Service<St, Req>>::Error

Error produced while checking readiness or processing a request.
Source§

fn map<F, Res>(self, f: F) -> ServiceChain<Map<F, Self, Res>, St, Req>
where Self: Sized, F: Fn(Self::Res) -> Res,

Maps this service’s output to a different type, returning a new service. Read more
Source§

fn map_err<F, E>(self, f: F) -> ServiceChain<MapErr<F, Self, E>, St, Req>
where Self: Sized, F: Fn(Self::Error) -> E,

Maps this service’s error to a different type, returning a new service. Read more
Source§

fn and_then<Next, F>(self, f: F) -> ServiceChain<AndThen<Self, Next>, St, Req>
where Self: Sized, Next: Service<St, Self::Res, Error = Self::Error>, F: IntoService<Next, St, Self::Res>,

Calls another service after this service completes successfully. Read more
Source§

fn pipeline(self, st: St) -> Pipeline<Req, Self::Res, Self::Error>
where Self: Sized + 'static, St: 'static, Req: 'static,

Wraps this service and its state in a Pipeline.

Auto Trait Implementations§

§

impl<P, Sf, S, St> !Freeze for ReconnectService<P, Sf, S, St>

§

impl<P, Sf, S, St> !RefUnwindSafe for ReconnectService<P, Sf, S, St>

§

impl<P, Sf, S, St> !Send for ReconnectService<P, Sf, S, St>

§

impl<P, Sf, S, St> !Sync for ReconnectService<P, Sf, S, St>

§

impl<P, Sf, S, St> !UnwindSafe for ReconnectService<P, Sf, S, St>

§

impl<P, Sf, S, St> Unpin for ReconnectService<P, Sf, S, St>
where P: Unpin, St: Unpin,

§

impl<P, Sf, S, St> UnsafeUnpin for ReconnectService<P, Sf, S, St>
where P: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<S, St, Req> IntoService<S, St, Req> for S
where S: Service<St, Req>,

Source§

fn into_service(self) -> S

Converts this value into a service.
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V