go-zero/zrpc/client.go

113 lines
2.9 KiB
Go
Raw Normal View History

2020-09-18 11:41:52 +08:00
package zrpc
2020-07-26 17:09:05 +08:00
import (
"log"
"time"
"github.com/tal-tech/go-zero/core/discov"
2020-09-18 11:41:52 +08:00
"github.com/tal-tech/go-zero/zrpc/internal"
"github.com/tal-tech/go-zero/zrpc/internal/auth"
"github.com/tal-tech/go-zero/zrpc/internal/clientinterceptors"
2020-07-29 18:56:03 +08:00
"google.golang.org/grpc"
2020-07-26 17:09:05 +08:00
)
var (
2021-03-01 23:52:44 +08:00
// WithDialOption is an alias of internal.WithDialOption.
WithDialOption = internal.WithDialOption
// WithNonBlock sets the dialing to be nonblock.
WithNonBlock = internal.WithNonBlock
2021-03-01 23:52:44 +08:00
// WithTimeout is an alias of internal.WithTimeout.
WithTimeout = internal.WithTimeout
// WithRetry is an alias of internal.WithRetry.
WithRetry = internal.WithRetry
// WithTransportCredentials return a func to make the gRPC calls secured with given credentials.
WithTransportCredentials = internal.WithTransportCredentials
2021-03-01 23:52:44 +08:00
// WithUnaryClientInterceptor is an alias of internal.WithUnaryClientInterceptor.
2020-10-20 18:03:05 +08:00
WithUnaryClientInterceptor = internal.WithUnaryClientInterceptor
)
type (
2021-03-01 23:52:44 +08:00
// Client is an alias of internal.Client.
Client = internal.Client
// ClientOption is an alias of internal.ClientOption.
ClientOption = internal.ClientOption
2021-03-01 23:52:44 +08:00
// A RpcClient is a rpc client.
RpcClient struct {
client Client
}
)
2020-07-26 17:09:05 +08:00
2021-03-01 23:52:44 +08:00
// MustNewClient returns a Client, exits on any error.
func MustNewClient(c RpcClientConf, options ...ClientOption) Client {
2020-07-26 17:09:05 +08:00
cli, err := NewClient(c, options...)
if err != nil {
log.Fatal(err)
}
return cli
}
2021-03-01 23:52:44 +08:00
// NewClient returns a Client.
func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
var opts []ClientOption
2020-07-26 17:09:05 +08:00
if c.HasCredential() {
opts = append(opts, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
2020-07-26 17:09:05 +08:00
App: c.App,
Token: c.Token,
})))
}
if c.NonBlock {
opts = append(opts, WithNonBlock())
}
2020-07-26 17:09:05 +08:00
if c.Timeout > 0 {
opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
2020-07-26 17:09:05 +08:00
}
if c.Retry {
opts = append(opts, WithRetry())
}
2020-07-26 17:09:05 +08:00
opts = append(opts, options...)
var target string
2020-07-26 17:09:05 +08:00
var err error
2020-08-18 18:36:44 +08:00
if len(c.Endpoints) > 0 {
target = internal.BuildDirectTarget(c.Endpoints)
} else if len(c.Target) > 0 {
target = c.Target
} else {
if err = c.Etcd.Validate(); err != nil {
return nil, err
}
if c.Etcd.HasAccount() {
discov.RegisterAccount(c.Etcd.Hosts, c.Etcd.User, c.Etcd.Pass)
}
target = internal.BuildDiscovTarget(c.Etcd.Hosts, c.Etcd.Key)
2020-07-26 17:09:05 +08:00
}
client, err := internal.NewClient(target, opts...)
2020-07-26 17:09:05 +08:00
if err != nil {
return nil, err
}
return &RpcClient{
client: client,
}, nil
}
2021-03-01 23:52:44 +08:00
// NewClientWithTarget returns a Client with connecting to given target.
func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
2020-08-18 18:36:44 +08:00
return internal.NewClient(target, opts...)
}
2021-03-01 23:52:44 +08:00
// Conn returns the underlying grpc.ClientConn.
2020-08-06 20:55:38 +08:00
func (rc *RpcClient) Conn() *grpc.ClientConn {
return rc.client.Conn()
2020-07-26 17:09:05 +08:00
}
// SetClientSlowThreshold sets the slow threshold on client side.
func SetClientSlowThreshold(threshold time.Duration) {
clientinterceptors.SetSlowThreshold(threshold)
}