Make client init fail if server connection fails (#213)

* Make client init fail if server connection fails

This prevents client/server connectivity issues from resulting in silent
failure modes and ensures they're caught as early as possible

* Fix linter errors

Co-authored-by: Yaron Schneider <yaronsc@microsoft.com>
This commit is contained in:
Michael Nussbaum 2021-10-31 14:59:07 -07:00 committed by GitHub
parent b5d09e8e57
commit 5d3a4ad3f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import (
"net"
"os"
"sync"
"time"
"github.com/pkg/errors"
"google.golang.org/grpc"
@ -141,35 +142,54 @@ func NewClientWithAddress(address string) (client Client, err error) {
return nil, errors.New("nil address")
}
logger.Printf("dapr client initializing for: %s", address)
conn, err := grpc.Dial(address, grpc.WithInsecure())
ctx, ctxCancel := context.WithTimeout(context.Background(), 1*time.Second)
conn, err := grpc.DialContext(
ctx,
address,
grpc.WithInsecure(),
grpc.WithBlock(),
)
if err != nil {
ctxCancel()
return nil, errors.Wrapf(err, "error creating connection to '%s': %v", address, err)
}
if hasToken := os.Getenv(apiTokenEnvVarName); hasToken != "" {
logger.Println("client uses API token")
}
return NewClientWithConnection(conn), nil
return newClientWithConnectionAndCancelFunc(conn, ctxCancel), nil
}
// NewClientWithConnection instantiates Dapr client using specific connection.
func NewClientWithConnection(conn *grpc.ClientConn) Client {
return newClientWithConnectionAndCancelFunc(conn, func() {})
}
func newClientWithConnectionAndCancelFunc(
conn *grpc.ClientConn,
cancelFunc context.CancelFunc,
) Client {
return &GRPCClient{
connection: conn,
protoClient: pb.NewDaprClient(conn),
authToken: os.Getenv(apiTokenEnvVarName),
connection: conn,
ctxCancelFunc: cancelFunc,
protoClient: pb.NewDaprClient(conn),
authToken: os.Getenv(apiTokenEnvVarName),
}
}
// GRPCClient is the gRPC implementation of Dapr client.
type GRPCClient struct {
connection *grpc.ClientConn
protoClient pb.DaprClient
authToken string
mux sync.Mutex
connection *grpc.ClientConn
ctxCancelFunc context.CancelFunc
protoClient pb.DaprClient
authToken string
mux sync.Mutex
}
// Close cleans up all resources created by the client.
func (c *GRPCClient) Close() {
c.ctxCancelFunc()
if c.connection != nil {
c.connection.Close()
}

View File

@ -35,6 +35,11 @@ func TestMain(m *testing.M) {
}
func TestNewClient(t *testing.T) {
t.Run("return error when unable to reach server", func(t *testing.T) {
_, err := NewClientWithPort("1")
assert.Error(t, err)
})
t.Run("no arg for with port", func(t *testing.T) {
_, err := NewClientWithPort("")
assert.Error(t, err)
@ -46,18 +51,11 @@ func TestNewClient(t *testing.T) {
})
t.Run("new client closed with empty token", func(t *testing.T) {
c, err := NewClient()
assert.NoError(t, err)
defer c.Close()
c.WithAuthToken("")
testClient.WithAuthToken("")
})
t.Run("new client with trace ID", func(t *testing.T) {
c, err := NewClient()
assert.NoError(t, err)
defer c.Close()
ctx := c.WithTraceID(context.Background(), "")
_ = c.WithTraceID(ctx, "test")
_ = testClient.WithTraceID(context.Background(), "test")
})
}