Search code examples
httpgoload-balancingsocksgo-http

Go HTTP Proxy - Prevent Re-using Proxy Connections


I have a set of SOCKS proxies, which are sitting behind a load balancer (an AWS Network Load Balancer, to be specific). When the load balancer receives a request on the SOCKS port, it forwards the request to the Docker container (proxy) with the fewest active connections.

I have a Go application that uses the standard HTTP package. I'd like to use the built-in support that this package has for SOCKS proxies, and point it to my load balancer's address.

My question is, does the Go HTTP client:

  1. Open and maintain a TCP connection to the SOCKS proxy I point it to (a single TCP connection for all HTTP connections that it keeps open),
  2. Open and maintain a TCP connection to the SOCKS proxy for each HTTP connection (i.e. a one-to-one relationship of SOCKS proxy connections to open/idle HTTP connections),
  3. Open and subsequently close a TCP connection to the SOCKS proxy for each HTTP/S request, regardless of the concurrent/idle connection settings on the HTTP client, or
  4. Do something else entirely?

If the answer is (1) or (2), is there a way to prevent this behavior and ensure it always re-connects to the SOCKS proxy for each HTTP/S request?

The reason I'd like to do this is to ensure that the requests are balanced across the many proxies, instead of the clients (each of which may have many concurrent requests) being balanced across the proxies.

Specifically, say I have 3 proxies behind the load balancer, labeled A, B, and C. And say I have two applications, 1 and 2. Application 1 makes 5 HTTP requests per second, and Application 2 makes 500 HTTP requests per second.

What I want to avoid is having Application 1 make a SOCKS connection to the load balancer, and having it forwarded to proxy A, and that connection to the proxy being maintained, while Application 2 makes a SOCKS connection to the load balancer and has it forwarded to proxy B, which is also maintained. If that occurs, proxy A will be handling 5 requests/second while proxy B will be handling 500 requests/second, which is obviously massively imbalanced. I'd rather all three proxies each get ~168 requests/second.


Solution

  • SOCKS is protocol agnostic. It has no idea of what a HTTP request is but only what a TCP connection is which gets tunneled by the proxy. Thus every TCP connection caused by the HTTP stack will mean a new connection through the proxy - no reuse. If you want to make sure that every HTTP/S requests gets its own connection through the proxy you must disable HTTP keep-alive. See this question on how to do this.