Hedged HTTP client which helps to reduce tail latency at scale.
See paper Tail at Scale by Jeffrey Dean, Luiz André Barroso. In short: the client first sends one request, but then sends an additional request after a timeout if the previous hasn't returned an answer in the expected time. The client cancels remaining requests once the first result is received.
Thanks to Bohdan Storozhuk for the review and powerful hints.
- Simple API.
- Easy to integrate.
- Optimized for speed.
- Clean and tested code.
- Supports
http.Client
andhttp.RoundTripper
. - Dependency-free.
Go version 1.16+
go get github.com/cristalhq/hedgedhttp
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://google.com", http.NoBody)
if err != nil {
panic(err)
}
timeout := 10 * time.Millisecond
upto := 7
client := &http.Client{Timeout: time.Second}
hedged, err := hedgedhttp.NewClient(timeout, upto, client)
if err != nil {
panic(err)
}
// will take `upto` requests, with a `timeout` delay between them
resp, err := hedged.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
Also see examples: examples_test.go.
See these docs.