104

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

I've created a transport and client like so:

// Create a new transport and HTTP client
tr := &http.Transport{}
client := &http.Client{Transport: tr}

I'm then passing this client pointer into a goroutine which is making multiple posts to the same endpoint like so:

r, err := client.Post(url, "application/json", post)

Looking at netstat this appears to be resulting in a new connection for every post resulting in a large number of concurrent connections being open.

What is the correct way to reuse connections in this case?

1

10 Answers 10

132

Ensure that you read until the response is complete AND call Close().

e.g.

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

Again... To ensure http.Client connection reuse be sure to:

  • Read until Response is complete (i.e. ioutil.ReadAll(resp.Body))
  • Call Body.Close()
12
  • 1
    I am posting to the same host. However, my understanding is that MaxIdleConnsPerHost would result in idle connections being closed. Is that not the case?
    – sicr
    Jul 30, 2013 at 18:19
  • 6
    +1, because I called defer res.Body.Close() in a similar program, but ended up returning from the function occasionally before that part was executed (if resp.StatusCode != 200, for example), which left lots of open file descriptors idle and eventually killed my program. Hitting this thread made me revisit that part of the code and facepalm myself. thanks.
    – sa125
    Apr 10, 2014 at 12:48
  • 3
    one interesting note is that the read step appears to be necessary and sufficient. The read-step alone will return the connection to the pool, but the close alone will not; the connection would end up in TCP_WAIT. Also ran into trouble because I was using a json.NewDecoder() to read the response.Body, which did not fully read it. Make sure to include the io.Copy(ioutil.Discard, res.Body) if you're not sure. Feb 16, 2016 at 19:00
  • 3
    Is there a way to check if the body has been completely read? Is a ioutil.ReadAll() guaranteed to be enough or do i still need to sprinkle io.Copy() calls all over the place, just in case? Apr 10, 2018 at 6:27
  • 6
    I looked at the source code and it seems that response body Close() already takes care of draining the body: github.com/golang/go/blob/…
    – dr.scre
    Jan 24, 2019 at 11:56
51

If anyone is still finding answers on how to do it, this is how I am doing it.

package main

import (
  "bytes"
  "io/ioutil"
  "log"
  "net/http"
  "time"
)

func httpClient() *http.Client {
    client := &http.Client{
        Transport: &http.Transport{
            MaxIdleConnsPerHost: 20,
        },
        Timeout: 10 * time.Second,
    }

    return client
}

func sendRequest(client *http.Client, method string) []byte {
    endpoint := "https://httpbin.org/post"
    req, err := http.NewRequest(method, endpoint, bytes.NewBuffer([]byte("Post this data")))
    if err != nil {
        log.Fatalf("Error Occured. %+v", err)
    }

    response, err := client.Do(req)
    if err != nil {
        log.Fatalf("Error sending request to API endpoint. %+v", err)
    }

    // Close the connection to reuse it
    defer response.Body.Close()

    body, err := ioutil.ReadAll(response.Body)
    if err != nil {
        log.Fatalf("Couldn't parse response body. %+v", err)
    }

    return body
}

func main() {
    c := httpClient()
    response := sendRequest(c, http.MethodPost)
    log.Println("Response Body:", string(response))
}

Go Playground: https://play.golang.org/p/cYWdFu0r62e

In summary, I am creating a different method to create an HTTP client and assigning it to a variable, and then using it to make requests. Note the

defer response.Body.Close() 

This will close the connection after the request is complete at the end of the function execution and you can reuse the client as many times.

If you want to send a request in a loop call the function that sends the request in a loop.

If you want to change anything in the client transport configuration, like add proxy config, make a change in the client config.

Hope this will help someone.

6
  • 5
    Is using the http.Client as a global variable safe from race conditions if there are multiple goroutines calling a function using that variable? Jul 20, 2017 at 18:15
  • 3
    @bn00d is defer response.Body.Close() correct? i ask because by defering the close we won't actually close the conn for reuse until the main function exits, thus one should simply call .Close() directly after .ReadAll(). this may not seem like an issue in your example b/c it doesn't actually demonstrate making multiple req, it simply makes one req and then exits but if we were to make several req back to back, it would seem that since defered, .Close() won't be called til func exits. or... am i missing something? thanks.
    – mad.meesh
    Sep 8, 2017 at 16:10
  • 1
    @mad.meesh if you do multiple calls (eg. inside a loop), just wrap the call to Body.Close() inside a closure, this way it will get closed as soon as you're done processing the data. May 10, 2018 at 7:46
  • How can I set different proxies for every request in this way ? Is it possible ? Jun 22, 2020 at 15:16
  • @bn00d Your example seems not be working. After adding a loop, each request still results in a new connection. play.golang.org/p/9Ah_lyfYxgV
    – Lewis Chan
    Jul 23, 2020 at 7:45
41

Edit: This is more of a note for people that construct a Transport and Client for every request.

Edit2: Changed link to godoc.

Transport is the struct that holds connections for re-use; see https://godoc.org/net/http#Transport ("By default, Transport caches connections for future re-use.")

So if you create a new Transport for each request, it will create new connections each time. In this case the solution is to share the one Transport instance between clients.

