doubairan4213 2018-11-07 01:58
浏览 57

通过每个TCP连接的每个HTTP请求

My question is simple: How many http requests make how many tcp connections to server in Go.

I'm coding a tool can send many http requests, but I find all this requests over one or two tcp connetions, seem like golang http Transport have a connections pool.

  • 写回答

1条回答 默认 最新

  • dongwen7187 2018-11-07 03:01
    关注

    If you are using the DefaultTransport for your HTTP requests then the TCP connections are reused.

    DefaultTransport is the default implementation of Transport and is used by DefaultClient. It establishes network connections as needed and caches them for reuse by subsequent calls.

    You can use MaxConnsPerHost to limit the total number of connections:

    // 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.
    //
    // For HTTP/2, this currently only controls the number of new
    // connections being created at a time, instead of the total
    // number. In practice, hosts using HTTP/2 only have about one
    // idle connection, though.
    MaxConnsPerHost int
    

    Edit

    I highly suggest you read the docs as Go has one of the most well documented standard library. Anyway, you can configure http.Transport to disable keep-alive to force usage of one TCP connection per request.

    // 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
    
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来