pub struct Ctx<'a, Svc, St = ()>where
Svc: ?Sized,{ /* private fields */ }Expand description
Context provided to Service lifecycle methods.
A context gives a service access to pipeline state and coordinates calls to
inner services with the pipeline’s readiness and shutdown machinery.
It borrows the current readiness scope from a pipeline or service binding.
Copying or mapping the context preserves that scope; calling a binding
selects the binding’s own scope without changing the caller’s context.
Existing dependencies must be registered in Service::bind using
BindCtx. Lazily created children can be initialized with Ctx::create;
readiness checks, calls, and shutdown never register dependencies implicitly.
Implementations§
Source§impl<'a, Svc, St> Ctx<'a, Svc, St>
impl<'a, Svc, St> Ctx<'a, Svc, St>
Sourcepub async fn create<Sf, Req>(
&self,
factory: &Sf,
) -> Result<<Sf as ServiceFactory<St, Req>>::Service, <Sf as ServiceFactory<St, Req>>::InitError>where
Sf: ServiceFactory<St, Req>,
pub async fn create<Sf, Req>(
&self,
factory: &Sf,
) -> Result<<Sf as ServiceFactory<St, Req>>::Service, <Sf as ServiceFactory<St, Req>>::InitError>where
Sf: ServiceFactory<St, Req>,
Creates a child service and binds it to this context’s readiness scope.
Uses the current pipeline state to create the service, then invokes its
Service::bind hook before returning it. Successful creation invalidates
this scope’s cached readiness and its ancestors. Failed or cancelled
creation does not bind a service or change this scope’s dependencies.
The service is returned unchanged, without checking readiness or adding
a cache. Use crate::ready_factory for independent child readiness.
Retain the created service for subsequent calls rather than recreating it
on each readiness check. Other scopes must bind it separately before use.
Sourcepub fn ready<'b, S, Req>(
&self,
svc: &'b S,
) -> impl Future<Output = Result<(), <S as Service<St, Req>>::Error>> + use<'a, 'b, Svc, St, S, Req>where
S: Service<St, Req>,
pub fn ready<'b, S, Req>(
&self,
svc: &'b S,
) -> impl Future<Output = Result<(), <S as Service<St, Req>>::Error>> + use<'a, 'b, Svc, St, S, Req>where
S: Service<St, Req>,
Waits until svc is ready to process a request.
A crate::ReadyBinding reuses successful readiness until a subscribed
source changes. A raw service is checked on every invocation.
Sourcepub fn call<S, Req>(
&self,
svc: &'a S,
req: Req,
) -> impl Future<Output = Result<<S as Service<St, Req>>::Res, <S as Service<St, Req>>::Error>>where
S: Service<St, Req>,
pub fn call<S, Req>(
&self,
svc: &'a S,
req: Req,
) -> impl Future<Output = Result<<S as Service<St, Req>>::Res, <S as Service<St, Req>>::Error>>where
S: Service<St, Req>,
Waits for svc to become ready, then calls it.
Uses the nearest upstream scope carried by this context. A valid cache
allows immediate dispatch; otherwise, only svc is checked, without
marking the enclosing scope ready. A crate::ReadyBinding also
checks its own cache when called directly, independently of this scope.
Calls do not consume cached readiness.
Calling after an explicit readiness check is cheap: a valid cache avoids
another readiness check or readiness allocation, while invalidation before
dispatch is still observed.
Raw Self::ready calls do not populate the upstream cache; use
crate::ready() for independent child caching.
Readiness checks made while handling a call, including this one, do not wait for another binding’s pipeline-level readiness check, which may itself be waiting for resources held by the call. They may run concurrently with it.
use ntex_service::{Ctx, Service};
struct Forward<S> {
inner: S,
}
impl<S: Service<(), usize>> Service<(), usize> for Forward<S> {
type Res = S::Res;
type Error = S::Error;
ntex_service::forward!((), inner);
async fn call(&self, req: usize, ctx: Ctx<'_, Self>) -> Result<S::Res, S::Error> {
ctx.call(&self.inner, req).await
}
}