Connection pooling: interaction of MaxIdleConnsPerHost and IdleConnTimeout in http.Transport

huangapple go评论81阅读模式
英文:

Connection pooling: interaction of MaxIdleConnsPerHost and IdleConnTimeout in http.Transport

问题

我正在尝试使用Golang编写一个重型代理,用于访问一组Web API。为了防止端口耗尽,我决定不使用DefaultClient,而是为http.Client创建一个自定义实例。http.Transport中有许多有趣的设置可以调整。

我遇到了MaxIdleConnsPerHostIdleConnTimeout字段,并且有一个问题。

如果我增加MaxIdleConnsPerHost的值,这意味着会有更多的空闲连接,但它们是可重用的空闲连接吗?换句话说,为了创建一个良好的连接池,我应该相应地增加MaxIdleConnsPerHost的值和IdleConnTimeout的超时时间,还是相反?

英文:

I am trying to write a heavy duty proxy to a set of web APIs in Golang. In order to prevent port exhaustion, I have decided to not use the DefaultClient and instead create a customized instance for http.Client. There are many interesting setting in the http.Transport that I can play around with.

I have come across the MaxIdleConnsPerHost and IdleConnTimeout fields and I have this question.

If I increase the value of MaxIdleConnsPerHost it means there will be more idle connection, but are they reusable idle connections? Or in other words, to make a decent connection pool, should I increase the value of MaxIdleConnsPerHost together with the timeout for IdleConnTimeout accordingly, or does it behave exactly the opposite?

答案1

得分: 3

是的,IdleConns是可重用的,因为它们是keep-alive连接。但是为了让Golang尊重keep-alive连接的可重用性,在应用程序中需要确保以下两点:

  1. 读取完整的响应(即ioutil.ReadAll(rep.Body)
  2. 调用Body.Close()

这里有一个链接提供更详细的解释。

英文:

yes, IdleConns are reusable as these are keep-alive connections. But to make golang honour the reusability of keep-alive connections you need to make sure of 2 things in your applications.

  1. Read until Response is complete (i.e. ioutil.ReadAll(rep.Body))
  2. Call Body.Close()

here's a link for more explaination.

huangapple
  • 本文由 发表于 2022年11月6日 15:38:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/74334066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定