Skip to content

Commit

Permalink
[fix] Do not overwrite the error in http client
Browse files Browse the repository at this point in the history
  • Loading branch information
Ak-Army committed Jul 2, 2024
1 parent 24484c9 commit c204e05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
36 changes: 20 additions & 16 deletions transport/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}()

Expand Down Expand Up @@ -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
}
7 changes: 4 additions & 3 deletions transport/http_transport_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package transport

import (
"errors"
"io"
"net"
"sync"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c204e05

Please sign in to comment.