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

ClientConn: Introduce GetStatus method to get the latest connection error #2055

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions clientconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,16 @@ func (cc *ClientConn) GetState() connectivity.State {
return cc.csMgr.getState()
}

// GetStatus returns the status of cc as an error produced by the status
// package. If the state is TransientFailure, the status error will be
// non-nil. Otherwise, it will always be nil.
func (cc *ClientConn) GetStatus() error {
if cc.GetState() != connectivity.TransientFailure {
return nil
}
return status.Errorf(codes.Unavailable, "latest connection error: %v", cc.blockingpicker.connectionError())
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the advantage of using status error here?
What other error codes will we return?

Copy link
Member Author

Choose a reason for hiding this comment

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

It mirrors the Java API. That one returns a status directly, but I thought "error" would be more idiomatic for us.

}

func (cc *ClientConn) scWatcher() {
for {
select {
Expand Down Expand Up @@ -877,6 +887,9 @@ func (cc *ClientConn) removeAddrConn(ac *addrConn, err error) {
return
}
delete(cc.conns, ac)
if len(cc.conns) == 0 {
cc.blockingpicker.updateConnectionError(errors.New("no backends returned by resolver"))
Copy link
Contributor

Choose a reason for hiding this comment

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

What if the balancer wanted to remove the last addrConn from cc? It will also be zero in that case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Isn't this that code path?

}
cc.mu.Unlock()
ac.tearDown(err)
}
Expand Down
13 changes: 12 additions & 1 deletion clientconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ package grpc
import (
"math"
"net"
"strings"
"sync/atomic"
"testing"
"time"

"golang.org/x/net/context"
"golang.org/x/net/http2"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/naming"
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/resolver/manual"
_ "google.golang.org/grpc/resolver/passthrough"
"google.golang.org/grpc/status"
"google.golang.org/grpc/test/leakcheck"
"google.golang.org/grpc/testdata"
)
Expand Down Expand Up @@ -309,6 +312,9 @@ func TestConnectivityStates(t *testing.T) {
if state, ok := assertState(wantState, cc); !ok {
t.Fatalf("asserState(%s) = %s, false, want %s, true", wantState, state, wantState)
}
if err := cc.GetStatus(); err != nil {
t.Fatalf("cc.GetStatus() = %v; want <nil>", err)
}
// Send an update to delete the server connection (tearDown addrConn).
update := []*naming.Update{
{
Expand All @@ -321,6 +327,9 @@ func TestConnectivityStates(t *testing.T) {
if state, ok := assertState(wantState, cc); !ok {
t.Fatalf("asserState(%s) = %s, false, want %s, true", wantState, state, wantState)
}
if err := cc.GetStatus(); status.Code(err) != codes.Unavailable || !strings.Contains(status.Convert(err).Message(), "no backends returned by resolver") {
t.Fatalf("cc.GetStatus() = %v; want <code=%v, msg contains %q>", err, codes.Unavailable, "no backends returned by resolver")
}
update[0] = &naming.Update{
Op: naming.Add,
Addr: "localhost:" + servers[1].port,
Expand All @@ -330,7 +339,9 @@ func TestConnectivityStates(t *testing.T) {
if state, ok := assertState(wantState, cc); !ok {
t.Fatalf("asserState(%s) = %s, false, want %s, true", wantState, state, wantState)
}

if err := cc.GetStatus(); err != nil {
t.Fatalf("cc.GetStatus() = %v; want <nil>", err)
}
}

func TestWithTimeout(t *testing.T) {
Expand Down