2
  • Please give links by using the specific commit. Your link isn't correct anymore. May 27, 2018 at 1:30
  • play.golang.org/p/9Ah_lyfYxgV this example shows just one transport, but it still spawn one connection per request. Why is that ?
    – Lewis Chan
    Jul 23, 2020 at 7:54
13

IIRC, the default client does reuse connections. Are you closing the response?

Callers should close resp.Body when done reading from it. If resp.Body is not closed, the Client's underlying RoundTripper (typically Transport) may not be able to re-use a persistent TCP connection to the server for a subsequent "keep-alive" request.

4
  • Hi, thanks for the response. Yes, sorry I should have also included that. I am closing the connection with r.Body.Close().
    – sicr
    Jul 30, 2013 at 13:58
  • @sicr, are you positive the server doesn't actually closes the connections itself? I mean, these outstanding connections might be in one of the *_WAIT states or something like this
    – kostix
    Jul 30, 2013 at 14:17
  • 1
    @kostix I see a large number of connections with the state ESTABLISHED when looking at netstat. It appears that a new connection is being spawned on every POST request as opposed to the same connection being reused.
    – sicr
    Jul 30, 2013 at 15:17
  • @sicr, did you find a solution about connection re-use? many thanks, Daniele
    – Daniele B
    Mar 15, 2014 at 18:56
5

about Body

// It is the caller's responsibility to
// close Body. The default HTTP client's Transport may not
// reuse HTTP/1.x "keep-alive" TCP connections if the Body is
// not read to completion and closed.

So if you want to reuse TCP connections, you have to close Body every time after read to completion. Also, with defer, you can make sure Body.Close() is called after all. An function ReadBody(io.ReadCloser) is suggested like this.

package main

import (
    "fmt"
    "io"
    "io/ioutil"
    "net/http"
    "time"
)

func main() {
    req, err := http.NewRequest(http.MethodGet, "https://github.com", nil)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    client := &http.Client{}
    i := 0
    for {
        resp, err := client.Do(req)
        if err != nil {
            fmt.Println(err.Error())
            return
        }
        _, _ = readBody(resp.Body)
        fmt.Println("done ", i)
        time.Sleep(5 * time.Second)
    }
}

func readBody(readCloser io.ReadCloser) ([]byte, error) {
    defer readCloser.Close()
    body, err := ioutil.ReadAll(readCloser)
    if err != nil {
        return nil, err
    }
    return body, nil
}

And don't call Close like below:

res, _ := client.Do(req)
io.Copy(ioutil.Discard, res.Body) // what if io.Copy panics, res.Body.Close() will not called.
res.Body.Close()
3

Another approach to init() is to use a singleton method to get the http client. By using sync.Once you can be sure that only one instance will be used on all your requests.

var (
    once              sync.Once
    netClient         *http.Client
)

func newNetClient() *http.Client {
    once.Do(func() {
        var netTransport = &http.Transport{
            Dial: (&net.Dialer{
                Timeout: 2 * time.Second,
            }).Dial,
            TLSHandshakeTimeout: 2 * time.Second,
        }
        netClient = &http.Client{
            Timeout:   time.Second * 2,
            Transport: netTransport,
        }
    })

    return netClient
}

func yourFunc(){
    URL := "local.dev"
    req, err := http.NewRequest("POST", URL, nil)
    response, err := newNetClient().Do(req)
    // ...
}

2
  • This worked perfectly for me handling 100 HTTP requests per seond May 2, 2020 at 16:30
  • @philipmudenyo How you test 100 HTTP requests per second. What command you used for testing
    – m suresh
    Dec 15, 2021 at 8:08
2

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.

0

https://golang.org/src/net/http/transport.go#L196

you should set MaxConnsPerHost explicitly to your http.Client. Transport does reuse the TCP connection, but you should limit the MaxConnsPerHost (default 0 means no limit).

func init() {
    // singleton http.Client
    httpClient = createHTTPClient()
}

// createHTTPClient for connection re-use
func createHTTPClient() *http.Client {
    client := &http.Client{
        Transport: &http.Transport{
            MaxConnsPerHost:     1,
            // other option field
        },
        Timeout: time.Duration(RequestTimeout) * time.Second,
    }

    return client
}
0

It is very useful function for GO http call, you can keep connection alive and resue this connection.

    var (
        respReadLimit       = int64(4096)
    )
    
    // Try to read the response body so we can reuse this connection.
    func (c *Client) drainBody(body io.ReadCloser) error {
        defer body.Close()
        _, err := io.Copy(ioutil.Discard, io.LimitReader(body, respReadLimit))
        if err != nil {
            return err
        }
        return nil
    }
-3

There are two possible ways:

  1. Use a library that internally reuses and manages the file descriptors, associated with each requests. Http Client does the same thing internally, but then you would have the control over how many concurrent connections to open, and how to manage your resources. If you are interested, look at the netpoll implementation, which internally uses epoll/kqueue to manage them.

  2. The easy one would be, instead of pooling network connections, create a worker pool, for your goroutines. This would be easy, and better solution, that would not hinder with your current codebase, and would require minor changes.

Let's assume you need to make n POST request, after you recieve a request.

enter image description here

enter image description here

You could use channels, to implement this.

Or, simply you could use third party libraries.
Like: https://github.com/ivpusic/grpool

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.