Commit e72d8b2
committed
perf(daemon): idle exponential backoff in poll loop
Daemon was polling every 10s forever, burning ~17K "is there work?"
API calls per daemon per day even when idle. Five idle daemons alone
accounted for the observed 100K Worker requests / day we see on prod,
about 80 percent of which returned nothing.
Change: track whether each tick did real work - a dispatched task, a
reaped session, a resumed rate-limit backoff - and use that to drive
an exponential idle backoff capped at 120s. Any real event resets
to opts.pollInterval.
Implementation in packages/cli/src/daemon/loop.ts:
- Renamed backoffMs to errorBackoffMs for clarity. Only used by
handleTickError. Unchanged semantics.
- New idleBackoffMs starts at opts.pollInterval. resetIdleBackoff()
snaps back to base; bumpIdleBackoff() multiplies by 1.5 capped at
MAX_IDLE_BACKOFF_MS (120s).
- tick() snapshots pool.activeCount before reap/kill/resume phases.
After dispatch, if dispatched OR activeCount changed -> reset,
else bump. Pool saturation (>= maxConcurrent) leaves backoff
untouched so a full daemon doesn't accelerate against a 409.
- nextPollDelay() uses idleBackoffMs as baseline, still clamped
against the nearest rate-limit resume deadline.
- onSlotFreed() and resumeRateLimitedSessions() reset backoff
before scheduling, so any real signal snaps the daemon back.
Backoff curve: 10 -> 15 -> 22 -> 33 -> 50 -> 75 -> 112 -> 120 seconds.
Reaches ceiling after ~7 idle ticks (~70s of real time).
Expected: idle daemon request count drops from ~17K to ~3-5K per day,
about 4-6x reduction. At 5 daemons: 100K/day -> 15-25K/day.
16 new tests added in tests/loop-idle-backoff.test.ts covering the
backoff state machine, reset triggers, pool saturation path, and
nextPollDelay clamping. All 2044 tests pass.1 parent ab3ea32 commit e72d8b2
2 files changed
Lines changed: 445 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
32 | 39 | | |
33 | 40 | | |
34 | 41 | | |
| |||
39 | 46 | | |
40 | 47 | | |
41 | 48 | | |
42 | | - | |
| 49 | + | |
| 50 | + | |
43 | 51 | | |
44 | 52 | | |
45 | 53 | | |
| |||
49 | 57 | | |
50 | 58 | | |
51 | 59 | | |
52 | | - | |
| 60 | + | |
| 61 | + | |
53 | 62 | | |
54 | 63 | | |
55 | 64 | | |
| |||
63 | 72 | | |
64 | 73 | | |
65 | 74 | | |
| 75 | + | |
66 | 76 | | |
67 | 77 | | |
68 | 78 | | |
| |||
80 | 90 | | |
81 | 91 | | |
82 | 92 | | |
| 93 | + | |
83 | 94 | | |
84 | 95 | | |
85 | 96 | | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
86 | 105 | | |
87 | 106 | | |
88 | 107 | | |
| |||
97 | 116 | | |
98 | 117 | | |
99 | 118 | | |
100 | | - | |
| 119 | + | |
101 | 120 | | |
102 | 121 | | |
103 | 122 | | |
104 | 123 | | |
105 | 124 | | |
106 | 125 | | |
107 | | - | |
| 126 | + | |
| 127 | + | |
108 | 128 | | |
109 | 129 | | |
110 | 130 | | |
| |||
116 | 136 | | |
117 | 137 | | |
118 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
119 | 144 | | |
120 | 145 | | |
121 | 146 | | |
| |||
129 | 154 | | |
130 | 155 | | |
131 | 156 | | |
| 157 | + | |
| 158 | + | |
132 | 159 | | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
133 | 163 | | |
134 | 164 | | |
135 | 165 | | |
136 | 166 | | |
137 | | - | |
| 167 | + | |
138 | 168 | | |
139 | 169 | | |
140 | 170 | | |
141 | 171 | | |
142 | | - | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
143 | 179 | | |
144 | 180 | | |
145 | 181 | | |
| |||
153 | 189 | | |
154 | 190 | | |
155 | 191 | | |
156 | | - | |
| 192 | + | |
157 | 193 | | |
158 | 194 | | |
159 | | - | |
| 195 | + | |
160 | 196 | | |
161 | | - | |
| 197 | + | |
162 | 198 | | |
163 | 199 | | |
164 | 200 | | |
| |||
0 commit comments