go-zero/zrpc/internal/client.go

91 lines
2.0 KiB
Go
Raw Normal View History

2020-07-29 18:06:57 +08:00
package internal
2020-07-26 17:09:05 +08:00
import (
"context"
"fmt"
"time"
2020-09-18 11:41:52 +08:00
"github.com/tal-tech/go-zero/zrpc/internal/balancer/p2c"
"github.com/tal-tech/go-zero/zrpc/internal/clientinterceptors"
"github.com/tal-tech/go-zero/zrpc/internal/resolver"
2020-07-26 17:09:05 +08:00
"google.golang.org/grpc"
)
const dialTimeout = time.Second * 3
2020-08-18 18:36:44 +08:00
func init() {
resolver.RegisterResolver()
}
2020-07-26 17:09:05 +08:00
type (
ClientOptions struct {
Timeout time.Duration
DialOptions []grpc.DialOption
}
ClientOption func(options *ClientOptions)
2020-08-18 18:36:44 +08:00
client struct {
conn *grpc.ClientConn
}
2020-07-26 17:09:05 +08:00
)
2020-08-18 18:36:44 +08:00
func NewClient(target string, opts ...ClientOption) (*client, error) {
opts = append(opts, WithDialOption(grpc.WithBalancerName(p2c.Name)))
conn, err := dial(target, opts...)
if err != nil {
return nil, err
}
return &client{conn: conn}, nil
}
func (c *client) Conn() *grpc.ClientConn {
return c.conn
}
2020-07-26 17:09:05 +08:00
func WithDialOption(opt grpc.DialOption) ClientOption {
return func(options *ClientOptions) {
options.DialOptions = append(options.DialOptions, opt)
}
}
func WithTimeout(timeout time.Duration) ClientOption {
return func(options *ClientOptions) {
options.Timeout = timeout
}
}
func buildDialOptions(opts ...ClientOption) []grpc.DialOption {
var clientOptions ClientOptions
for _, opt := range opts {
opt(&clientOptions)
}
options := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithBlock(),
WithUnaryClientInterceptors(
clientinterceptors.BreakerInterceptor,
clientinterceptors.DurationInterceptor,
clientinterceptors.PromMetricInterceptor,
clientinterceptors.TimeoutInterceptor(clientOptions.Timeout),
clientinterceptors.TracingInterceptor,
),
}
return append(options, clientOptions.DialOptions...)
}
func dial(server string, opts ...ClientOption) (*grpc.ClientConn, error) {
options := buildDialOptions(opts...)
timeCtx, cancel := context.WithTimeout(context.Background(), dialTimeout)
defer cancel()
conn, err := grpc.DialContext(timeCtx, server, options...)
if err != nil {
return nil, fmt.Errorf("rpc dial: %s, error: %s", server, err.Error())
}
return conn, nil
}