pub struct HealthProbe { /* private fields */ }Expand description
A cheap, cloneable handle reporting the service’s HealthState and how many of its
subscriptions are consuming.
Obtained from RunningApp::health; backed by a
watch channel written only on lifecycle transitions and when a
subscription’s loop starts or ends, so reading is a lock-free borrow and nothing runs on the
per-delivery hot path. The probe outlives shutdown (which
consumes the app handle), so a sibling HTTP task keeps answering with the terminal state.
Watch semantics: observers always see the latest state; rapid transitions (ShuttingDown
immediately followed by Stopped) may coalesce, so poll state for snapshots
and use changed only as a wake-up.
Dropping the RunningApp without calling shutdown detaches the service
(per the crate rule that destructors never block); a probe then keeps reporting the last
observed state, and changed resolves only if a fail-fast failure still
flips the state or a subscription still ends.
Implementations§
Source§impl HealthProbe
impl HealthProbe
Sourcepub fn state(&self) -> HealthState
pub fn state(&self) -> HealthState
A snapshot of the current state.
Sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
Whether the service is currently running (the healthy answer for a liveness endpoint).
Sourcepub fn subscriptions(&self) -> SubscriptionCount
pub fn subscriptions(&self) -> SubscriptionCount
How many subscriptions are consuming, and how many the service started.
A subscription whose stream ended while the service runs leaves the active count, under
the default SubscriptionEnd::Log. The lifecycle state stays
Running: the service decides what a partly working service
reports outside.
§Examples
use axum::Router;
use axum::extract::State;
use axum::http::StatusCode;
use axum::routing::get;
use ruststream::memory::MemoryBroker;
use ruststream::runtime::{AppInfo, HealthProbe, RustStream};
use tokio::net::TcpListener;
/// Ready while every subscription the service started still consumes.
async fn readyz(State(health): State<HealthProbe>) -> StatusCode {
let count = health.subscriptions();
if health.is_running() && count.active == count.started {
StatusCode::OK
} else {
StatusCode::SERVICE_UNAVAILABLE
}
}
let app =
RustStream::new(AppInfo::new("svc", "0.1.0")).register_broker(MemoryBroker::new());
let running = app.start().await?;
let routes = Router::new()
.route("/readyz", get(readyz))
.with_state(running.health());
axum::serve(TcpListener::bind("0.0.0.0:8080").await?, routes)
.with_graceful_shutdown(running.stopping())
.await?;
running.shutdown().await?;Sourcepub async fn changed(&mut self) -> HealthState
pub async fn changed(&mut self) -> HealthState
Waits for the next change, a lifecycle transition or a subscription starting or ending, and returns the state.
If the service was detached (the app handle dropped without shutdown) and no further
change can arrive, the future stays pending forever rather than spinning the caller’s
loop; pair it with state when a snapshot is enough.
§Cancel safety
Cancel-safe: dropping the future loses nothing; a fresh call observes the same channel.
Trait Implementations§
Source§impl Clone for HealthProbe
impl Clone for HealthProbe
Auto Trait Implementations§
impl Freeze for HealthProbe
impl RefUnwindSafe for HealthProbe
impl Send for HealthProbe
impl Sync for HealthProbe
impl Unpin for HealthProbe
impl UnsafeUnpin for HealthProbe
impl UnwindSafe for HealthProbe
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request