Skip to content

feat: Add retry state for RETRY-spec backoff - #522

Merged
jsonbailey merged 7 commits into
mainfrom
jb/sdk-2792/retry-state
Sep 17, 2026
Merged

jsonbailey merged 7 commits into
mainfrom
jb/sdk-2792/retry-state

Conversation

@jsonbailey

@jsonbailey jsonbailey commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds the retry state machine that the FDv1 data sources will use for RETRY-spec conformance. Pure addition — nothing imports it yet, so it can be reviewed against the spec without reading any data-source code.

RetryState answers one question: how long to wait before the next attempt. It serves all four data sources; streaming and polling differ only in their construction parameters and their reset policy.

The algorithm

Every failure is classified NORMAL or UNEXPECTED. 400, 408, 429 and all 5xx are normal; every other 4xx, including 401 and 403, is unexpected. Only an HTTP status can be unexpected — every network and TLS failure is normal.

A normal failure advances the delay on the current curve. An unexpected failure raises both bounds and keeps them raised until the reset condition is met, so a normal failure that follows cannot lower them.

streaming, normal:    1 -> 2 -> 4 -> 8 -> 16 -> 30 -> 30 ...
streaming, extended:  300 -> 600 -> 1200 -> 2400 -> 3600 -> 3600 ...
polling, normal:      poll_interval, flat — no escalation
polling, extended:    max(300, poll_interval) doubling to max(3600, poll_interval)

Each delay is then reduced by a jitter of up to half itself, and never falls below the caller's operating cadence — poll_interval for polling, absent for streaming.

Two things that are easy to get wrong

A backoff wait applies to a retry, not to every operation. record_success restores the operating cadence even while the retry state is still raised, so a recovered service is polled at its normal rate immediately. Another SDK shipped without this: after an outage its first successful poll still waited twenty minutes.

Clearing the retry state is separate from restoring the cadence. record_success does the latter; _reset_if_due does the former, and only when the reset policy is satisfied. Streaming's policy is 60 seconds of continuous healthy operation, polling's is two consecutive successes.

_reset_if_due is called from record_failure because nothing runs while a stream is healthy — the moment the 60-second threshold is crossed is otherwise unobservable, so the next failure is the only place it can be noticed.

Why TLS failures are normal

The narrow classification was built first and reverted. All SDK traffic is HTTPS, so a genuine certificate problem cannot be distinguished from an ordinary transient fault across platforms — a peer sending FIN mid-handshake surfaces differently from one sending RST, and the classification would depend on which an intermediary happened to send. A connection flapping faster than the reset threshold then ratchets to the hour ceiling with no way out.

Testing

make test: 1599 passed. make lint: clean across 226 files.

71 tests covering both delay ladders exactly, jitter bounds, ceiling stickiness, both reset policies, the cadence floor, and the classification table. No test sleeps: the clock and the jitter are patched at the module level, so the 60-second reset and a 20-cycle flapping scenario both run instantly.


Note

Overview
Introduces a standalone RetryState module (ldclient/impl/retry.py) that will drive FDv1 data-source reconnect/poll timing. Nothing in production imports it yet.

Failures are classified as normal vs unexpected (via classify_http_status: most 4xx except 400/408/429 are unexpected; 5xx and transport errors stay normal). Backoff doubles to a ceiling, subtracts up to half jitter, and never waits less than the operating cadence (zero for streaming, poll_interval for polling). An unexpected failure switches to an extended delay ladder (5–60 minutes) that stays sticky until reset.

Streaming uses for_streaming with exponential normal delays (default 1s → 30s cap) and resets after 60s of healthy operation. Polling uses for_polling with flat normal retries at the poll interval and resets after two consecutive successes. record_success restores the healthy cadence immediately even while extended state is still raised; clearing extended state is separate and gated by the reset policy.

Adds test_retry.py (delay tables, jitter bounds, reset policies, invalid config fallbacks) plus test_util helpers to patch retry’s time/random without sleeping.

