Which HTTP Library Does FastProxy Use for High-Performance Forwarding?
FastProxy uses github.com/valyala/fasthttp as its high-performance HTTP library, falling back to the standard net/http package only when the FastHttpEnable configuration flag is disabled.
FastProxy (available at kingson4wu/fast_proxy) is a Go-based proxy solution designed for efficient request forwarding. Understanding which HTTP library powers its performance-critical paths is essential for optimizing throughput and latency in production deployments.
What HTTP Library Does FastProxy Use?
FastProxy employs a dual-client architecture, but its high-performance path is built exclusively on fasthttp, a zero-allocation HTTP client library optimized for speed. When configured for performance, FastProxy routes requests through fasthttp.Client methods (Do, DoTimeout, DoDeadline), leveraging connection pooling and memory-efficient request handling.
Primary High-Performance Client (fasthttp)
According to the source code in inproxy/internal/proxy/httpclientProxy.go, FastProxy initializes a fasthttp.Client instance with configurable connection limits:
fastHttpClient = &fasthttp.Client{
MaxConnsPerHost: c.HttpClientMaxIdleConnsPerHost(),
}
This client handles the request-forwarding hot path when server.Config().FastHttpEnable() returns true.
Fallback Standard Library Client (net/http)
For compatibility scenarios where fasthttp is not desired, FastProxy maintains a standard net/http.Client instance:
tr := &http.Transport{
MaxIdleConns: c.HttpClientMaxIdleConns(),
MaxIdleConnsPerHost: c.HttpClientMaxIdleConnsPerHost(),
}
client = &http.Client{Transport: tr}
How FastProxy Implements HTTP Client Selection
The selection logic resides in inproxy/internal/proxy/httpclientProxy.go. The proxy checks the configuration flag at request time to determine which client executes the forward.
The FastHttpEnable Configuration Flag
In common/config/config.go, the FastHttpEnable boolean flag controls the execution path. This flag is accessed via server.Config().FastHttpEnable() (defined in common/server/server.go).
Runtime Client Selection
The DoProxy function implements the conditional logic:
func DoProxy(w http.ResponseWriter, r *http.Request) {
// ... setup code ...
if server.Config().FastHttpEnable() {
fastDoProxy(w, r) // High-performance fasthttp path
return
}
// Standard net/http client execution follows
client.Do(req)
}
This branching occurs at lines 70-73 of httpclientProxy.go, ensuring zero-cost abstraction when the fast path is disabled.
FastHTTP Implementation Details
When the fast path is active, FastProxy executes fastDoProxy, which constructs fasthttp.Request objects using memory pools to minimize GC pressure.
Connection Management and Timeouts
The fasthttp.Client supports three execution modes based on deadline requirements:
Do– Standard synchronous executionDoTimeout– Timeout-based cancellation (milliseconds)DoDeadline– Absolute deadline-based cancellation (Unix timestamp)
Request Forwarding Example
The fastDoProxy function demonstrates zero-allocation request building:
func fastDoProxy(w http.ResponseWriter, r *http.Request) {
// Acquire pooled request/response objects
reqProxy := fasthttp.AcquireRequest()
resProxy := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(reqProxy)
defer fasthttp.ReleaseResponse(resProxy)
// Build the proxied request
reqProxy.SetRequestURI(callUrl)
reqProxy.Header.SetMethod(r.Method)
for k, v := range r.Header {
reqProxy.Header.Set(k, v[0])
}
reqProxy.SetBody(bodyBytes)
// Execute with appropriate timeout strategy
var err error
if deadTime > 0 {
err = fastHttpClient.DoDeadline(reqProxy, resProxy, time.Unix(deadTime, 0))
} else if timeout > 0 {
err = fastHttpClient.DoTimeout(reqProxy, resProxy, time.Duration(timeout)*time.Millisecond)
} else {
err = fastHttpClient.Do(reqProxy, resProxy)
}
// ... response handling ...
}
Summary
- FastProxy uses
github.com/valyala/fasthttpfor high-performance request forwarding, as implemented ininproxy/internal/proxy/httpclientProxy.go - The
fastHttpEnableconfiguration flag toggles between fasthttp and standard library clients at runtime - Connection pooling is configured via
MaxConnsPerHostin the fasthttp client initialization - Three timeout strategies are supported:
Do,DoTimeout, andDoDeadline - The implementation uses memory pools (
AcquireRequest/ReleaseRequest) to achieve zero-allocation proxing
Frequently Asked Questions
What is the default HTTP client in FastProxy?
The default behavior depends on the FastHttpEnable configuration value. By default, if this flag is false or unset, FastProxy uses the standard net/http.Client. When enabled, it routes all traffic through the fasthttp.Client for better performance.
How do I enable the fasthttp client in FastProxy?
Set the FastHttpEnable configuration option to true in your FastProxy configuration. This flag is evaluated at runtime in the DoProxy function within inproxy/internal/proxy/httpclientProxy.go. Once enabled, the proxy immediately begins using the fastDoProxy execution path.
Why does FastProxy use fasthttp instead of net/http?
fasthttp provides zero-allocation HTTP handling and more efficient connection pooling compared to the standard library. For proxy workloads that handle high concurrent request volumes, fasthttp significantly reduces GC pressure and improves throughput, which is why FastProxy selects it for the performance-critical path.
Does FastProxy support request timeouts with fasthttp?
Yes. The fasthttp.Client implementation in httpclientProxy.go supports both relative timeouts (DoTimeout) and absolute deadlines (DoDeadline). The code checks for deadTime (Unix timestamp) first, then falls back to millisecond timeout values, executing the appropriate method on fastHttpClient accordingly.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →