Commit 17f7b4e
authored
feat: Add FDv2 synchronizer fallback and recovery conditions (#297)
## What this adds
Timed conditions that the FDv2 data source orchestrator (a later PR)
observes alongside the active synchronizer's results to drive tier
transitions:
- **Fallback condition**: starts its timer when the synchronizer reports
an interrupted status and cancels it when a change set arrives. If it
fires (120 seconds by default), the orchestrator moves to the next
available synchronizer. Terminal statuses (shutdown, terminal error,
goodbye) do not arm the timer — the orchestrator reacts to those
immediately rather than waiting out a fallback period — and repeated
interruptions do not extend the deadline; the period counts from the
first interruption.
- **Recovery condition**: starts when observed and ignores results. If
it fires (300 seconds by default), the orchestrator returns to the
primary synchronizer.
`getConditions` selects which conditions apply: none when only one
synchronizer is available (nowhere to fall back to), fallback only for
the primary, and both for a non-primary synchronizer. `ConditionGroup`
merges its members and emits the first to fire.
The timeout defaults match the other client-side FDv2 implementations.
## Why streams instead of futures
The Java and C++ implementations model conditions as one-shot futures,
and both had the same leak (fixed in java-core by `c27bf26` /
launchdarkly/java-core#163): a future's listeners can never be detached,
only released by completion, so an orchestration loop that races a
long-lived pending future per result accumulates an irremovable listener
garland per change set — unbounded on a healthy primary. Dart futures
have the identical property (measured: ~559 B retained per race against
a pending future), but Dart also has the primitive those platforms lack:
cancellable stream subscriptions.
Each condition therefore exposes a single-subscription
`Stream<ConditionType>` that emits **at most once** and then closes
(closing without emitting if the condition is closed first). Lifetimes
are scoped to the subscription: self-starting timers begin when the
stream is listened to, and cancelling the subscription closes the
condition and releases its timers. One bounded edge remains by design —
informing a never-listened group can arm a fallback timer until its
timeout elapses or `close()` is called — and is documented on the API.
The orchestrator PR consumes these with one subscription per
synchronizer run, closes in a `finally`, and includes a soak test
asserting bounded memory across a sustained stream of results.
## Testing
Tests run inside a `package:fake_async` zone, advancing time with
`elapse` and asserting timer state through `pendingTimers` — no timer
injection in the production code. They cover timer start/cancel behavior
for both condition kinds, terminal statuses not arming the fallback
timer, the fallback deadline not extending on repeated interruptions,
at-most-once emission including when two member timers contend in the
same instant, close semantics, subscription cancellation releasing
condition and group timers, group merging and inform broadcast, and the
`getConditions` selection rules.
SDK-2186
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> New timing logic will drive synchronizer failover when wired into the
orchestrator; behavior is isolated in new modules with thorough tests
and no production integration in this PR.
>
> **Overview**
> Adds **FDv2 synchronizer tier-transition conditions** for a future
orchestrator: timed signals that emit **fallback** (switch to the next
synchronizer after sustained interruption) or **recovery** (return to
primary after running on a backup).
>
> **Fallback** arms on `interrupted`, cancels on a change set, ignores
terminal statuses, and does not reset the deadline on repeated
interruptions (defaults: 120s). **Recovery** starts when the condition
is observed and ignores results (default 300s). Conditions expose
**single-subscription streams** (at-most-one emit) so subscriptions can
be cancelled without the listener-retention issues of racing futures.
>
> `ConditionGroup` merges members (first fire wins), broadcasts `inform`
to all members, and `getConditions` picks an empty group, fallback-only
for primary, or fallback+recovery for non-primary when multiple
synchronizers exist.
>
> Adds **`fake_async`** tests for timer behavior, close/cancel
semantics, group contention, and `getConditions` rules.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
4fc80f6. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent cb6a5c0 commit 17f7b4e
2 files changed
Lines changed: 597 additions & 0 deletions
File tree
- packages/common_client
- lib/src/data_sources/fdv2
- test/data_sources/fdv2
Lines changed: 256 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
0 commit comments