From 870144a7cc0f3bc38ccb07f275b02cc69644537a Mon Sep 17 00:00:00 2001
From: Chris Edwards <279314+ckedwards@users.noreply.github.com>
Date: Thu, 12 Nov 2026 13:14:03 -0500
Subject: [PATCH] fix(rivetkit-core): keep one sleep rearm task per actor
Every finished HTTP request spawned a task that waits for the actor's
in-flight requests to reach zero before re-arming the sleep timer. Under
steady concurrent load the count rarely reaches zero, so these tasks piled
up, one per request, and every idle notification (from any counter wired to
idle_notify) woke all of them to re-check and wait again. CPU per call grew
with the backlog: 0.7 ms with one request in flight, 18 to 43 ms with 4 to
64, and worse the longer the load lasted.
A request now spawns the waiter only if none is pending: an AtomicBool on
SleepState is claimed before spawning and released when the waiter returns,
just before it resets the sleep timer, so a request finishing in that gap is
covered by the reset that follows. With the change, a call costs 0.7 ms of
CPU at every concurrency (1 to 64 in flight, held for 60 s).
Fixes #5991
---
.../packages/rivetkit-core/src/actor/sleep.rs | 20 +++++++++++++++++++
.../rivetkit-core/src/registry/http.rs | 5 +++++
2 files changed, 25 insertions(+)
diff --git a/rivetkit-rust/packages/rivetkit-core/src/actor/sleep.rs b/rivetkit-rust/packages/rivetkit-core/src/actor/sleep.rs
index 08cafde6f2..cb9e2294c0 100644
--- a/rivetkit-rust/packages/rivetkit-core/src/actor/sleep.rs
+++ b/rivetkit-rust/packages/rivetkit-core/src/actor/sleep.rs
@@ -46,6 +46,9 @@ pub(crate) struct SleepState {
// than a destroy.
pub(super) stop_error: Mutex