Skip to content

Commit

Permalink
*: drain http.Response.Body before closing
Browse files Browse the repository at this point in the history
  • Loading branch information
gyuho committed Mar 30, 2016
1 parent 2deed74 commit 83e6ceb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
3 changes: 2 additions & 1 deletion etcdserver/cluster_util.go
Expand Up @@ -22,6 +22,7 @@ import (
"sort"
"time"

"github.com/coreos/etcd/pkg/httputil"
"github.com/coreos/etcd/pkg/types"
"github.com/coreos/etcd/version"
"github.com/coreos/go-semver/semver"
Expand Down Expand Up @@ -231,7 +232,7 @@ func getVersion(m *Member, rt http.RoundTripper) (*version.Versions, error) {
}
// etcd 2.0 does not have version endpoint on peer url.
if resp.StatusCode == http.StatusNotFound {
resp.Body.Close()
httputil.GracefulClose(resp)
return &version.Versions{
Server: "2.0.0",
Cluster: "2.0.0",
Expand Down
14 changes: 13 additions & 1 deletion pkg/httputil/cancelreq.go → pkg/httputil/httputil.go
Expand Up @@ -7,7 +7,11 @@
// Package httputil provides HTTP utility functions.
package httputil

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

func RequestCanceler(rt http.RoundTripper, req *http.Request) func() {
ch := make(chan struct{})
Expand All @@ -17,3 +21,11 @@ func RequestCanceler(rt http.RoundTripper, req *http.Request) func() {
close(ch)
}
}

// GracefulClose drains http.Response.Body until it hits EOF
// and closes it. This prevents TCP/TLS connections from closing,
// therefore available for reuse.
func GracefulClose(resp *http.Response) {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
2 changes: 1 addition & 1 deletion rafthttp/snapshot_sender.go
Expand Up @@ -124,7 +124,7 @@ func (s *snapshotSender) post(req *http.Request) (err error) {
// close the response body when timeouts.
// prevents from reading the body forever when the other side dies right after
// successfully receives the request body.
time.AfterFunc(snapResponseReadTimeout, func() { resp.Body.Close() })
time.AfterFunc(snapResponseReadTimeout, func() { httputil.GracefulClose(resp) })
body, err := ioutil.ReadAll(resp.Body)
result <- responseAndError{resp, body, err}
}()
Expand Down
10 changes: 5 additions & 5 deletions rafthttp/stream.go
Expand Up @@ -417,14 +417,14 @@ func (cr *streamReader) dial(t streamType) (io.ReadCloser, error) {
rv := serverVersion(resp.Header)
lv := semver.Must(semver.NewVersion(version.Version))
if compareMajorMinorVersion(rv, lv) == -1 && !checkStreamSupport(rv, t) {
resp.Body.Close()
httputil.GracefulClose(resp)
cr.picker.unreachable(u)
return nil, errUnsupportedStreamType
}

switch resp.StatusCode {
case http.StatusGone:
resp.Body.Close()
httputil.GracefulClose(resp)
cr.picker.unreachable(u)
err := fmt.Errorf("the member has been permanently removed from the cluster")
select {
Expand All @@ -435,7 +435,7 @@ func (cr *streamReader) dial(t streamType) (io.ReadCloser, error) {
case http.StatusOK:
return resp.Body, nil
case http.StatusNotFound:
resp.Body.Close()
httputil.GracefulClose(resp)
cr.picker.unreachable(u)
return nil, fmt.Errorf("remote member %s could not recognize local member", cr.remote)
case http.StatusPreconditionFailed:
Expand All @@ -444,7 +444,7 @@ func (cr *streamReader) dial(t streamType) (io.ReadCloser, error) {
cr.picker.unreachable(u)
return nil, err
}
resp.Body.Close()
httputil.GracefulClose(resp)
cr.picker.unreachable(u)

switch strings.TrimSuffix(string(b), "\n") {
Expand All @@ -459,7 +459,7 @@ func (cr *streamReader) dial(t streamType) (io.ReadCloser, error) {
return nil, fmt.Errorf("unhandled error %q when precondition failed", string(b))
}
default:
resp.Body.Close()
httputil.GracefulClose(resp)
cr.picker.unreachable(u)
return nil, fmt.Errorf("unhandled http status %d", resp.StatusCode)
}
Expand Down
1 change: 1 addition & 0 deletions rafthttp/util.go
Expand Up @@ -19,6 +19,7 @@ import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down

0 comments on commit 83e6ceb

Please sign in to comment.