Skip to content

Commit

Permalink
Merge pull request #24 from powerman/23-fix-leak-on-close
Browse files Browse the repository at this point in the history
fix goroutine leak on HTTP Client.Close
  • Loading branch information
powerman committed Mar 1, 2018
2 parents f48fde4 + 7e75941 commit 3e1ab3b
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion jsonrpc2/http.go
Expand Up @@ -102,11 +102,16 @@ type httpClientConn struct {
doer Doer
ready chan io.ReadCloser
body io.ReadCloser
close chan struct{}
}

func (conn *httpClientConn) Read(buf []byte) (int, error) {
if conn.body == nil {
conn.body = <-conn.ready
select {
case <-conn.close:
return 0, io.EOF
case conn.body = <-conn.ready:
}
}
n, err := conn.body.Read(buf)
if err == io.EOF {
Expand Down Expand Up @@ -173,6 +178,7 @@ func (conn *httpClientConn) Write(buf []byte) (int, error) {
}

func (conn *httpClientConn) Close() error {
close(conn.close)
return nil
}

Expand All @@ -197,5 +203,6 @@ func NewCustomHTTPClient(url string, doer Doer) *Client {
url: url,
doer: doer,
ready: make(chan io.ReadCloser, 16),
close: make(chan struct{}),
})
}

0 comments on commit 3e1ab3b

Please sign in to comment.