mirror of https://github.com/dapr/go-sdk.git
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:
parent
b5d09e8e57
commit
5d3a4ad3f2
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue