Golang / GoLang Production Patterns and Web Standards Interview Questions
What are the best practices for using Go's HTTP client in production?
The default http.DefaultClient is not suitable for production — it has no timeouts, uses a shared transport, and cannot be instrumented. Production code creates dedicated clients with explicit configuration.
// Production HTTP client — never use http.DefaultClient in libraries
func newHTTPClient() *http.Client {
return &http.Client{
Timeout: 30 * time.Second, // total request timeout including body read
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 10,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
// Keep-alive enabled by default
DisableKeepAlives: false,
},
}
}
// Reuse clients — they manage connection pools internally
var apiClient = newHTTPClient()
// Always use context for cancellation and timeout
func callAPI(ctx context.Context, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "myservice/1.0")
resp, err := apiClient.Do(req)
if err != nil {
return nil, fmt.Errorf("executing request: %w", err)
}
defer resp.Body.Close()
// Always read and close body — even on error status
body, err := io.ReadAll(io.LimitReader(resp.Body, 10<<20)) // 10 MB limit
if err != nil {
return nil, fmt.Errorf("reading body: %w", err)
}
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, body)
}
return body, nil
}
// RETRY with exponential backoff for idempotent requests
func callWithRetry(ctx context.Context, url string, maxRetries int) ([]byte, error) {
var lastErr error
for attempt := 0; attempt < maxRetries; attempt++ {
if attempt > 0 {
delay := time.Duration(attempt*attempt) * 100 * time.Millisecond
select {
case <-time.After(delay):
case <-ctx.Done(): return nil, ctx.Err()
}
}
data, err := callAPI(ctx, url)
if err == nil { return data, nil }
lastErr = err
}
return nil, fmt.Errorf("after %d retries: %w", maxRetries, lastErr)
}
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
