亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 Go HTTPS Client 不重用連接?

為什么 Go HTTPS Client 不重用連接?

Go
慕村9548890 2021-06-08 13:15:04
我有一個 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 循環中調用它時。但這會不斷創建無限數量的連接。使用請求庫的類似 python 代碼只創建一個連接。#!/usr/bin/env pythonimport requestsendpoint_url_fmt      = "https://something.com/restserver.php"params = {}params['method'] = 'write'params['category'] = category_errors_scubaparams['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
查看完整描述

2 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

有一些有趣的改進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


查看完整回答
反對 回復 2021-06-21
  • 2 回答
  • 0 關注
  • 266 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號