Reuse a single http.Transport instance with configured MaxIdleConns and IdleConnTimeout to implement efficient database-like connection pooling in Go.
Reuse a single http.Transport instance across your application and configure its pooling fields to manage connection limits and timeouts.
var client = &http.Client{
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 2,
IdleConnTimeout: 90 * time.Second,
DisableKeepAlives: false,
},
}
This setup ensures connections are cached and reused efficiently, preventing resource exhaustion under load.
Connection pooling keeps a stash of open network lines ready to use instead of building a new one for every request. It works like a library of pre-checked-out books; you grab one, use it, and put it back for the next person, saving time and effort. You use this pattern to make your Go application faster and more stable when handling many requests.