10

ホストへの複数の接続を作成する 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)
    }
  }

}

これにより、1つの接続が作成されると予想されます。forループで呼び出しているので。しかし、これは無限の数の接続を作成し続けます。

requests ライブラリを使用する同様の python コードは、1 つの接続のみを作成します。

#!/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 接続を再利用していません。

EDIT : go コードでは、接続を再利用するために本体を閉じる必要があります。

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

2 に答える 2

18

OPからのさらなる明確化に基づいています。デフォルトのクライアント接続を再利用します。

必ず応答を閉じてください。

読み取りが完了したら、呼び出し元は resp.Body を閉じる必要があります。resp.Body が閉じられていない場合、クライアントの基盤となる RoundTripper (通常はトランスポート) は、後続の「キープアライブ」要求のためにサーバーへの永続的な TCP 接続を再利用できない場合があります。

さらに、 Close() を呼び出す前に、応答が完了するまで読み取る必要があることもわかりました。

例えば

res, _ := client.Do(req)
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()

http.Client 接続を確実に再利用するには、次の 2 つのことを行ってください。

  • 応答が完了するまで読み取ります (つまり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()
}
于 2013-07-31T04:26:10.287 に答える
1

には興味深い改善点がありますhttp.Transport

// 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
于 2020-01-22T08:22:11.330 に答える