|
|
@ -32,36 +32,29 @@ type (
|
|
|
|
|
|
|
|
|
|
|
|
client struct {
|
|
|
|
client struct {
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
conn *grpc.ClientConn
|
|
|
|
|
|
|
|
interceptors []grpc.UnaryClientInterceptor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func NewClient(target string, opts ...ClientOption) (*client, error) {
|
|
|
|
func NewClient(target string, opts ...ClientOption) (*client, error) {
|
|
|
|
|
|
|
|
var cli client
|
|
|
|
opts = append(opts, WithDialOption(grpc.WithBalancerName(p2c.Name)))
|
|
|
|
opts = append(opts, WithDialOption(grpc.WithBalancerName(p2c.Name)))
|
|
|
|
conn, err := dial(target, opts...)
|
|
|
|
if err := cli.dial(target, opts...); err != nil {
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &client{conn: conn}, nil
|
|
|
|
return &cli, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c *client) Conn() *grpc.ClientConn {
|
|
|
|
func (c *client) AddInterceptor(interceptor grpc.UnaryClientInterceptor) {
|
|
|
|
return c.conn
|
|
|
|
c.interceptors = append(c.interceptors, interceptor)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func WithDialOption(opt grpc.DialOption) ClientOption {
|
|
|
|
|
|
|
|
return func(options *ClientOptions) {
|
|
|
|
|
|
|
|
options.DialOptions = append(options.DialOptions, opt)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func WithTimeout(timeout time.Duration) ClientOption {
|
|
|
|
func (c *client) Conn() *grpc.ClientConn {
|
|
|
|
return func(options *ClientOptions) {
|
|
|
|
return c.conn
|
|
|
|
options.Timeout = timeout
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func buildDialOptions(opts ...ClientOption) []grpc.DialOption {
|
|
|
|
func (c *client) buildDialOptions(opts ...ClientOption) []grpc.DialOption {
|
|
|
|
var clientOptions ClientOptions
|
|
|
|
var clientOptions ClientOptions
|
|
|
|
for _, opt := range opts {
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&clientOptions)
|
|
|
|
opt(&clientOptions)
|
|
|
@ -78,12 +71,15 @@ func buildDialOptions(opts ...ClientOption) []grpc.DialOption {
|
|
|
|
clientinterceptors.TimeoutInterceptor(clientOptions.Timeout),
|
|
|
|
clientinterceptors.TimeoutInterceptor(clientOptions.Timeout),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, interceptor := range c.interceptors {
|
|
|
|
|
|
|
|
options = append(options, WithUnaryClientInterceptors(interceptor))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return append(options, clientOptions.DialOptions...)
|
|
|
|
return append(options, clientOptions.DialOptions...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func dial(server string, opts ...ClientOption) (*grpc.ClientConn, error) {
|
|
|
|
func (c *client) dial(server string, opts ...ClientOption) error {
|
|
|
|
options := buildDialOptions(opts...)
|
|
|
|
options := c.buildDialOptions(opts...)
|
|
|
|
timeCtx, cancel := context.WithTimeout(context.Background(), dialTimeout)
|
|
|
|
timeCtx, cancel := context.WithTimeout(context.Background(), dialTimeout)
|
|
|
|
defer cancel()
|
|
|
|
defer cancel()
|
|
|
|
conn, err := grpc.DialContext(timeCtx, server, options...)
|
|
|
|
conn, err := grpc.DialContext(timeCtx, server, options...)
|
|
|
@ -96,9 +92,22 @@ func dial(server string, opts ...ClientOption) (*grpc.ClientConn, error) {
|
|
|
|
service = server[pos+1:]
|
|
|
|
service = server[pos+1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("rpc dial: %s, error: %s, make sure rpc service %q is alread started",
|
|
|
|
return fmt.Errorf("rpc dial: %s, error: %s, make sure rpc service %q is alread started",
|
|
|
|
server, err.Error(), service)
|
|
|
|
server, err.Error(), service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
c.conn = conn
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|