首页 > 解决方案 > 关闭 Go http.Client 的连接池

问题描述

出于测试目的,我正在尝试net/http.Client在 Go 中创建一个禁用连接池的功能。我想要实现的是在每个 HTTP/1.x 请求的地址上建立一个新的 TCP 连接。

目前我有:

        c = &http.Client{
            Transport: &http.Transport{
                DialContext: (&net.Dialer{
                    Timeout:   5 * time.Second,
                    KeepAlive: 5 * time.Second,
                }).DialContext,
                TLSHandshakeTimeout:   5 * time.Second,
                ResponseHeaderTimeout: 5 * time.Second,
                ExpectContinueTimeout: 1 * time.Second,
            },
        }

任何想法我应该如何调整这个?

我看到如果我设置c.Transport.MaxIdleConns = 1它可以工作,但我不确定这是否仍然允许 1 个使用中 + 1 个空闲(总共 2 个)TCP 连接:

    // MaxIdleConns controls the maximum number of idle (keep-alive)
    // connections across all hosts. Zero means no limit.
    MaxIdleConns int

同样,似乎c.Dialer.KeepAlive = -1也可以这样做:

    // KeepAlive specifies the keep-alive period for an active
    // network connection.
    // If zero, keep-alives are enabled if supported by the protocol
    // and operating system. Network protocols or operating systems
    // that do not support keep-alives ignore this field.
    // If negative, keep-alives are disabled.

但我不确定 TCP 连接 + Keep-Alive + HTTP 的行为。

另一种方法是尝试尽快杀死空闲的 TCP 连接,所以我设置了c.Transport.IdleConnTimeout = 1*time.Nanosecond.

当我这样做时,我Client.Do()现在偶尔会返回错误:

tls: use of closed connection

我怀疑这是一个 Go stdlib 问题(可能是一场比赛),它使用了一个应该从池中取出的连接。

标签: go

解决方案


在函数Transport.tryPutIdleConn中将连接添加到池中。如果Transport.DisableKeepAlives为 true 或 Transport.MaxIdleConnsPerHost小于零,则连接不会被池化。

设置任一值都会禁用池化。Connection: close当 DisableKeepAlives 为 true 时,传输会添加请求标头。根据您正在测试的内容,这可能是可取的,也可能不是可取的。

以下是设置 DisableKeepAlives 的方法:

t := http.DefaultTransport.(*http.Transport).Clone()
t.DisableKeepAlives = true
c := &http.Client{Transport: t}

在操场上运行 DisableKeepAlives = true 的演示

以下是设置 MaxIdleConnsPerHost 的方法:

t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConnsPerHost = -1
c := &http.Client{Transport: t}

在操场上运行 MaxIdleConnsPerHost = -1 的演示

上面的代码克隆了默认传输以确保使用默认传输选项。如果您明确想要问题中的选项,请使用

    c = &http.Client{
        Transport: &http.Transport{
            DialContext: (&net.Dialer{
                Timeout:   5 * time.Second,
                KeepAlive: 5 * time.Second,
            }).DialContext,
            TLSHandshakeTimeout:   5 * time.Second,
            ResponseHeaderTimeout: 5 * time.Second,
            ExpectContinueTimeout: 1 * time.Second,
            DisableKeepAlives: true,
        },
    }

或者

    c = &http.Client{
        Transport: &http.Transport{
            DialContext: (&net.Dialer{
                Timeout:   5 * time.Second,
                KeepAlive: 5 * time.Second,
            }).DialContext,
            TLSHandshakeTimeout:   5 * time.Second,
            ResponseHeaderTimeout: 5 * time.Second,
            ExpectContinueTimeout: 1 * time.Second,
            MaxIdleConnsPerHost: -1,
        },
    }

MaxIdleConnsPerHost 不限制每个主机的活动连接数。 有关演示,请参阅此操场示例

通过将Dialer.KeepAlive设置为 -1不会禁用池 化。请参阅此答案以获取解释。


推荐阅读