valyala/fasthttp
View on GitHubClient: add DoContext method for context.Context-aware request execution
Open
#2198 opened on May 1, 2026
feature requesthelp wanted
Description
Feature Request
Is your feature request related to a problem? Please describe.
The fasthttp client provides DoTimeout() and DoDeadline(), but lacks a DoContext(ctx context.Context, req, resp) method. This means:
- No cancellation propagation: If a parent context is cancelled (e.g., an HTTP handler's
ctx.Done()), the in-flight fasthttp request continues until its own timeout or completion. - No deadline inheritance: If a caller has an existing context with a deadline (e.g., from gRPC or an upstream request), there's no way to pass it through.
- No metadata attachment: Tracing systems (OpenTelemetry, etc.) rely on context to propagate trace IDs.
This forces users to either:
- Use
net/http(which hasClient.Do(req.WithContext(ctx))) and lose performance - Manually create a
context.WithTimeoutand callDoDeadline, losing parent cancellation
Describe the solution you'd like
Add a DoContext method to both Client and HostClient:
func (c *Client) DoContext(ctx context.Context, req *Request, resp *Response) error
func (c *HostClient) DoContext(ctx context.Context, req *Request, resp *Response) error
Behavior:
- If
ctxis already cancelled, returnctx.Err()immediately - If
ctxhas a deadline, use it (respecting any existing timeout set on the Client) - If the context is cancelled during dial, reading, or writing, abort the operation and return
ctx.Err() - The implementation can reuse the existing timeout/deadline logic in the TCP dialer, which already uses
context.Contextinternally
Describe alternatives you've considered
- Using
DoDeadlinewithctx.Deadline()— works but loses cancellation propagation (only deadline is respected, notctx.Done()) - Wrapping
DoTimeoutin a goroutine withselect { case <-ctx.Done() }— racy, doesn't actually cancel the underlying I/O - Using
net/http— defeats the purpose of using fasthttp
Additional context
- The internal
tcpdialeralready usescontext.Contextfor DNS resolution (Resolver.LookupIPAddr(ctx, host)) Server.ShutdownWithContext(ctx)already exists, showing the project embraces context for lifecycle management- Other Go HTTP clients (net/http, resty, go-resty) all support context-aware requests