Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ pin-project-lite = "0.2.16"
proc-macro2 = { version = "1" }
prometheus = "0.14.0"
prost = "0.14.1"
prost-types = "0.14.1"
quinn = "0.11.12"
quote = { version = "1" }
rcgen = { version = "0.14.10", default-features = false, features = ["aws_lc_rs"] }
Expand Down
3 changes: 2 additions & 1 deletion tako-rs-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mime_guess = { workspace = true }
parking_lot = { workspace = true }
pin-project-lite = { workspace = true }
prost = { workspace = true, optional = true }
prost-types = { workspace = true, optional = true }
rustls = { workspace = true, optional = true }
scc = { workspace = true }
send_wrapper = { workspace = true, optional = true }
Expand Down Expand Up @@ -78,7 +79,7 @@ simd = ["tako-rs/simd", "simd-sonic", "simd-json-impl"]
simd-sonic = ["tako-rs/simd-sonic", "dep:sonic-rs"]
simd-json-impl = ["tako-rs/simd-json-impl"]
tls = ["tako-rs/tls", "dep:rustls"]
grpc = ["tako-rs/grpc", "protobuf", "dep:base64"]
grpc = ["tako-rs/grpc", "protobuf", "dep:base64", "dep:prost-types"]
queue-cron = ["tako-rs/queue-cron", "dep:cron", "dep:chrono"]
http3 = ["tako-rs/http3", "dep:rustls"]
webtransport = ["tako-rs/webtransport", "http3"]
Expand Down
125 changes: 118 additions & 7 deletions tako-rs-core/src/grpc/health.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,107 @@
//! Per-service status map for a `grpc.health.v1.Health` service; serve it from your own
//! `Check` and `Watch` handlers.
//! `grpc.health.v1.Health`: register Check, Watch, and List on an ordinary router.

use std::sync::Arc;

use futures_util::Stream;
use scc::HashMap as SccHashMap;
use tokio::sync::watch;

use super::GrpcDeadline;
use super::GrpcRequest;
use super::GrpcResponse;
use super::GrpcServerStream;
use super::GrpcStatus;
use super::GrpcStatusCode;
use crate::router::Router;

mod protocol;
pub use protocol::*;

/// Encoded health schema for reflection registration.
pub const FILE_DESCRIPTOR_SET: &[u8] = include_bytes!("proto/health.bin");

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[repr(i32)]
pub enum ServingStatus {
Unknown,
Unknown = 0,
#[default]
Serving,
NotServing,
ServiceUnknown,
Serving = 1,
NotServing = 2,
ServiceUnknown = 3,
}

/// Service statuses; clones share one map.
#[derive(Clone, Default)]
#[derive(Clone)]
pub struct HealthRegistry {
inner: Arc<SccHashMap<String, ServingStatus>>,
updates: Arc<watch::Sender<()>>,
}

impl Default for HealthRegistry {
fn default() -> Self {
Self {
inner: Arc::default(),
updates: Arc::new(watch::channel(()).0),
}
}
}

impl HealthRegistry {
/// Registers Check, Watch, and List.
pub fn register(&self, router: &mut Router) {
let registry = self.clone();
router.post(
"/grpc.health.v1.Health/Check",
move |deadline: Option<GrpcDeadline>, request: GrpcRequest<HealthCheckRequest>| {
let status = registry.status_of(&request.message.service);
async move {
if deadline.is_some_and(|deadline| deadline.remaining().is_zero()) {
return GrpcResponse::error(GrpcStatusCode::DeadlineExceeded, "deadline exceeded");
}
if status == ServingStatus::ServiceUnknown {
GrpcResponse::error(GrpcStatusCode::NotFound, "unknown service")
} else {
GrpcResponse::ok(HealthCheckResponse {
status: status as i32,
})
}
}
},
);
let registry = self.clone();
router.post(
"/grpc.health.v1.Health/Watch",
move |deadline: Option<GrpcDeadline>, request: GrpcRequest<HealthCheckRequest>| {
let stream = registry.watch(request.message.service);
async move { GrpcServerStream::new(stream).with_deadline(deadline) }
},
);
let registry = self.clone();
router.post(
"/grpc.health.v1.Health/List",
move |deadline: Option<GrpcDeadline>, _: GrpcRequest<HealthListRequest>| {
let statuses = registry
.snapshot()
.into_iter()
.map(|(name, status)| {
(
name,
HealthCheckResponse {
status: status as i32,
},
)
})
.collect();
async move {
if deadline.is_some_and(|deadline| deadline.remaining().is_zero()) {
return GrpcResponse::error(GrpcStatusCode::DeadlineExceeded, "deadline exceeded");
}
GrpcResponse::ok(HealthListResponse { statuses })
}
},
);
}

pub fn new() -> Self {
Self::default()
}
Expand All @@ -29,6 +110,7 @@ impl HealthRegistry {
pub fn set_status(&self, service: impl Into<String>, status: ServingStatus) {
// `insert_sync` would keep the first status ever set.
self.inner.upsert_sync(service.into(), status);
self.updates.send_replace(());
}

/// `ServiceUnknown` for a service that was never set.
Expand All @@ -47,4 +129,33 @@ impl HealthRegistry {
});
out
}

/// Yields the current status immediately, then changes until dropped.
/// Unknown services stay subscribed; slow readers see the latest status.
pub fn watch<S: Into<String>>(
&self,
service: S,
) -> impl Stream<Item = Result<HealthCheckResponse, GrpcStatus>> + Send + 'static + use<S> {
let state = (self.clone(), self.updates.subscribe(), service.into(), None);
futures_util::stream::unfold(
state,
|(registry, mut updates, service, mut previous)| async move {
loop {
if previous.is_some() {
updates.changed().await.ok()?;
}
let status = registry.status_of(&service);
if previous != Some(status) {
previous = Some(status);
return Some((
Ok(HealthCheckResponse {
status: status as i32,
}),
(registry, updates, service, previous),
));
}
}
},
)
}
}
36 changes: 36 additions & 0 deletions tako-rs-core/src/grpc/health/protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Wire messages for <https://github.com/grpc/grpc-proto/blob/master/grpc/health/v1/health.proto>.

/// Service to check; the empty name selects the whole server.
#[derive(Clone, PartialEq, prost::Message)]
pub struct HealthCheckRequest {
#[prost(string, tag = "1")]
pub service: String,
}

#[derive(Clone, Copy, PartialEq, prost::Message)]
pub struct HealthCheckResponse {
#[prost(enumeration = "health_check_response::ServingStatus", tag = "1")]
pub status: i32,
}

pub mod health_check_response {
/// The wire default is `Unknown`; the registry's convenience enum defaults to `Serving`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, prost::Enumeration)]
#[repr(i32)]
pub enum ServingStatus {
Unknown = 0,
Serving = 1,
NotServing = 2,
ServiceUnknown = 3,
}
}

#[derive(Clone, Copy, PartialEq, prost::Message)]
pub struct HealthListRequest {}

/// Snapshot of explicitly registered services.
#[derive(Clone, PartialEq, prost::Message)]
pub struct HealthListResponse {
#[prost(btree_map = "string, message", tag = "1")]
pub statuses: std::collections::BTreeMap<String, HealthCheckResponse>,
}
Loading
Loading