Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect unary should return unavailable instead of unimplemented for io.EOF errors from the transport #776

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ linters-settings:
importas:
no-unaliased: true
alias:
- pkg: connectrpc.com/connect
alias: connect
Comment on lines -22 to -23
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since v1.11.0 (the move to the connectrpc.com domain), it is no longer necessary/helpful to always have an import alias here. So I've removed this requirement and removed the (superfluous) alias in the file I touched below.

- pkg: connectrpc.com/connect/internal/gen/connect/ping/v1
alias: pingv1
varnamelen:
Expand Down
60 changes: 59 additions & 1 deletion client_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"testing"
"time"

connect "connectrpc.com/connect"
"connectrpc.com/connect"
"connectrpc.com/connect/internal/assert"
pingv1 "connectrpc.com/connect/internal/gen/connect/ping/v1"
"connectrpc.com/connect/internal/gen/connect/ping/v1/pingv1connect"
Expand Down Expand Up @@ -227,6 +227,58 @@ func TestGetNoContentHeaders(t *testing.T) {
assert.Equal(t, http.MethodGet, unaryReq.HTTPMethod())
}

func TestConnectionDropped(t *testing.T) {
t.Parallel()
ctx := context.Background()
for _, protocol := range []string{connect.ProtocolConnect, connect.ProtocolGRPC, connect.ProtocolGRPCWeb} {
emcfarlane marked this conversation as resolved.
Show resolved Hide resolved
var opts []connect.ClientOption
switch protocol {
case connect.ProtocolGRPC:
opts = []connect.ClientOption{connect.WithGRPC()}
case connect.ProtocolGRPCWeb:
opts = []connect.ClientOption{connect.WithGRPCWeb()}
}
t.Run(protocol, func(t *testing.T) {
t.Parallel()
httpClient := httpClientFunc(func(_ *http.Request) (*http.Response, error) {
return nil, io.EOF
})
client := pingv1connect.NewPingServiceClient(
httpClient,
"http://1.2.3.4",
opts...,
)
t.Run("unary", func(t *testing.T) {
t.Parallel()
req := connect.NewRequest[pingv1.PingRequest](nil)
_, err := client.Ping(ctx, req)
assert.NotNil(t, err)
if !assert.Equal(t, connect.CodeOf(err), connect.CodeUnavailable) {
t.Logf("err = %v\n%#v", err, err)
}
})
t.Run("stream", func(t *testing.T) {
t.Parallel()
req := connect.NewRequest[pingv1.CountUpRequest](nil)
svrStream, err := client.CountUp(ctx, req)
if err == nil {
t.Cleanup(func() {
assert.Nil(t, svrStream.Close())
})
if !assert.False(t, svrStream.Receive()) {
return
}
err = svrStream.Err()
}
assert.NotNil(t, err)
if !assert.Equal(t, connect.CodeOf(err), connect.CodeUnavailable) {
t.Logf("err = %v\n%#v", err, err)
}
})
})
}
}

func TestSpecSchema(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
Expand Down Expand Up @@ -762,3 +814,9 @@ func addUnrecognizedBytes[M proto.Message](msg M, data []byte) M {
msg.ProtoReflect().SetUnknown(data)
return msg
}

type httpClientFunc func(*http.Request) (*http.Response, error)

func (fn httpClientFunc) Do(req *http.Request) (*http.Response, error) {
return fn(req)
}
5 changes: 5 additions & 0 deletions duplex_http_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ func (d *duplexHTTPCall) makeRequest() {
// pipe. Write's check for io.ErrClosedPipe and will convert this to io.EOF.
response, err := d.httpClient.Do(d.request) //nolint:bodyclose
if err != nil {
if errors.Is(err, io.EOF) {
// We use io.EOF as a sentinel in many places and don't want this
// transport error to be confused for those other situations.
err = io.ErrUnexpectedEOF
}
emcfarlane marked this conversation as resolved.
Show resolved Hide resolved
err = wrapIfContextError(err)
err = wrapIfLikelyH2CNotConfiguredError(d.request, err)
err = wrapIfLikelyWithGRPCNotUsedError(err)
Expand Down