diff --git a/dmsgget/dmsgget_test.go b/dmsgget/dmsgget_test.go index a841b282d..125c914ff 100644 --- a/dmsgget/dmsgget_test.go +++ b/dmsgget/dmsgget_test.go @@ -172,7 +172,7 @@ func newHTTPClient(t *testing.T, dc disc.APIClient) *http.Client { t.Cleanup(func() { assert.NoError(t, dmsgC.Close()) }) <-dmsgC.Ready() - log := logging.MustGetLogger(fmt.Sprintf("http_client")) + log := logging.MustGetLogger("http_client") ctx, cancel := cmdutil.SignalContext(context.Background(), log) defer cancel() return &http.Client{Transport: dmsghttp.MakeHTTPTransport(ctx, dmsgC)} diff --git a/dmsghttp/examples_test.go b/dmsghttp/examples_test.go index d8c47a9d2..3f242f279 100644 --- a/dmsghttp/examples_test.go +++ b/dmsghttp/examples_test.go @@ -15,6 +15,7 @@ import ( "github.com/skycoin/dmsg/cmdutil" "github.com/skycoin/dmsg/disc" "github.com/skycoin/dmsg/dmsghttp" + "github.com/skycoin/skycoin/src/util/logging" ) @@ -89,7 +90,7 @@ func ExampleMakeHTTPTransport() { go dmsgC2.Serve(context.Background()) <-dmsgC2.Ready() - log := logging.MustGetLogger(fmt.Sprintf("http_client")) + log := logging.MustGetLogger("http_client") ctx, cancel := cmdutil.SignalContext(context.Background(), log) defer cancel() // Run HTTP client. diff --git a/dmsghttp/http_transport.go b/dmsghttp/http_transport.go index 331a16282..cc55e969c 100644 --- a/dmsghttp/http_transport.go +++ b/dmsghttp/http_transport.go @@ -38,9 +38,6 @@ func (t HTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) { hostAddr.Port = defaultHTTPPort } - // TODO(evanlinjin): In the future, we should implement stream reuse to save bandwidth. - // We do not close the stream here as it is the user's responsibility to close the stream after resp.Body is fully - // read. stream, err := t.dmsgC.DialStream(req.Context(), hostAddr) if err != nil { return nil, err @@ -55,13 +52,13 @@ func (t HTTPTransport) RoundTrip(req *http.Request) (*http.Response, error) { } defer func() { - go test(t.ctx, resp, stream) + go closeStream(t.ctx, resp, stream) }() return resp, nil } -func test(ctx context.Context, resp *http.Response, stream *dmsg.Stream) { +func closeStream(ctx context.Context, resp *http.Response, stream *dmsg.Stream) { ticker := time.NewTicker(time.Second) defer ticker.Stop() @@ -72,12 +69,13 @@ func test(ctx context.Context, resp *http.Response, stream *dmsg.Stream) { case <-ticker.C: _, err := resp.Body.Read(nil) log := stream.Logger() - log.Errorf("err %v", err) - if err == nil { - // can still read from body so it's not closed - - } else if err != nil && err.Error() == "http: invalid Read on closed Body" { - stream.Close() + // If error is not nil and is equal to `http: invalid Read on closed Body` + // then it means that the body has been closed so we close the stream + if err != nil && err.Error() == "http: invalid Read on closed Body" { + err := stream.Close() + if err != nil { + log.Warnf("Error closing stream: %v", err) + } return } } diff --git a/dmsghttp/http_transport_test.go b/dmsghttp/http_transport_test.go index e43254005..253436712 100644 --- a/dmsghttp/http_transport_test.go +++ b/dmsghttp/http_transport_test.go @@ -64,7 +64,7 @@ func TestHTTPTransport_RoundTrip(t *testing.T) { startHTTPServer(t, server0Results, lis) addr := lis.Addr().String() - log := logging.MustGetLogger(fmt.Sprintf("http_client")) + log := logging.MustGetLogger("http_client") ctx, cancel := cmdutil.SignalContext(context.Background(), log) defer cancel() // Arrange: create http clients (in which each http client has an underlying dmsg client).