由于持久连接的原因,HAProxy无法进行负载均衡。

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

HAProxy is not load balancing due to persistent connections

问题

我们有一个使用Go语言编写的Web服务器和客户端,它们相互交互。我们希望使用HAProxy在多个服务器实例之间进行请求的负载均衡,但是目前无法正常工作。客户端在服务器仍然可用时总是连接到同一个服务器。

如果我查看"netstat -anp"的输出,我可以看到客户端和服务器之间通过HAProxy建立了一个持久连接。我尝试将响应中的Connection头设置为"close",但完全没有起作用。

毫无疑问,我对此感到完全困惑。我的第一个问题是,这是客户端、服务器还是HAProxy的问题?如何强制客户端断开连接?我是否在某个方面遗漏了什么?Curl工作正常,所以我知道HAProxy确实进行了负载均衡,但是Curl在完成后也会完全关闭,因此我怀疑是持久连接导致了问题,因为客户端和服务器都是长时间运行的。

顺便说一下,我在服务器上使用的是go-martini。

谢谢。

英文:

We have a web server and a client, both written in go, that interact with each other. We want HAProxy to load balance requests between several instance of the server, but it's not working. The client will always connect to the same server while it's still up.

If I look at the output of "netstat -anp", I can see that there is a persistent connection that was established between the client and the sever through HAProxy. I tried setting the Connection Header in the response to "close", but that didn't work at all.

Needless to say, I'm completely confused by this. My first question is, is this a problem with the client, server, or HAProxy? How does one force the client to disconnect? Am I missing something regarding this? Curl works fine, so I know that HAProxy does load balance, but curl also completely shuts down when finished, hence why I'm suspecting it's the persistent connection that's causing me issues since the client and server are long running.

Just as an FYI, I'm using go-martini on the server.

Thanks.

答案1

得分: 5

HTTP/1.1默认使用KeepAlive。由于连接没有关闭,HAProxy无法在不同的后端之间平衡请求。

你有几个选项:

在你的代码中强制在每个请求后关闭连接。在客户端或服务器上设置Request.Close = true将发送一个Connection: close头,告诉双方关闭TCP连接。

或者,你可以让HAProxy通过设置http-server-close来修改请求,这样在每个请求后关闭后端,或者通过设置http-close来关闭双方。

通常情况下,http-server-close是最好的选择,因为它仍然为客户端维护持久连接,同时逐个代理每个请求。

英文:

HTTP/1.1 uses KeepAlive by default. Since the connections aren't closed, HAProxy can't balance the requests between different backends.

You have a couple options:

Force the connection to close after each request in your code. Setting Request.Close = true on either the client or the server will send a Connection: close header, telling both sides to close the tcp connection.

Alternatively you could have HAPoxy alter the requests by setting http-server-close so the backend is closed after each request, or http-closeto shutdown both sides after each request.

http-server-close is usually the best option, since that still maintains persistent connections for the client, while proxying each request individually.

huangapple
  • 本文由 发表于 2015年3月13日 02:28:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/29017581.html
匿名

发表评论

匿名网友

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

确定