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,
impl<Sf, S, St> ReconnectService<DefaultPolicy, Sf, S, St>where
St: 'static,
Sf: 'static,
Sourcepub 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>,
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>
impl<P, Sf, S, St> ReconnectService<P, Sf, S, St>
Sourcepub fn retry(self, retry: usize) -> ReconnectService<P, Sf, S, St>
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.
Sourcepub fn policy<T, Req>(self, policy: T) -> ReconnectService<T, Sf, S, St>
pub fn policy<T, Req>(self, policy: T) -> ReconnectService<T, Sf, S, St>
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,
impl<P, Sf, S, St> Clone for ReconnectService<P, Sf, S, St>where
P: Clone,
Source§fn clone(&self) -> ReconnectService<P, Sf, S, St>
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)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<P, Sf, S, St> Debug for ReconnectService<P, Sf, S, St>
impl<P, Sf, S, St> Debug for ReconnectService<P, Sf, S, St>
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,
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<'_>)
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>
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>
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>)
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.