|
|
|
@ -10,11 +10,22 @@ import (
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type RpcClient struct {
|
|
|
|
|
client internal.Client
|
|
|
|
|
}
|
|
|
|
|
var (
|
|
|
|
|
WithDialOption = internal.WithDialOption
|
|
|
|
|
WithTimeout = internal.WithTimeout
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
|
Client interface {
|
|
|
|
|
Conn() *grpc.ClientConn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RpcClient struct {
|
|
|
|
|
client Client
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func MustNewClient(c RpcClientConf, options ...internal.ClientOption) *RpcClient {
|
|
|
|
|
func MustNewClient(c RpcClientConf, options ...internal.ClientOption) Client {
|
|
|
|
|
cli, err := NewClient(c, options...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
@ -23,20 +34,20 @@ func MustNewClient(c RpcClientConf, options ...internal.ClientOption) *RpcClient
|
|
|
|
|
return cli
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewClient(c RpcClientConf, options ...internal.ClientOption) (*RpcClient, error) {
|
|
|
|
|
func NewClient(c RpcClientConf, options ...internal.ClientOption) (Client, error) {
|
|
|
|
|
var opts []internal.ClientOption
|
|
|
|
|
if c.HasCredential() {
|
|
|
|
|
opts = append(opts, internal.WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
|
|
|
|
|
opts = append(opts, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
|
|
|
|
|
App: c.App,
|
|
|
|
|
Token: c.Token,
|
|
|
|
|
})))
|
|
|
|
|
}
|
|
|
|
|
if c.Timeout > 0 {
|
|
|
|
|
opts = append(opts, internal.WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
|
|
|
|
|
opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
|
|
|
|
|
}
|
|
|
|
|
opts = append(opts, options...)
|
|
|
|
|
|
|
|
|
|
var client internal.Client
|
|
|
|
|
var client Client
|
|
|
|
|
var err error
|
|
|
|
|
if len(c.Server) > 0 {
|
|
|
|
|
client, err = internal.NewDirectClient(c.Server, opts...)
|
|
|
|
@ -52,7 +63,7 @@ func NewClient(c RpcClientConf, options ...internal.ClientOption) (*RpcClient, e
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewClientNoAuth(c discov.EtcdConf) (*RpcClient, error) {
|
|
|
|
|
func NewClientNoAuth(c discov.EtcdConf) (Client, error) {
|
|
|
|
|
client, err := internal.NewDiscovClient(c.Hosts, c.Key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|