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(capture): update v1 Retry-After handling for spec parity (#57896)

Eli Reisman2fec09622e8f
diff --git a/rust/capture/src/v1/analytics/constants.rs b/rust/capture/src/v1/analytics/constants.rs
index 56e463157ddb..20c1e6873964 100644
--- a/rust/capture/src/v1/analytics/constants.rs
+++ b/rust/capture/src/v1/analytics/constants.rs
@@ -10,8 +10,9 @@ pub const ACCEPT_JSON: HeaderValue = HeaderValue::from_static("application/json"
/// Accepted compression encodings advertised in Accept-Encoding response header.
pub const ACCEPT_ENCODING_ALL: HeaderValue = HeaderValue::from_static("gzip, deflate, br, zstd");
-/// Default Retry-After value (seconds) sent on rate-limited or server-error responses.
-pub const DEFAULT_RETRY_AFTER_SECS: HeaderValue = HeaderValue::from_static("60");
+/// Retry-After value (seconds) sent on retryable error responses (429, 408, 5xx).
+/// SDKs are expected to layer their own jittered exponential backoff on top of this floor.
+pub const DEFAULT_RETRY_AFTER_SECS: HeaderValue = HeaderValue::from_static("1");
// ---------------------------------------------------------------------------
// Supported content encodings
diff --git a/rust/capture/src/v1/error.rs b/rust/capture/src/v1/error.rs
index 4a92180809ce..c96031c0081e 100644
--- a/rust/capture/src/v1/error.rs
+++ b/rust/capture/src/v1/error.rs
@@ -85,14 +85,10 @@ pub enum Error {
#[error("body read stalled after receiving {0} bytes")]
BodyReadTimeout(usize),
- // 402 - billing_error (non-retryable, unlike 429)
+ // 402 - billing_error (non-retryable)
#[error("billing limit exceeded")]
BillingLimitExceeded,
- // 429 - rate_limit_error
- #[error("rate limited: {0}")]
- RateLimited(String),
-
// 500 - server_error
#[error("internal server error: {0}")]
InternalError(String),
@@ -133,7 +129,6 @@ impl Error {
Self::UnsupportedContentType(_) => "unsupported_content_type",
Self::UnsupportedEncoding(_) => "unsupported_encoding",
Self::BillingLimitExceeded => "billing_limit_exceeded",
- Self::RateLimited(_) => "rate_limited",
Self::InternalError(_) => "internal_error",
Self::ServiceUnavailable(_) => "service_unavailable",
Self::GatewayTimeout => "gateway_timeout",
@@ -146,7 +141,6 @@ impl Error {
Self::RequestParsingError(_) => "Failed to parse request body.".to_string(),
Self::InvalidApiToken(_) => "The provided API token is not valid.".to_string(),
Self::BillingLimitExceeded => "Billing quota exceeded. Events are being dropped. Upgrade your plan to resume ingestion.".to_string(),
- Self::RateLimited(_) => "Rate limit exceeded.".to_string(),
Self::InternalError(_) | Self::ServiceUnavailable(_) | Self::GatewayTimeout => self
.status_code()
.canonical_reason()
@@ -189,8 +183,7 @@ impl Error {
| Self::PayloadTooLarge(_)
| Self::UnsupportedContentType(_)
| Self::UnsupportedEncoding(_)
- | Self::BillingLimitExceeded
- | Self::RateLimited(_) => Level::WARN,
+ | Self::BillingLimitExceeded => Level::WARN,
// body read timeout: error-level despite being 4xx
Self::BodyReadTimeout(_) => Level::ERROR,
@@ -256,8 +249,6 @@ impl Error {
Self::BillingLimitExceeded => StatusCode::PAYMENT_REQUIRED,
- Self::RateLimited(_) => StatusCode::TOO_MANY_REQUESTS,
-
Self::InternalError(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::ServiceUnavailable(_) => StatusCode::SERVICE_UNAVAILABLE,
@@ -271,7 +262,15 @@ impl Error {
headers.insert(header::ACCEPT, ACCEPT_JSON);
headers.insert(header::ACCEPT_ENCODING, ACCEPT_ENCODING_ALL);
- if let Self::RateLimited(_) = self {
+ // 402 (BillingLimitExceeded) is intentionally non-retryable per RFC, so no Retry-After.
+ if matches!(
+ self,
+ Self::RequestTimeout
+ | Self::BodyReadTimeout(_)
+ | Self::InternalError(_)
+ | Self::ServiceUnavailable(_)
+ | Self::GatewayTimeout
+ ) {
headers.insert(header::RETRY_AFTER, DEFAULT_RETRY_AFTER_SECS);
}
@@ -366,6 +365,7 @@ mod tests {
use super::*;
use axum::body::to_bytes;
use axum::http::header::RETRY_AFTER;
+ use rstest::rstest;
async fn response_body(resp: Response) -> serde_json::Value {
let bytes = to_bytes(resp.into_body(), 65_536).await.unwrap();
@@ -385,14 +385,6 @@ mod tests {
assert!(body["error_uri"].is_string());
}
- #[tokio::test]
- async fn rate_limited_includes_retry_after() {
- let err = Error::RateLimited("too many requests".into());
- let resp = err.into_response();
- assert_eq!(resp.status(), StatusCode::TOO_MANY_REQUESTS);
- assert!(resp.headers().contains_key(RETRY_AFTER));
- }
-
#[tokio::test]
async fn unauthorized_status() {
let err = Error::InvalidApiToken("bad".into());
@@ -424,4 +416,40 @@ mod tests {
let resp = err.into_response();
assert_eq!(resp.status(), StatusCode::GATEWAY_TIMEOUT);
}
+
+ #[rstest]
+ #[case::request_timeout(Error::RequestTimeout, StatusCode::REQUEST_TIMEOUT)]
+ #[case::body_read_timeout(Error::BodyReadTimeout(1024), StatusCode::REQUEST_TIMEOUT)]
+ #[case::internal_error(Error::InternalError("boom".into()), StatusCode::INTERNAL_SERVER_ERROR)]
+ #[case::service_unavailable(Error::ServiceUnavailable("nope".into()), StatusCode::SERVICE_UNAVAILABLE)]
+ #[case::gateway_timeout(Error::GatewayTimeout, StatusCode::GATEWAY_TIMEOUT)]
+ #[tokio::test]
+ async fn retryable_errors_emit_retry_after_one_second(
+ #[case] err: Error,
+ #[case] expected_status: StatusCode,
+ ) {
+ let resp = err.into_response();
+ assert_eq!(resp.status(), expected_status);
+ let value = resp
+ .headers()
+ .get(RETRY_AFTER)
+ .expect("Retry-After header must be present on retryable errors");
+ assert_eq!(value.to_str().unwrap(), "1");
+ }
+
+ #[rstest]
+ #[case::billing_limit(Error::BillingLimitExceeded)]
+ #[case::invalid_api_token(Error::InvalidApiToken("bad".into()))]
+ #[case::empty_batch(Error::EmptyBatch)]
+ #[case::payload_too_large(Error::PayloadTooLarge("big".into()))]
+ #[case::unsupported_content_type(Error::UnsupportedContentType("text/plain".into()))]
+ #[tokio::test]
+ async fn non_retryable_errors_omit_retry_after(#[case] err: Error) {
+ let resp = err.into_response();
+ assert!(
+ !resp.headers().contains_key(RETRY_AFTER),
+ "Retry-After must NOT be present on non-retryable errors (status {})",
+ resp.status()
+ );
+ }
}