dio_resilience 0.2.0
dio_resilience: ^0.2.0 copied to clipboard
Coordinated Dio interceptor for retry, timeout, circuit breaker, rate limit, fallback, and OAuth token refresh. Strategies share state so they don't fight each other.
Changelog #
0.2.0 — 2026-05-05 #
New strategies:
- Circuit breaker: closed / open / half-open states with rolling failure window, configurable cooldown and half-open probe budget. Per-host keying with custom
keyExtractorhook.CircuitOverride.disabled / threshold / cooldownper-request overrides..noCircuit()and.circuitKey(...)Optionsshortcuts. Throws typedCircuitOpenExceptionwhen short-circuiting. - Rate limiter: pluggable algorithm via
RateLimitAlgorithm.tokenBucket(...)(burst-friendly) orRateLimitAlgorithm.slidingWindow(...)(strict).RateLimitMode.wait(block until token, respecting total budget) orRateLimitMode.fail(throw immediately). Per-host keying withkeyExtractorhook.RateLimitOverride.disabled / bypass..noRateLimit()/.rateLimitKey(...)shortcuts. ThrowsRateLimitExceededException. - Hedging: speculative parallel requests after a configurable inter-hedge delay; first success wins, losers cancelled via
CancelToken. Defaults to GET/HEAD only viashouldHedgepredicate.HedgingOverride.disabled / maxParallel(n) / delay(d)..noHedging()/.hedge(...)shortcuts. Counts as a single attempt for the retry strategy so retry still fires on a failed hedging round. - Fallback: walks an ordered list of
FallbackHandlers; supports cannedResponse(FallbackHandler.response(...)), alternate endpoint refire (FallbackHandler.endpoint(url, method:)), and conditional wrappers (FallbackHandler.predicate(...)). Endpoint fallback re-fires through the same Dio so other strategies still apply, with a guard flag preventing recursive fallback.FallbackOverride.disabled / response / handlers..noFallback()/.fallbackTo(...)/.fallbackHandlers(...)shortcuts.
Pipeline:
- New
ResilienceStrategy.onDispatchhook that lets a strategy own request dispatch (used by hedging). Default returnsnull; existing strategies are unaffected. - Strategy execution order:
auth → rateLimit → circuit → hedging → retry → timeout → fallback. - Total-budget exhaustion now surfaces as
PipelineTimeoutExceptionfromonErrorinstead of propagating the underlying transport error.
Bug fixes (carried over from 0.1):
RefreshFailedExceptionis now thrown directly from the auth strategy instead of being wrapped in a genericDioException. The unawaited single-flight completer error no longer escapes when there are no concurrent waiters.overridesFor()now accepts bothRequestOptionsandOptions, removing the need foras RequestOptionscasts at call sites.RetryCondition.responseBodyContainsandRetryCondition.responseFieldcorrectly evaluate when the test/eval input only specifies a response body (no status code).
0.1.0 — 2026-04-28 #
Initial release.
- Auth strategy with single-flight token refresh, multi-account support, AuthState stream.
- Retry strategy with all backoff variants (constant / linear / exponential / exponential-jitter), Retry-After header support.
- Timeout strategy with per-attempt and total budget enforcement.
- Typed override system: ResilienceOverrides + sealed RetryOverride / AuthOverride / TimeoutOverride classes.
- RetryCondition typed conditions (status, statusIn, statusBetween, networkError, timeout, dioMessageContains, responseBodyContains, responseField, predicate, plus or/and/negated combinators).
- Options extensions: .resilience(), .noRetry(), .noAuth(), .withTimeout(), .withAccount(), .retryAttempts(), .dontRetryOn(), .dontRetryWhen(), .retryOnlyWhen().
- Stub override types for circuit breaker, rate limiter, hedging, fallback (v0.2).