Fix leaking go routine issue

This commit is contained in:
Asim 2015-10-22 15:14:56 +01:00
parent 9697dc503c
commit 5e598d8ef0
2 changed files with 16 additions and 2 deletions

View File

@ -82,7 +82,11 @@ func (r *rpcClient) call(ctx context.Context, address string, request Request, r
defer c.Close() defer c.Close()
client := rpc.NewClientWithCodec(newRpcPlusCodec(msg, c)) client := rpc.NewClientWithCodec(newRpcPlusCodec(msg, c))
return client.Call(ctx, request.Method(), request.Request(), response) err = client.Call(ctx, request.Method(), request.Request(), response)
if err != nil {
return err
}
return client.Close()
} }
func (r *rpcClient) stream(ctx context.Context, address string, request Request, responseChan interface{}) (Streamer, error) { func (r *rpcClient) stream(ctx context.Context, address string, request Request, responseChan interface{}) (Streamer, error) {

View File

@ -4,10 +4,12 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"errors" "errors"
"io"
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"sync"
) )
type httpTransport struct{} type httpTransport struct{}
@ -19,6 +21,7 @@ type httpTransportClient struct {
buff *bufio.Reader buff *bufio.Reader
dialOpts dialOptions dialOpts dialOptions
r chan *http.Request r chan *http.Request
once sync.Once
} }
type httpTransportSocket struct { type httpTransportSocket struct {
@ -63,7 +66,11 @@ func (h *httpTransportClient) Send(m *Message) error {
func (h *httpTransportClient) Recv(m *Message) error { func (h *httpTransportClient) Recv(m *Message) error {
var r *http.Request var r *http.Request
if !h.dialOpts.stream { if !h.dialOpts.stream {
r = <-h.r rc, ok := <-h.r
if !ok {
return io.EOF
}
r = rc
} }
rsp, err := http.ReadResponse(h.buff, r) rsp, err := http.ReadResponse(h.buff, r)
@ -96,6 +103,9 @@ func (h *httpTransportClient) Recv(m *Message) error {
func (h *httpTransportClient) Close() error { func (h *httpTransportClient) Close() error {
h.buff.Reset(nil) h.buff.Reset(nil)
h.once.Do(func() {
close(h.r)
})
return h.conn.Close() return h.conn.Close()
} }