Reviewed by Cursor Bugbot for commit 4d300c4. Bugbot is set up for automated code reviews on this repo. Configure here.

@jsonbailey
jsonbailey marked this pull request as ready for review September 14, 2026 18:30
@jsonbailey
jsonbailey requested a review from a team as a code owner September 14, 2026 18:30
Comment thread ldclient/impl/retry.py Outdated
Comment thread ldclient/impl/retry.py

# The delay bounds of the extended regime, in seconds. A component enters the
# extended regime after an unexpected failure.
EXTENDED_INITIAL_DELAY = 5 * 60

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it the norm in python that all time values / APIs are float seconds?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. time.monotonic(), time.sleep(), threading.Event.wait() and asyncio.sleep() all take or return float seconds, so a float is what every call site here needs. Config already exposes poll_interval and initial_reconnect_delay as float seconds too, so this matches what callers already pass in.

Comment thread ldclient/impl/retry.py

# An upper bound on the backoff exponent, so a long outage cannot overflow the
# delay computation. Any real ceiling is reached long before this.
_MAX_BACKOFF_EXPONENT = 30

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are limited by the float seconds data type that is used in the SDK so those bound protections won't work.

Comment thread ldclient/impl/retry.py Outdated
Comment thread ldclient/impl/retry.py
self._extended_initial_delay = extended_initial_delay
self._extended_ceiling = extended_ceiling
self._reset_policy = reset_policy
self._operating_cadence = operating_cadence

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this operating_cadence is to help with the normal second poll after the first success? Can't this be calculated based on normal_initial_delay? It seems like redundant state? Maybe I'm not understanding the usage of operating_cadence and initial_delay set to different non-zero values.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can think of operating cadence as the time between normal healthy operations and the normal_initial_delay as the starting point for delays between failed operations. For polling, these are the same, but for streaming the default cadence is 0s, but the failed starts at 1s or whatever the user configures so that a backoff can actually be calculated.

Comment thread ldclient/impl/retry.py
"""
self._reset_policy.note_healthy()
self._reset_if_due()
self._next_delay = self._operating_cadence

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be 0? I think this indicates a logical flaw in how the values are flowing through / overriding each other.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be 0, we can meet if it would be helpful to talk through this but 0 is intentional (for streaming).

Comment thread ldclient/impl/retry.py Outdated
Comment thread ldclient/impl/retry.py Outdated
Comment thread ldclient/impl/retry.py
log.warning(
"%s must be a positive, finite number of seconds; using the default of %ss"
% (name, default)
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are logger instances ever passed around in the code base? This static call stands out to me coming from our other code bases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, its is the standard in this repo to pull from the import. And it is different then most other languages.

…loor

Streaming's operating cadence is zero rather than absent, so a healthy
stream schedules no wait and a retry delay has no floor. Polling is the
only data source that reads next_delay as a DelaySource, and its cadence
floor keeps that from reaching zero.

Make the seven observational properties private. No data source reads
them; they are test instrumentation, and as public API they invite
callers to attach logic to unsynchronized state.
The RETRY spec binds a normal ceiling and an extended ceiling. Use those
words for the configured bounds so the parameters read as the spec does,
and keep the _delay suffix the initial-delay names already use.

The mutable maxDelay keeps its name: Requirement 1.3.2 calls it that.
…e delay

The exponent driver was _n and a second counter held the name attempts,
which is what Requirement 1.4.1 calls the exponent driver. A reviewer
reading self.attempts against the spec was reading the wrong field.

The second counter is gone. It had no reader outside tests, not even a
logger, and every test that used it recorded only normal failures --
where the two counters are equal by construction.
@jsonbailey
jsonbailey merged commit e734558 into main Sep 17, 2026
15 checks passed
@jsonbailey
jsonbailey deleted the jb/sdk-2792/retry-state branch September 17, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants