diff --git a/transport/http_client.go b/transport/http_client.go index e43ce15177..8fa0a40dd8 100644 --- a/transport/http_client.go +++ b/transport/http_client.go @@ -147,8 +147,8 @@ func (h *httpTransportClient) Recv(msg *Message) (err error) { } defer func() { - if err = rsp.Body.Close(); err != nil { - err = errors.Wrap(err, "failed to close body") + if err2 := rsp.Body.Close(); err2 != nil { + err = errors.Wrap(err2, "failed to close body") } }() @@ -180,25 +180,29 @@ func (h *httpTransportClient) Recv(msg *Message) (err error) { func (h *httpTransportClient) Close() error { if !h.dialOpts.Stream { - h.once.Do(func() { - h.Lock() - h.buff.Reset(nil) - h.closed = true - h.Unlock() - close(h.req) - }) + h.once.Do( + func() { + h.Lock() + h.buff.Reset(nil) + h.closed = true + h.Unlock() + close(h.req) + }, + ) return h.conn.Close() } err := h.conn.Close() - h.once.Do(func() { - h.Lock() - h.buff.Reset(nil) - h.closed = true - h.Unlock() - close(h.req) - }) + h.once.Do( + func() { + h.Lock() + h.buff.Reset(nil) + h.closed = true + h.Unlock() + close(h.req) + }, + ) return err } diff --git a/transport/http_transport_test.go b/transport/http_transport_test.go index 2f395b352e..23ec10d7b5 100644 --- a/transport/http_transport_test.go +++ b/transport/http_transport_test.go @@ -1,6 +1,7 @@ package transport import ( + "errors" "io" "net" "sync" @@ -126,7 +127,7 @@ func TestHTTPTransportError(t *testing.T) { for { var m Message if err := sock.Recv(&m); err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { return } t.Fatal(err) @@ -335,7 +336,7 @@ func TestHTTPTransportMultipleSendWhenRecv(t *testing.T) { Body: []byte(`{"message": "Hello World"}`), } - wgSend := sync.WaitGroup{} + var wgSend sync.WaitGroup fn := func(sock Socket) { defer sock.Close() @@ -344,7 +345,6 @@ func TestHTTPTransportMultipleSendWhenRecv(t *testing.T) { if err := sock.Recv(&mr); err != nil { return } - wgSend.Add(1) go func() { defer wgSend.Done() <-readyToSend @@ -388,6 +388,7 @@ func TestHTTPTransportMultipleSendWhenRecv(t *testing.T) { } } }() + wgSend.Add(3) <-readyForRecv for i := 0; i < 3; i++ { if err := c.Send(&m); err != nil {