From 05d6ae1ffea7ea964750b83f0c227c29cc91a6f6 Mon Sep 17 00:00:00 2001 From: Avanti Date: Wed, 2 Dec 2026 16:43:34 +0530 Subject: [PATCH] fix(relay): answer error when event rate limiter is unavailable (#1300) --- .changeset/unavailable-limiter-error.md | 4 ++++ crates/nostream-relay/src/handlers/event.rs | 2 +- crates/nostream-relay/tests/limits.rs | 9 +++++++-- docs/rewrite/decisions/0004-wire-transport.md | 7 ++++--- 4 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 .changeset/unavailable-limiter-error.md diff --git a/.changeset/unavailable-limiter-error.md b/.changeset/unavailable-limiter-error.md new file mode 100644 index 000000000..2668a43b9 --- /dev/null +++ b/.changeset/unavailable-limiter-error.md @@ -0,0 +1,4 @@ +--- +--- + +fix(relay): answer `error: rate limiter unavailable` when event limiter fails (#1300) diff --git a/crates/nostream-relay/src/handlers/event.rs b/crates/nostream-relay/src/handlers/event.rs index ecbda9d76..b9ed056cd 100644 --- a/crates/nostream-relay/src/handlers/event.rs +++ b/crates/nostream-relay/src/handlers/event.rs @@ -98,7 +98,7 @@ pub async fn handle(event: Event, context: &HandlerContext) { Err(error) => { // Fail closed: an unavailable limiter must not leave the gate open. tracing::warn!(error = %error, "rate limiter unavailable; refusing the event"); - context.send_ok(event.id, false, "rate-limited: slow down", None); + context.send_ok(event.id, false, "error: rate limiter unavailable", None); return; } } diff --git a/crates/nostream-relay/tests/limits.rs b/crates/nostream-relay/tests/limits.rs index eca60b120..646220fd1 100644 --- a/crates/nostream-relay/tests/limits.rs +++ b/crates/nostream-relay/tests/limits.rs @@ -991,9 +991,14 @@ async fn a_failing_event_limiter_limits_the_event_without_a_backoff() { assert_eq!( frame, - format!(r#"["OK","{}",false,"rate-limited: slow down"]"#, event.id), - "fail closed, with no backoff to promise" + format!( + r#"["OK","{}",false,"error: rate limiter unavailable"]"#, + event.id + ), + "fail closed with an error reason and no backoff" ); + assert_eq!(fixture.metrics.events_rejected("error"), 1); + assert_eq!(fixture.metrics.events_rejected("rate-limited"), 0); } #[test] diff --git a/docs/rewrite/decisions/0004-wire-transport.md b/docs/rewrite/decisions/0004-wire-transport.md index 835d7bd23..23090dd72 100644 --- a/docs/rewrite/decisions/0004-wire-transport.md +++ b/docs/rewrite/decisions/0004-wire-transport.md @@ -42,9 +42,10 @@ it refuses, and an EWMA key expires at `ceil(period / 1000)` seconds while a sli entry drops inclusively at `now - period`. Both are implemented in process over a `Mutex`, behind a `Limiter` trait, because the behaviour under test is the window's arithmetic and the wiring, not the transport to a shared store. A store error is -fail-closed: the event is answered `rate-limited: slow down` with no backoff, which is -what the capture's catch does. A later job that wants a shared store implements -`Limiter`; nothing in the handlers changes. +fail-closed: the event is answered `error: rate limiter unavailable` with no backoff, +matching the capture (`src/handlers/event-message-handler.ts:136-144`) so clients do not +back off as if rate-limited for an outage on the relay's side. A later job that wants a +shared store implements `Limiter`; nothing in the handlers changes. `RateLimits` keeps the four levels (`connection`, `message`, `event`, `invite`) and their whitelists, and `Limiter` is injectable so the failure path is exercised rather