Remove bad_event_ratelimiter entries after expiration.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2026-02-26 13:06:36 +00:00
parent 1a434443b9
commit 0933943dd6
3 changed files with 27 additions and 4 deletions

View File

@@ -4,7 +4,7 @@ use std::{
time::Duration,
};
use futures::{FutureExt, StreamExt};
use futures::{FutureExt, StreamExt, TryFutureExt};
use ruma::{
CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedEventId, RoomId, RoomVersionId,
ServerName, api::federation::event::get_event,
@@ -89,6 +89,7 @@ where
if next_id == id {
pdus.push((pdu, Some(json)));
}
self.cancel_back_off(&next_id);
} else {
self.back_off(&next_id);
}
@@ -152,8 +153,11 @@ async fn fetch_auth_chain(
.services
.federation
.execute(origin, get_event::v1::Request { event_id: next_id.clone() })
.inspect_err(|e| debug_error!(?next_id, "Failed to fetch event: {e}"))
.inspect_ok(|_| {
self.cancel_back_off(&next_id);
})
.await
.inspect_err(|e| debug_error!("Failed to fetch event {next_id}: {e}"))
else {
debug_warn!("Backing off from {next_id}");
self.back_off(&next_id);
@@ -168,6 +172,7 @@ async fn fetch_auth_chain(
continue;
};
self.cancel_back_off(&next_id);
if calculated_event_id != next_id {
warn!(
"Server didn't return event id we requested: requested: {next_id}, we got \

View File

@@ -170,6 +170,9 @@ pub async fn handle_incoming_pdu<'a>(
warn!("Prev {prev_id} failed: {e}");
self.back_off(prev_id);
})
.inspect_ok(|()| {
self.cancel_back_off(prev_id);
})
.map(|_| self.services.server.check_running())
})
.boxed()

View File

@@ -76,7 +76,16 @@ impl crate::Service for Service {
}
#[implement(Service)]
fn back_off(&self, event_id: &EventId) {
fn cancel_back_off(&self, event_id: &EventId) -> bool {
self.bad_event_ratelimiter
.write()
.expect("locked")
.remove(event_id)
.is_some()
}
#[implement(Service)]
fn back_off(&self, event_id: &EventId) -> bool {
use hash_map::Entry::{Occupied, Vacant};
match self
@@ -87,9 +96,11 @@ fn back_off(&self, event_id: &EventId) {
{
| Vacant(e) => {
e.insert((Instant::now(), 1));
true
},
| Occupied(mut e) => {
*e.get_mut() = (Instant::now(), e.get().1.saturating_add(1));
false
},
}
}
@@ -106,7 +117,11 @@ fn is_backed_off(&self, event_id: &EventId, range: Range<Duration>) -> bool {
return false;
};
continue_exponential_backoff(range.start, range.end, time.elapsed(), tries)
if !continue_exponential_backoff(range.start, range.end, time.elapsed(), tries) {
return false;
}
true
}
#[implement(Service)]