Reusing http connections in Golang

前端 未结 9 831
时光取名叫无心
时光取名叫无心 2020-12-02 04:27

I\'m currently struggling to find a way to reuse connections when making HTTP posts in Golang.

I\'ve created a transport and client like so:

// Crea         


        
9条回答
  •  爱一瞬间的悲伤
    2020-12-02 05:14

    The missing point here is the "goroutine" thing. Transport has its own connection pool, by default each connection in that pool is reused (if body is fully read and closed) but if several goroutines are sending requests, new connections will be created (the pool has all connections busy and will create new ones). To solve that you will need to limit the maximum number of connections per host: Transport.MaxConnsPerHost (https://golang.org/src/net/http/transport.go#L205).

    Probably you also want to setup IdleConnTimeout and/or ResponseHeaderTimeout.

提交回复
热议问题