为什么Go HTTPS客户端不重用连接?

为什么Go HTTPS客户端不重用连接?,http,go,Http,Go,我有一个http客户端,它可以创建到主机的多个连接。我想设置它可以设置到特定主机的最大连接数。 go的request.Transport中没有此类选项。 我的代码看起来像 package main import ( "fmt" "net/http" "net/url" ) const ( endpoint_url_fmt = "https://blah.com/api1?%s" ) func main() { transport := http.Tran

我有一个http客户端,它可以创建到主机的多个连接。我想设置它可以设置到特定主机的最大连接数。 go的request.Transport中没有此类选项。 我的代码看起来像

package main 

import (
  "fmt"
  "net/http"
  "net/url"
)

const (
  endpoint_url_fmt      = "https://blah.com/api1?%s"
)


func main() {

  transport := http.Transport{ DisableKeepAlives : false }

  outParams := url.Values{}
  outParams.Set("method", "write")
  outParams.Set("message", "BLAH")

  for {
    // Encode as part of URI.
    outboundRequest, err := http.NewRequest(
      "GET",
      fmt.Sprintf(endpoint_url_fmt, outParams.Encode()),
      nil
    )
    outboundRequest.Close = false
    _ , err = transport.RoundTrip(outboundRequest)
    if err != nil {
      fmt.Println(err)
    }
  }

}
我希望这会创建一个连接。就像我在for循环中调用它一样。但这会不断创建无限多的连接

其中,使用请求库的类似python代码只创建一个连接

#!/usr/bin/env python
import requests
endpoint_url_fmt      = "https://something.com/restserver.php"
params = {}
params['method'] = 'write'
params['category'] = category_errors_scuba
params['message'] = "blah"
while True:
  r = requests.get(endpoint_url_fmt, params = params)
出于某种原因,go代码没有重用http连接

编辑: go代码需要关闭主体以重用连接

 resp , err = transport.RoundTrip(outboundRequest)
 resp.Close() //  This allows the connection to be reused

根据OP的进一步说明。默认客户端不会重用连接

一定要关上门

调用方在完成读取后应关闭相应的正文。如果响应主体未关闭,则客户端的底层往返程序(通常为传输)可能无法为后续“保持活动”请求重新使用到服务器的持久TCP连接

此外,我发现在调用Close()之前,我还需要阅读,直到响应完成

e、 g

要确保http.Client连接重用,请确保执行两件事:

  • 读取直到响应完成(即,
    ioutil.ReadAll(resp.Body)
  • 调用
    Body.Close()

旧答案,对利率限制有用,但不是OP的目标:

我认为通过golang 1.1 http API设置最大连接数是不可能的。这意味着,如果你不小心的话,你可以用大量的TCP连接(直到你用完文件描述符或其他任何东西)来攻击自己

也就是说,您可以通过time.Tick限制调用特定主机的go例程的速率(因此也可以限制出站请求和连接)

例如:

import "time"

requests_per_second := 5
throttle := time.Tick(1000000000 / requests_per_second)

for i := 0; i < 16; i += 1 {
  <-throttle  
  go serveQueue()
}
导入“时间”
每秒请求数:=5
节流阀:=时间。滴答(100000000/请求/秒)
对于i:=0;i<16;i+=1{

在以下方面有一些有趣的改进:


我不认为
maxidleconsperhost
正在做你认为它正在做的事情。它的目的是支持HTTP 1.1保持生存,而不是限制你连接的主机数量。你似乎也在以一种稍微原始的方式做
HTTP GET
。你能改为适应吗?编辑的问题简化示例,只有一个for循环。No go routines.添加了等效的python代码,这是正确的。相关讨论:我添加了一个等效的python代码。这段代码做了预期的事情,只打开一个连接。问题的编写方式(即“设置最大数量”)这让我相信您试图限制出站连接,而不是重复使用它们。我现在可以推断您希望尽可能地重用连接。您只需要设置python请求库处理的保持活动标头,而go不保持活动。我将在回答中添加一个片段。go客户端使用http 1.1默认设置。设置标题不是问题。Go需要关闭响应主体才能重新使用连接。是的。我没有发现它。很高兴它现在可以工作。为将来的用户更新了我的答案…希望它能帮助别人。
import "time"

requests_per_second := 5
throttle := time.Tick(1000000000 / requests_per_second)

for i := 0; i < 16; i += 1 {
  <-throttle  
  go serveQueue()
}
// DisableKeepAlives, if true, disables HTTP keep-alives and
// will only use the connection to the server for a single
// HTTP request.
//
// This is unrelated to the similarly named TCP keep-alives.
DisableKeepAlives bool

// ...

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

// MaxIdleConnsPerHost, if non-zero, controls the maximum idle
// (keep-alive) connections to keep per-host. If zero,
// DefaultMaxIdleConnsPerHost is used.
MaxIdleConnsPerHost int

// MaxConnsPerHost optionally limits the total number of
// connections per host, including connections in the dialing,
// active, and idle states. On limit violation, dials will block.
//
// Zero means no limit.
MaxConnsPerHost int // Go 1.11