Skip to content

Commit

Permalink
Don't check the response body close error in the client
Browse files Browse the repository at this point in the history
If we successfully read the response all the way to EOF and decode it,
that constitutes a successful RPC. If the body close happens to return a
non-nil error, the client shouldn't care, since it was able to fully
read the response.
  • Loading branch information
cespare committed Nov 15, 2021
1 parent c4c8032 commit e0e9a4d
Show file tree
Hide file tree
Showing 17 changed files with 16 additions and 165 deletions.
8 changes: 1 addition & 7 deletions clientcompat/internal/clientcompat/clientcompat.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions example/service.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 0 additions & 53 deletions internal/twirptest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package twirptest
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -681,55 +680,3 @@ func (c *wrappedHTTPClient) Do(req *http.Request) (resp *http.Response, err erro
c.wasCalled = true
return c.client.Do(req)
}

func TestClientReturnsCloseErrors(t *testing.T) {
h := PickyHatmaker(1)
s := httptest.NewServer(NewHaberdasherServer(h, nil))
defer s.Close()

httpClient := &bodyCloseErrClient{base: http.DefaultClient}

testcase := func(client Haberdasher) func(*testing.T) {
return func(t *testing.T) {
_, err := client.MakeHat(context.Background(), &Size{Inches: 1})
if err == nil {
t.Error("expected an error when body fails to close, have nil")
} else {
if errors.Cause(err) != bodyCloseErr {
t.Errorf("got wrong root cause for error, have=%v, want=%v", err, bodyCloseErr)
}
}
}
}
t.Run("json client", testcase(NewHaberdasherJSONClient(s.URL, httpClient)))
t.Run("protobuf client", testcase(NewHaberdasherProtobufClient(s.URL, httpClient)))
}

// bodyCloseErrClient implements HTTPClient, but the response bodies it returns
// give an error when they are closed.
type bodyCloseErrClient struct {
base HTTPClient
}

func (c *bodyCloseErrClient) Do(req *http.Request) (*http.Response, error) {
resp, err := c.base.Do(req)
if resp == nil {
return resp, err
}
resp.Body = &errBodyCloser{resp.Body}
return resp, nil
}

var bodyCloseErr = errors.New("failed closing")

type errBodyCloser struct {
base io.ReadCloser
}

func (ec *errBodyCloser) Read(p []byte) (int, error) {
return ec.base.Read(p)
}

func (ec *errBodyCloser) Close() error {
return bodyCloseErr
}
8 changes: 1 addition & 7 deletions internal/twirptest/empty_service/empty_service.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions internal/twirptest/google_protobuf_imports/service.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions internal/twirptest/importable/importable.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions internal/twirptest/importer/importer.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions internal/twirptest/importer_local/importer_local.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions internal/twirptest/importmapping/x/x.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions internal/twirptest/multiple/multiple1.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions internal/twirptest/no_package_name/no_package_name.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions internal/twirptest/service.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions protoc-gen-twirp/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,13 +794,7 @@ func (t *twirp) generateUtils() {
t.P(` if err != nil {`)
t.P(` return ctx, wrapInternal(err, "failed to do request")`)
t.P(` }`)
t.P()
t.P(` defer func() {`)
t.P(` cerr := resp.Body.Close()`)
t.P(` if err == nil && cerr != nil {`)
t.P(` err = wrapInternal(cerr, "failed to close response body")`)
t.P(` }`)
t.P(` }()`)
t.P(` defer func() { _ = resp.Body.Close() }()`)
t.P()
t.P(` if err = ctx.Err(); err != nil {`)
t.P(` return ctx, wrapInternal(err, "aborted because context was done")`)
Expand Down

0 comments on commit e0e9a4d

Please sign in to comment.