MCPcopy Index your code
hub / github.com/cristalhq/hedgedhttp

github.com/cristalhq/hedgedhttp @v0.9.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.9.1 ↗ · + Follow
98 symbols 461 edges 5 files 28 documented · 29% 5 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

hedgedhttp

build-img pkg-img reportcard-img coverage-img version-img

Hedged HTTP client which helps to reduce tail latency at scale.

Rationale

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.

Acknowledge

Thanks to Bohdan Storozhuk for the review and powerful hints.

Features

  • Simple API.
  • Easy to integrate.
  • Optimized for speed.
  • Clean and tested code.
  • Supports http.Client and http.RoundTripper.
  • Dependency-free.

Install

Go version 1.16+

go get github.com/cristalhq/hedgedhttp

Example

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.

Documentation

See these docs.

License

MIT License.

Extension points exported contracts — how you extend this code

RateLimiter (Interface)
by example https://pkg.go.dev/golang.org/x/time/rate [1 implementers]
examples_test.go
NextFn (FuncType)
NextFn represents a function that is called for each HTTP request for retrieving hedging options.
hedged.go
ErrorFormatFunc (FuncType)
ErrorFormatFunc is called by MultiError to return the list of errors as a string.
hedged.go

Core symbols most depended-on inside this repo

ActualRoundTrips
called by 17
stats.go
Do
called by 15
hedged.go
NewClientAndStats
called by 14
hedged.go
RequestedRoundTrips
called by 11
stats.go
FailedRoundTrips
called by 11
stats.go
CanceledByUserRoundTrips
called by 11
stats.go
CanceledSubRequests
called by 11
stats.go
New
called by 6
hedged.go

Shape

Function 47
Method 31
Struct 15
FuncType 3
Interface 1
TypeAlias 1

Languages

Go100%

Modules by API surface

hedged_test.go28 symbols
hedged.go27 symbols
stats.go19 symbols
examples_test.go19 symbols
hedged_bench_test.go5 symbols

For agents

$ claude mcp add hedgedhttp \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact