Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(personhog): shed identity requests with resource exhausted (#121835)

Nick Bestbdce88c31c1d
diff --git a/rust/personhog-common/src/grpc.rs b/rust/personhog-common/src/grpc.rs
index e29aba9e1ca1..cd731366a385 100644
--- a/rust/personhog-common/src/grpc.rs
+++ b/rust/personhog-common/src/grpc.rs
@@ -534,6 +534,7 @@ impl<F> PinnedDrop for GrpcMetricsFuture<F> {
pub struct GrpcLoadShedLayer {
max_requests: usize,
in_flight: Arc<AtomicUsize>,
+ grpc_status: HeaderValue,
}
impl GrpcLoadShedLayer {
@@ -541,8 +542,18 @@ impl GrpcLoadShedLayer {
Self {
max_requests,
in_flight: Arc::new(AtomicUsize::new(0)),
+ grpc_status: grpc_status_header(tonic::Code::Unavailable),
}
}
+
+ pub fn with_code(mut self, code: tonic::Code) -> Self {
+ self.grpc_status = grpc_status_header(code);
+ self
+ }
+}
+
+fn grpc_status_header(code: tonic::Code) -> HeaderValue {
+ HeaderValue::from(code as i32)
}
impl<S> Layer<S> for GrpcLoadShedLayer {
@@ -553,6 +564,7 @@ impl<S> Layer<S> for GrpcLoadShedLayer {
inner: service,
max_requests: self.max_requests,
in_flight: self.in_flight.clone(),
+ grpc_status: self.grpc_status.clone(),
}
}
}
@@ -562,6 +574,7 @@ pub struct GrpcLoadShedService<S> {
inner: S,
max_requests: usize,
in_flight: Arc<AtomicUsize>,
+ grpc_status: HeaderValue,
}
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for GrpcLoadShedService<S>
@@ -600,7 +613,7 @@ where
let response = Response::builder()
.status(200)
.header("content-type", "application/grpc")
- .header("grpc-status", "14") // UNAVAILABLE
+ .header("grpc-status", self.grpc_status.clone())
.header("grpc-message", "Server at capacity")
.header(NOT_APPLIED_HEADER, "load_shed")
.body(ResBody::default())
@@ -742,32 +755,40 @@ mod tests {
#[tokio::test]
async fn sheds_at_capacity() {
- let in_flight = Arc::new(AtomicUsize::new(0));
- let layer = GrpcLoadShedLayer {
- max_requests: 2,
- in_flight: in_flight.clone(),
- };
- in_flight.store(2, Ordering::Relaxed);
-
- let mut svc = layer.layer(OkService);
-
- let resp = svc.call(grpc_request("/pkg.Svc/GetPerson")).await.unwrap();
- assert_eq!(resp.headers().get("grpc-status").unwrap(), "14");
- assert_eq!(
- resp.headers().get("grpc-message").unwrap(),
- "Server at capacity"
- );
- assert_eq!(resp.headers().get(NOT_APPLIED_HEADER).unwrap(), "load_shed");
- // Shed path: increment then immediate decrement, net zero change
- assert_eq!(in_flight.load(Ordering::Relaxed), 2);
+ for (layer, expected_status) in [
+ (GrpcLoadShedLayer::new(2), "14"),
+ (
+ GrpcLoadShedLayer::new(2).with_code(tonic::Code::ResourceExhausted),
+ "8",
+ ),
+ ] {
+ let in_flight = Arc::new(AtomicUsize::new(0));
+ let layer = GrpcLoadShedLayer {
+ in_flight: in_flight.clone(),
+ ..layer
+ };
+ in_flight.store(2, Ordering::Relaxed);
+
+ let mut svc = layer.layer(OkService);
+
+ let resp = svc.call(grpc_request("/pkg.Svc/GetPerson")).await.unwrap();
+ assert_eq!(resp.headers().get("grpc-status").unwrap(), expected_status);
+ assert_eq!(
+ resp.headers().get("grpc-message").unwrap(),
+ "Server at capacity"
+ );
+ assert_eq!(resp.headers().get(NOT_APPLIED_HEADER).unwrap(), "load_shed");
+ // Shed path: increment then immediate decrement, net zero change
+ assert_eq!(in_flight.load(Ordering::Relaxed), 2);
+ }
}
#[tokio::test]
async fn counter_decrements_on_completion() {
let in_flight = Arc::new(AtomicUsize::new(0));
let layer = GrpcLoadShedLayer {
- max_requests: 10,
in_flight: in_flight.clone(),
+ ..GrpcLoadShedLayer::new(10)
};
let mut svc = layer.layer(OkService);
@@ -784,8 +805,8 @@ mod tests {
async fn counter_decrements_on_drop() {
let in_flight = Arc::new(AtomicUsize::new(0));
let layer = GrpcLoadShedLayer {
- max_requests: 10,
in_flight: in_flight.clone(),
+ ..GrpcLoadShedLayer::new(10)
};
let mut svc = layer.layer(PendingService);
@@ -801,8 +822,8 @@ mod tests {
async fn shed_response_does_not_decrement_existing() {
let in_flight = Arc::new(AtomicUsize::new(5));
let layer = GrpcLoadShedLayer {
- max_requests: 5,
in_flight: in_flight.clone(),
+ ..GrpcLoadShedLayer::new(5)
};
let mut svc = layer.layer(OkService);
diff --git a/rust/personhog-identity/src/main.rs b/rust/personhog-identity/src/main.rs
index 58bb6a14211d..87ffde0cada4 100644
--- a/rust/personhog-identity/src/main.rs
+++ b/rust/personhog-identity/src/main.rs
@@ -488,7 +488,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// callers not to send pathological frames, same as the replica.
if let Err(e) = server
.layer(GrpcMetricsLayer::default().with_processing_time_header())
- .layer(GrpcLoadShedLayer::new(max_concurrent_requests))
+ // Identity pods share one fleet-wide load, so a shed pod means the
+ // fleet is saturated. RESOURCE_EXHAUSTED stops the router from
+ // retrying the shed on another saturated pod.
+ .layer(
+ GrpcLoadShedLayer::new(max_concurrent_requests)
+ .with_code(tonic::Code::ResourceExhausted),
+ )
.add_service(
PersonHogIdentityServer::new(service)
.accept_compressed(CompressionEncoding::Gzip)
diff --git a/rust/personhog-router/src/grpc_http.rs b/rust/personhog-router/src/grpc_http.rs
index d5ad53d35890..1f31f8326c9c 100644
--- a/rust/personhog-router/src/grpc_http.rs
+++ b/rust/personhog-router/src/grpc_http.rs
@@ -123,6 +123,10 @@ mod tests {
Some("load_shed")
);
assert_eq!(unapplied_refusal_reason(&marked(Code::Ok)), None);
+ assert_eq!(
+ unapplied_refusal_reason(&marked(Code::ResourceExhausted)),
+ None
+ );
assert_eq!(unapplied_refusal_reason(&marked(Code::Internal)), None);
assert_eq!(
unapplied_refusal_reason(&grpc_error_response(