55 "log"
66 "net"
77 "os"
8+ "sync"
89
910 "github.com/pkg/errors"
1011 "google.golang.org/grpc"
@@ -19,8 +20,10 @@ const (
1920)
2021
2122var (
22- logger = log .New (os .Stdout , "" , 0 )
23- _ Client = (* GRPCClient )(nil )
23+ logger = log .New (os .Stdout , "" , 0 )
24+ _ Client = (* GRPCClient )(nil )
25+ defaultClient Client
26+ doOnce sync.Once
2427)
2528
2629// Client is the interface for Dapr client implementation.
@@ -71,12 +74,25 @@ type Client interface {
7174}
7275
7376// NewClient instantiates Dapr client using DAPR_GRPC_PORT environment variable as port.
77+ // Note, this default factory function creates Dapr client only once. All subsequent invocations
78+ // will return the already created instance. To create multiple instances of the Dapr client,
79+ // use one of the parameterized factory functions:
80+ // NewClientWithPort(port string) (client Client, err error)
81+ // NewClientWithAddress(address string) (client Client, err error)
82+ // NewClientWithConnection(conn *grpc.ClientConn) Client
7483func NewClient () (client Client , err error ) {
7584 port := os .Getenv (daprPortEnvVarName )
7685 if port == "" {
7786 port = daprPortDefault
7887 }
79- return NewClientWithPort (port )
88+ var onceErr error
89+ doOnce .Do (func () {
90+ c , err := NewClientWithPort (port )
91+ onceErr = errors .Wrap (err , "error creating default client" )
92+ defaultClient = c
93+ })
94+
95+ return defaultClient , onceErr
8096}
8197
8298// NewClientWithPort instantiates Dapr using specific port.
0 commit comments