简体   繁体   English

go 似乎不支持持久连接设置

[英]go doesn't seem to honor persistent connections setting

I have a reverse proxy built using go.我有一个使用 go 构建的反向代理。 I have set it to use persistent connections.我已将其设置为使用持久连接。 But when I monitor the open connections (using lsof -i) on my machine, looks like its closing connections within a few minutes despite me setting it for 60 minutes.但是,当我在我的机器上监视打开的连接(使用 lsof -i)时,尽管我将它设置为 60 分钟,但它看起来就像在几分钟内关闭了连接。

If I set other timeouts in the settings below, like ResponseHeaderTimeout, those are being honored.如果我在下面的设置中设置了其他超时,比如 ResponseHeaderTimeout,那么这些超时就会被兑现。

    myProxy.Transport = &transport{&http.Transport{
        Proxy: http.ProxyFromEnvironment,
        DialContext: (&net.Dialer{
            Timeout: 200 * time.Millisecond,
        }).DialContext,
        MaxIdleConns:          0,                       //no limit
        MaxIdleConnsPerHost:   10,
        IdleConnTimeout:       60 * time.Minute, 
    }}

Idle connections in http.Transport relies upon HTTP keep-alive as mentioned in http.Transport documentation . http.Transport 中的空闲连接依赖于 HTTP 保持活动,如http.Transport 文档中所述。 These settings are used by the transport while exchanging http payloads with peers (by adding appropriate keep-alive headers).这些设置由传输使用,同时与对等方交换 http 有效负载(通过添加适当的保持活动标头)。

Having said that, HTTP keep-alive has to be agreed upon by both the parties in exchange (client and the server).话虽如此,HTTP keep-alive 必须由交换双方(客户端和服务器)同意。 Most of the servers restrict maximum number of open connections AND the duration (timeout) of open connections with keep-alive requests.大多数服务器限制打开连接的最大数量和保持活动请求的打开连接的持续时间(超时)。 This is done to avoid potential DOS attack and give all it's consumers fair chance with limited system resources at server (each keep-alive connection adds overhead on server).这样做是为了避免潜在的 DOS 攻击,并在服务器上的系统资源有限的情况下为所有消费者提供公平的机会(每个保持活动连接都会增加服务器的开销)。

A 60 minute idle timeout is way too high and I suspect any of the server will honor that (unless you control the server configuration on other end as well). 60 分钟的空闲超时太高了,我怀疑任何服务器都会遵守这一点(除非您也控制另一端的服务器配置)。 Typically this is kept in few seconds to give enough time to a single HTTP page and all it's resources (css, js, images) to load from server using a single connection.通常,这会保持在几秒钟内,以便为单个 HTTP 页面及其所有资源(css、js、图像)提供足够的时间来使用单个连接从服务器加载。 Once a page loads successfully, there's no reason for the connection to be kept open in general browsing world.一旦页面加载成功,就没有理由在一般浏览世界中保持连接打开。 Apache HTTPD, by default, keeps KeepAliveTimeout as 5 seconds. Apache HTTPD 默认保持KeepAliveTimeout为 5 秒。

It's also worth noting, as clearly stated in Mozilla Keep-Alive documentation , timeouts longer than the TCP timeout may be ignored if no keep-alive TCP message is set at the transport level.同样值得注意的是,正如Mozilla Keep-Alive 文档中明确指出的那样,如果在传输级别没有设置保持活动 TCP 消息,则可以忽略超过 TCP 超时的超时。

Given all these facts, this is absolutely normal for you to see what you see (connections being automatically closed).鉴于所有这些事实,您看到所看到的内容是绝对正常的(连接被自动关闭)。 The transport will automatically initiate new connections as and when needed.传输将在需要时自动启动新连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM