Using HTTP Proxies in Go (net/http)
Go net/http proxy setup for production collectors—Transport, ProxyURL, timeouts, and Proxy Grove Unlimited endpoints from $2/IP/day.
TL;DR: In Go, set a proxy by giving http.Transport a Proxy function, usually http.ProxyURL built from your ProxyGrove endpoint, and reuse that transport across requests. Credentials placed in the proxy URL are sent as proxy authentication automatically, including for HTTPS tunnels. Environment variables such as HTTPS_PROXY also work through http.ProxyFromEnvironment, but an explicit transport is clearer in production. Always set timeouts, bound idle connections, close and drain response bodies, and give each worker one sticky endpoint. Get an endpoint from Residential Proxy or Mobile Proxy and see plans on pricing.
This guide shows a production-minded collector in plain net/http: the transport, authentication, SOCKS5, worker pools and a verification routine. Replace the placeholder host, port and credentials with the values from your dashboard. See also the HTTP/HTTPS proxy product page.
A minimal proxied client
Build the transport once, wrap it in a client with an overall timeout, and reuse both for every request.
proxyURL, err := url.Parse("http://PROXY_USER:PROXY_PASS@PROXY_HOST:8080")
if err != nil {
log.Fatal(err)
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
MaxIdleConns: 100,
MaxIdleConnsPerHost: 10,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 20 * time.Second,
}
client := &http.Client{
Transport: transport,
Timeout: 30 * time.Second,
}
The client Timeout bounds the whole request including reading the body, while the transport timeouts bound individual phases. Setting both lets you tell a slow proxy hop from a slow target.
Environment variables versus an explicit transport
http.ProxyFromEnvironment reads HTTP_PROXY, HTTPS_PROXY and NO_PROXY, which is convenient for command-line tools and containers. In a service that talks to several destinations, hidden environment state is a liability: a deploy that changes one variable silently changes every outbound call. Prefer an explicit Transport per proxy profile and keep internal calls on a separate, direct client. Note that the environment-based function skips the proxy for loopback addresses, which surprises people who test against a local server.
Authentication
Put the username and password in the proxy URL, as above, and the transport adds the proxy authorization header for you. If your password contains special characters, build the URL with url.UserPassword so it is escaped properly:
proxyURL := &url.URL{
Scheme: "http",
User: url.UserPassword(os.Getenv("PROXY_USER"), os.Getenv("PROXY_PASS")),
Host: "PROXY_HOST:8080",
}
Read credentials from environment variables or a secret manager, never from source control. If you would rather not ship credentials, authorize your server's egress IP and connect without them; credentials vs allowlisting compares both models, and the auth setup guide walks through the dashboard steps.
SOCKS5 in Go
The standard transport accepts socks5:// proxy URLs, so the simplest SOCKS5 setup is a different scheme in the same http.ProxyURL call. If you need a custom dialer, the golang.org/x/net/proxy package offers proxy.SOCKS5 with an Auth struct, and you can plug the resulting dialer into Transport.DialContext. ProxyGrove plans include SOCKS5 alongside HTTP, so choose the protocol your tooling documents. For protocol tradeoffs, see SOCKS5 vs HTTP proxies.
Workers, sticky sessions and concurrency
Pin one proxy endpoint per worker, and one transport per endpoint. With a sticky endpoint, that worker then keeps a consistent exit for the session window, which suits multi-step flows such as a search page followed by detail pages. With a rotating endpoint, each new connection may use a different exit, so do not also shuffle a proxy list in code. Choose the mode on the plan and keep the code simple. The decision framework is in sticky vs rotating proxies.
- Bound concurrency. Use a buffered channel or a worker pool sized to what the target tolerates, and begin with a small number.
- Use contexts. Create every request with
http.NewRequestWithContextand a deadline so cancellations propagate. - Close and drain bodies. Call
resp.Body.Close()and read the remainder withio.Copy(io.Discard, resp.Body)so the connection returns to the pool. - Retry with backoff. Retry timeouts and
5xxresponses with exponential backoff, jitter and a cap. Treat429as a signal to slow down, and never retry407in a loop. - Log the profile. Include a profile label, status code, latency and exit IP in each log line.
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, target, nil)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, 10<<20))
Budgeting timeouts and watching the right signals
A proxied request has more hops than a direct one: your worker connects to the proxy, the proxy connects to the target, and the TLS handshake happens through the tunnel. Give each phase its own budget. A sensible pattern is a short dial and TLS timeout, a longer response-header timeout, and an overall client timeout slightly above the sum. When a request fails, the error text then tells you which phase ran out of time, which is usually enough to decide whether to tune your own settings, lower concurrency or look at the target.
Track four numbers per proxy profile and review them daily: success rate, median and slow-tail latency, error mix (timeouts, authentication failures, target errors) and bytes transferred. A drop in success rate that arrives together with a rise in timeouts points to load or pacing. A jump in 407 responses points to credentials or allowlist changes. Writing these counters into your metrics library from day one costs little and saves hours when a collector misbehaves.
Choosing the proxy type and plan
Residential Proxy suits consumer-facing pages, Mobile Proxy suits checks where the carrier network matters, and Corporate Proxy is worth a small pilot for B2B targets. ProxyGrove currently lists Residential and Corporate from $2 per IP per day and Mobile from $4.50 per IP per day, with 1, 7, 30 and 90 day terms, HTTP and SOCKS5, sticky or rotating sessions and fair-use unlimited traffic on the plan. Confirm the live numbers on pricing. For tuning, read proxy success-rate tips.
Verify before you scale
- Request an IP-echo endpoint through the client and confirm the address differs from your server's own IP.
- With a sticky endpoint, repeat the request and confirm the address is stable inside the session window.
- Call an internal URL with the direct client and confirm it does not use the proxy.
- Use a wrong password on purpose and confirm your code reports the authentication failure clearly.
- Run a small batch and record success rate and latency before adding workers.
Common mistakes
- Creating a transport per request. This leaks sockets and defeats connection reuse. Share one.
- Skipping body close and drain. Connections stay busy and the pool starves.
- No timeouts. The default client waits indefinitely, which hides faults and stalls workers.
- Hidden environment proxies. Use explicit transports for services.
- Rotating inside one flow. A page and its follow-up requests should share an identity.
Next steps
Open pricing or register at app.proxygrove.com, and choose residential, mobile or corporate. Related guides: Java, Python httpx and Node fetch. Collect public data only, and respect the terms of the sites you call.
Questions this article answers
Yes. HTTP_PROXY, HTTPS_PROXY and NO_PROXY work through http.ProxyFromEnvironment, but an explicit Transport is clearer and safer in production services.
Yes. The standard transport accepts socks5 proxy URLs, and golang.org/x/net/proxy provides a SOCKS5 dialer when you need custom behavior. ProxyGrove plans include SOCKS5.
Pin one proxy endpoint per worker and reuse one transport per endpoint. Choose sticky or rotating on the plan instead of shuffling endpoints in code.
Current plans and per-IP rates are listed on the pricing page. Confirm them there before you budget.