Expand description
gRPC health-checking service for connectrpc.
Wire-compatible with grpc.health.v1.Health, so grpc_health_probe,
grpcurl, Kubernetes’ gRPC liveness probes, and any other client of the
standard gRPC health protocol just work.
Non-empty unregistered services return Err(ConnectError::not_found(_))
from both Check and Watch; the empty service auto-subscribes on
Watch and returns Serving on Check by default — see
HealthService’s # Unknown services section for how this relates
to the gRPC Health spec.
§Cargo features
client(on by default) — re-exports the generatedHealthClientfor in-process probes, integration tests, and sidecar tooling. Pulls inconnectrpc’sclientfeature (the HTTP/2 transport stack). Server-only deployments drop it withconnectrpc-health = { version = "0.7", default-features = false };use connectrpc_health::HealthClientthen becomes an unresolved import (the type is gone), but the dependency graph losesconnectrpc/client.
§Writing a custom Checker
StaticChecker covers most servers. If you implement Checker
yourself (e.g. report NotServing while a database connection is
down), note that the default watch implementation returns
Unimplemented — fine for kubelet / grpc_health_probe (they only
call Check), but service meshes (Linkerd, Istio) and gRPC clients
with health-based load balancing call Watch too. Override the
method if your probes need it. See Checker::watch for details.
§Quick start
use connectrpc::Router;
use connectrpc_health::{install_static, Status};
// In real code, pass the generated `*_SERVICE_NAME` constant —
// the literal below is a stand-in.
let (router, health) = install_static(Router::new(), [
"acme.user.v1.UserService",
]);
// Later, when something goes wrong. `set_status` errors on unknown
// names; here the name was just registered above, so `.expect`
// documents the invariant.
health
.set_status("acme.user.v1.UserService", Status::NotServing)
.expect("registered above");
// ...and at shutdown. `shutdown()` flips every registered service,
// including the empty whole-process entry seeded on construction.
health.shutdown();For custom logic (probing a database, propagating dependency state),
implement Checker directly and wrap it in HealthService::new
/ HealthService::from_arc; see the next section for the one extra
call that path needs.
§Request limits
A HealthCheckRequest is one service name, so the health routes do not
need the multi-megabyte request ceiling a connectrpc service allows by
default. This crate sizes Check and Watch to MAX_REQUEST_BYTES
(16 KiB) per request through per-route
Limits — see request_limits for the exact
profile — and a larger request is refused with resource_exhausted
before it reaches the Checker. The profile replaces the
service-wide limits on these two routes, whether those are looser or
tighter.
install_staticappliesrequest_limitsfor you.- Registering a
HealthServiceany other way — the generatedHealthExt::registerorRouter::add_service— does not, so follow it withapply_request_limits(router,request_limits()). - To tune the health routes specifically, call
apply_request_limitswith your ownLimitsafter either path; the later call wins.
use connectrpc::{Limits, Router};
use connectrpc_health::{apply_request_limits, install_static};
let (router, health) = install_static(Router::new(), ["acme.user.v1.UserService"]);
// Optional: hold the health routes to 1 KiB instead of the bundled 16 KiB.
let router = apply_request_limits(
router,
Limits::default()
.with_max_request_body_size(1024)
.with_max_message_size(1024),
);Modules§
- wire
- Re-exports of the generated
grpc.health.v1wire types — request and response messages,ServingStatus, the*_SPECconstants. Downstream crates can build probe loops without regenerating the proto.
Structs§
- Health
Client client - Generated client for calling a
grpc.health.v1.Healthserver. - Health
Service - gRPC-compatible health service backed by a user-supplied
Checker. - Static
Checker - In-memory checker backed by a
HashMap<String, Status>. - Status
Stream - A stream of
Statusupdates produced byChecker::watch. - Unknown
Service Error - Returned by
StaticChecker::set_statuswhen called with a name that hasn’t been registered. Register first viaStaticChecker::register(or up front viaStaticChecker::with_services) to avoid this.
Enums§
- Status
- Health status of a single service or of the whole server.
Constants§
- HEALTH_
SERVICE_ NAME - Fully-qualified protobuf service name:
"grpc.health.v1.Health". Full service name for this service. - MAX_
REQUEST_ BYTES - The largest request message, in bytes on the wire and after
decompression, the health routes accept under
request_limits: 16 KiB.
Traits§
- Checker
- Reports the health of services on this server.
- Health
Ext - Generated extension trait that adds
.register(router)to anyArc<S> where S: Health. Import it to register aHealthService. Extension trait for registering a service implementation with a Router.
Functions§
- apply_
request_ limits - Set the
CheckandWatchroutes onroutertolimits, replacing the service-wide limits for them (whether looser or tighter). - install_
static - One-line installation for the static-checker happy path.
- request_
limits - The per-route
Limitsthis crate sets on itsCheckandWatchroutes: message size capped atMAX_REQUEST_BYTES(the request body at that plus the 5-byte envelope framed protocols add), decode budget left at theconnectrpcdefault.