You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Hedging Middleware
Adds a **hedging** resilience middleware to `seatbelt` that reduces tail
latency by launching additional concurrent requests when the original is
slow. The first acceptable result wins; remaining in-flight requests are
cancelled.
### How it works
1. The original request is sent immediately.
2. After a configurable delay, additional hedging attempts launch
concurrently.
3. A user-provided recovery callback classifies each result —
non-recoverable results are returned immediately.
### Hedging delay
| Method | Behavior |
|--------|----------|
| **`hedging_delay(duration)`** | Waits a fixed duration before each
hedging attempt *(default: 500ms)* |
| **`hedging_delay(Duration::ZERO)`** | Launches all hedging attempts at
once |
| **`hedging_delay_with(fn)`** | Computes delay per attempt via callback
|
### Configuration
Uses a **type-state builder** pattern — `clone_input` and `recovery` are
enforced at compile time before the layer can be constructed.
```rust
let stack = (
Hedging::layer("my_hedging", &context)
.clone_input()
.recovery_with(|result, _| match result {
Ok(_) => RecoveryInfo::never(),
Err(_) => RecoveryInfo::retry(),
})
.hedging_delay(Duration::from_secs(1))
.max_hedged_attempts(2),
Execute::new(my_operation),
);
Runtime configuration is also supported via HedgingConfig
(serde-compatible):
.config(&HedgingConfig {
enabled: true,
hedging_delay: Duration::from_millis(500),
max_hedged_attempts: 1,
})
```
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
*[`config`][__link23]: Loading settings from a [JSON file][__link24].
157
159
158
160
## Features
159
161
160
162
This crate provides several optional features that can be enabled in your `Cargo.toml`:
161
163
162
-
***`timeout`** - Enables the [`timeout`][__link23] middleware for canceling long-running operations.
163
-
***`retry`** - Enables the [`retry`][__link24] middleware for automatically retrying failed operations with
164
+
***`timeout`** - Enables the [`timeout`][__link25] middleware for canceling long-running operations.
165
+
***`retry`** - Enables the [`retry`][__link26] middleware for automatically retrying failed operations with
164
166
configurable backoff strategies, jitter, and recovery classification.
165
-
***`breaker`** - Enables the [`breaker`][__link25] middleware for preventing cascading failures.
166
-
***`fallback`** - Enables the [`fallback`][__link26] middleware for replacing invalid output with a
167
+
***`hedging`** - Enables the [`hedging`][__link27] middleware for reducing tail latency via additional
168
+
concurrent requests with configurable delay modes.
169
+
***`breaker`** - Enables the [`breaker`][__link28] middleware for preventing cascading failures.
170
+
***`fallback`** - Enables the [`fallback`][__link29] middleware for replacing invalid output with a
167
171
user-defined alternative.
168
172
***`metrics`** - Exposes the OpenTelemetry metrics API for collecting and reporting metrics.
169
173
***`logs`** - Enables structured logging for resilience middleware using the `tracing` crate.
170
174
***`serde`** - Enables `serde::Serialize` and `serde::Deserialize` implementations for
171
175
configuration types.
172
-
***`tower-service`** - Enables [`tower_service::Service`][__link27] trait implementations for all
176
+
***`tower-service`** - Enables [`tower_service::Service`][__link30] trait implementations for all
173
177
resilience middleware.
174
178
175
179
@@ -178,29 +182,32 @@ This crate provides several optional features that can be enabled in your `Cargo
178
182
This crate was developed as part of <ahref="../..">The Oxidizer Project</a>. Browse this crate's <ahref="https://github.com/microsoft/oxidizer/tree/main/crates/seatbelt">source code</a>.
Copy file name to clipboardExpand all lines: crates/seatbelt/examples/README.md
+3-2Lines changed: 3 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,14 @@
1
1
# Examples
2
2
3
-
Runnable examples covering each middleware and common composition patterns:
3
+
Examples covering each middleware and common composition patterns:
4
4
5
5
-[`timeout`](https://github.com/microsoft/oxidizer/blob/main/crates/seatbelt/examples/timeout.rs): Basic timeout that cancels long-running operations.
6
-
-[`timeout_advanced`](https://github.com/microsoft/oxidizer/blob/main/crates/seatbelt/examples/timeout_advanced.rs): Dynamic timeout durations and timeout callbacks.
6
+
-[`timeout_advanced`](https://github.com/microsoft/oxidizer/blob/main/crates/seatbelt/examples/timeout_advanced.rs): Dynamic timeout duration and timeout callbacks.
7
7
-[`retry`](https://github.com/microsoft/oxidizer/blob/main/crates/seatbelt/examples/retry.rs): Automatic retry with input cloning and recovery classification.
8
8
-[`retry_advanced`](https://github.com/microsoft/oxidizer/blob/main/crates/seatbelt/examples/retry_advanced.rs): Custom input cloning with attempt metadata injection.
9
9
-[`retry_outage`](https://github.com/microsoft/oxidizer/blob/main/crates/seatbelt/examples/retry_outage.rs): Input restoration from errors when cloning is not possible.
10
10
-[`breaker`](https://github.com/microsoft/oxidizer/blob/main/crates/seatbelt/examples/breaker.rs): Circuit breaker that monitors failure rates.
11
+
-[`hedging`](https://github.com/microsoft/oxidizer/blob/main/crates/seatbelt/examples/hedging.rs): Hedging slow requests with parallel attempts to reduce tail latency.
11
12
-[`fallback`](https://github.com/microsoft/oxidizer/blob/main/crates/seatbelt/examples/fallback.rs): Substitutes default values for invalid outputs.
12
13
-[`resilience_pipeline`](https://github.com/microsoft/oxidizer/blob/main/crates/seatbelt/examples/resilience_pipeline.rs): Composing retry and timeout with metrics.
0 commit comments