Skip to content

Performance Optimization

Configuration Preset Comparison

MetricDefaultSecurePerformanceMinimal
Request timeout180s15s60s180s
MaxIdleConns502010010
MaxConnsPerHost105202
MaxRetries3130
MaxResponseBodySize10MB5MB50MB1MB
HTTP/2OnOnOnOn
CookiesOffOffOnOff
SSRF ProtectionOnOnOnOn
FollowRedirectsOnOffOnOff

Scenario Selection

ScenarioRecommended PresetAdjustment Suggestion
General web servicesDefault-
Handling user-provided URLsSecure-
Internal microservices, high concurrencyPerformanceIncrease MaxIdleConns
One-off scriptsMinimal-
File download servicePerformanceIncrease MaxResponseBodySize
Financial/medical APIsSecure + customAdd audit middleware
go
// High throughput scenario
client, _ := httpc.New(httpc.PerformanceConfig())

// Fine-tune on top of preset
cfg := httpc.PerformanceConfig()
cfg.Timeouts.Request = 120 * time.Second
cfg.Connection.MaxIdleConns = 200
client, _ := httpc.New(cfg)

Object Pool Reuse

Internally, HTTPC reuses engine response objects and string builders via sync.Pool to reduce GC pressure; Result itself is created fresh per request and reclaimed by GC:

go
result, err := client.Get(url)
if err != nil {
    return err
}
// Result is created fresh per request, reclaimed by GC, no manual release needed

TIP

In high-concurrency scenarios, object pool reuse significantly reduces GC pressure.

Performance Anti-Patterns

Anti-PatternCauseCorrect Approach
Creating client per requestConnections cannot be reusedReuse client globally
Excessively large MaxResponseBodySizeMemory consumptionSet reasonable limits
Using result.String() in hot pathsString building overheadUse Body() directly

Next Steps