Skip to content

Commit

Permalink
Read HTTP response body and close it.
Browse files Browse the repository at this point in the history
When an unread response body exists, http.Client cannot reuse
the TCP connection for the next request.
  • Loading branch information
fujiwara committed Aug 4, 2021
1 parent 9ce2e80 commit 6aa347d
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -137,9 +139,14 @@ func waitForDependencies() {
time.Sleep(waitRetryInterval)
} else if err == nil && resp.StatusCode >= 200 && resp.StatusCode < 300 {
log.Printf("Received %d from %s\n", resp.StatusCode, u.String())
// dispose the response body and close it.
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
return
} else {
log.Printf("Received %d from %s. Sleeping %s\n", resp.StatusCode, u.String(), waitRetryInterval)
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
time.Sleep(waitRetryInterval)
}
}
Expand Down

0 comments on commit 6aa347d

Please sign in to comment.