【问题标题】:Turning off connection pool for Go http.Client关闭 Go http.Client 的连接池
【发布时间】:2019-08-27 22:55:27
【问题描述】:

出于测试目的,我尝试在 Go 中创建一个禁用连接池的 net/http.Client。我想要实现的是在每个 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 问题(可能是一场比赛),它使用了一个应该从池中取出的连接。

【问题讨论】:

  • 您只需要一个客户吗?最直接的方法是简单地使用一个函数,该函数返回一个您可以使用的新客户端对象。
  • @Jesse,如果你只这样做,那么你将泄漏打开的 tcp 连接
  • 为什么不直接关闭连接,而不用担心池?您是否尝试过使用Request.Close(或设置Connection: close 标头)?
  • @JimB 如果是出于测试目的,那可能不是问题。
  • 这些都可以。介意添加作为答案吗?

标签: go


【解决方案1】:

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

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

以下是设置 DisableKeepAlives 的方法:

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

Run a demonstration of DisableKeepAlives = true on the playground.

MaxIdleConnsPerHost 的设置方法如下:

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

Run a demonstration of MaxIdleConnsPerHost = -1 on the playground.

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

    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 不限制每个主机的活动连接数。 See this playground example for a demonstration.

将Dialer.KeepAlive 设置为-1 不会禁用池化。有关说明,请参阅 this answer。

【讨论】:

    【解决方案2】:

    您需要将DisableKeepAlives 设置为true,并将MaxIdleConnsPerHost 设置为-1。

    来自文档:

    // DisableKeepAlives, if true, disables HTTP keep-alives and
    // will only use the connection to the server for a single
    // HTTP request.
    

    https://golang.org/src/net/http/transport.go,第 166 和 187 行

    因此,您的客户端必须按以下方式初始化

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

    如果你使用的是 1.7 之前的 Go 版本,那么你需要消耗 body 的所有缓冲区,并且只有在调用 request.Body.Close() 之后。相反,如果您使用的是大于或等于 1.7 的版本,则可以推迟关闭而无需额外的预防措施。

    禁用连接池但仍能够执行并行请求的示例库: https://github.com/alessiosavi/Requests

    【讨论】:

      【解决方案3】:

      http.Transport 有一个名为MaxConnsPerHost 的属性

      MaxConnsPerHost 可以选择限制每台主机的连接总数,包括处于拨号、活动和空闲状态的连接。

      包括拨号、活动和空闲状态

      【讨论】:

      • 这无济于事,因为一旦我发出导致与example.com 的连接的请求,我就需要断开该连接,以便下一个请求使用从头开始的拨号。但是,设置 MaxConnsPerHost:1 仍将重用该 TCP 连接。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多