feat(client): retry + circuit-breaker (#39)#244
Merged
Conversation
Adds opt-in retry-with-exponential-backoff and a three-state circuit breaker (CLOSED/OPEN/HALF_OPEN) to HttpTestClient so chaos tests do not fail on the transient connection errors they are designed to create. ABI-conservative: new RetryPolicy / CircuitBreakerConfig structs default to no-op behaviour (max_retries=0, failure_threshold=0), so existing single-argument constructions compile and behave identically. Only status==0 (transport failure) is retried; deliberate 4xx/5xx replies are returned unchanged. Covered by test/src/test_http_client_retry_unit.cpp using the in-process MockServer pattern from test_http_client_unit.cpp. Closes #39 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- Move `run_with_retry` from anonymous-namespace free function to a private
static template member of `HttpTestClient` so it can refer to the
private `CircuitBreaker` nested type without exposing it publicly.
- Change template signature from `Fn&& fn` (forwarding reference never
std::forward'd, and invoked in a loop) to `Fn func` by value — safe for
the lambda callsites and removes the clang-tidy warning.
- Rename constructor and ctor params `cb` → `breaker_cfg` to satisfy
identifier-length checks on public API.
- Add `std::uint8_t` underlying-type to `BreakerState` enum.
- Drop redundant `{}` initializer on `opened_at_` (value-initialised by
default).
- Rename test-local identifiers (`p`/`c`/`cb`) to descriptive names and
add `const` to value-only locals to satisfy
`misc-const-correctness` / `readability-identifier-length`.
- Use `auto` for the static_cast<double>() result of base_delay_ms.
- clang-format -i over all three changed files for the format check.
a1609bc to
bb0ffd9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
HttpTestClient.status == 0) are retried; deliberate 4xx/5xx responses pass through unchanged.min(base * mult^attempt, cap) * uniform(0.5, 1.5); no sleep after the final attempt.RetryPolicyandCircuitBreakerConfigstructs default to no-op behaviour (max_retries = 0,failure_threshold = 0), so existing single-argHttpTestClient("...")constructions compile and behave identically — the change is ABI-conservative on purpose (cf. PR refactor: expose timeout constants as constructor params (#81) #231 cascade).ABI / migration
<chrono>and forward-declares a privateCircuitBreaker(pimpl-style), so<atomic>,<mutex>,<random>stay in the.cpp.Test plan
test/src/test_http_client_retry_unit.cppcovers default-policy constants, retry exhaustion against refused ports, HTTP-error non-retry, breaker trip/half-open/disabled paths via the in-processMockServerpattern.projectcharybdis_testsbuilds and discovers all new tests.HttpTestClientUnit/HttpTestClientOnlinetests still pass (default policy means zero behaviour drift).Closes #39
Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com