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
4 changes: 4 additions & 0 deletions .changeset/unavailable-limiter-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

fix(relay): answer `error: rate limiter unavailable` when event limiter fails (#1300)
2 changes: 1 addition & 1 deletion crates/nostream-relay/src/handlers/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
9 changes: 7 additions & 2 deletions crates/nostream-relay/tests/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 4 additions & 3 deletions docs/rewrite/decisions/0004-wire-transport.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading