Go HTTP代理 – 防止重用代理连接

huangapple go评论76阅读模式
英文:

Go HTTP Proxy - Prevent Re-using Proxy Connections

问题

我有一组 SOCKS 代理,它们位于负载均衡器后面(具体来说是 AWS Network Load Balancer)。当负载均衡器在 SOCKS 端口接收到请求时,它会将请求转发给具有最少活动连接的 Docker 容器(代理)。

我有一个使用标准 HTTP 包的 Go 应用程序。我想使用该包内置的 SOCKS 代理支持,并将其指向我的负载均衡器地址。

我的问题是,Go HTTP 客户端是否:

  1. 对我指定的 SOCKS 代理打开并维护一个 TCP 连接(对于它保持打开的所有 HTTP 连接使用单个 TCP 连接),
  2. 对于每个 HTTP 连接打开并维护一个 TCP 连接(即 SOCKS 代理连接与打开/空闲的 HTTP 连接之间是一对一的关系),
  3. 对于每个 HTTP/S 请求,无论 HTTP 客户端上的并发/空闲连接设置如何,都会打开并随后关闭一个 TCP 连接到 SOCKS 代理,还是
  4. 还有其他的操作方式?

如果答案是(1)或(2),是否有办法阻止这种行为,并确保它始终重新连接到 SOCKS 代理以进行每个 HTTP/S 请求?

我之所以这样做是为了确保请求在许多代理之间平衡,而不是客户端(每个客户端可能有许多并发请求)在代理之间平衡。

具体来说,假设我有 3 个位于负载均衡器后面的代理,标记为 A、B 和 C。假设我有两个应用程序,1 和 2。应用程序 1 每秒发出 5 个 HTTP 请求,应用程序 2 每秒发出 500 个 HTTP 请求。

我想要避免的情况是,应用程序 1 建立一个 SOCKS 连接到负载均衡器,并将其转发到代理 A,并且该连接被维护,同时应用程序 2 建立一个 SOCKS 连接到负载均衡器,并将其转发到代理 B,也被维护。如果发生这种情况,代理 A 将处理每秒 5 个请求,而代理 B 将处理每秒 500 个请求,这显然是严重不平衡的。我希望三个代理每个都能获得大约 168 个请求/秒。

英文:

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.

答案1

得分: 1

SOCKS是协议无关的。它不知道HTTP请求是什么,只知道通过代理进行隧道传输的TCP连接是什么。因此,由HTTP堆栈引起的每个TCP连接都意味着通过代理建立一个新的连接,不能重用。如果你想确保每个HTTP/S请求都通过代理获得自己的连接,你必须禁用HTTP keep-alive。请参考这个问题了解如何做到这一点。

英文:

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.

huangapple
  • 本文由 发表于 2022年7月1日 04:59:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/72821962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定