代码拉取完成,页面将自动刷新
package discover
import (
"context"
"time"
"github.com/lukeaxu/discover/registry"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type ClientOpt func(*Client)
type Client struct {
registry registry.Registry
insecure bool
timeout time.Duration
}
func ClientWithInsecure() ClientOpt {
return func(c *Client) {
c.insecure = true
}
}
func ClientWithRegistry(r registry.Registry, d time.Duration) ClientOpt {
return func(c *Client) {
c.registry = r
c.timeout = d
}
}
func NewClient(opts ...ClientOpt) (*Client, error) {
client := &Client{}
for _, opt := range opts {
opt(client)
}
return client, nil
}
func (c *Client) Dial(ctx context.Context, sn string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
var target = sn
dialOpts := make([]grpc.DialOption, 0)
if c.registry != nil {
rb, err := NewGrpcResolverBuilder(c.registry, c.timeout)
if err != nil {
return nil, err
}
dialOpts = append(dialOpts, grpc.WithResolvers(rb))
target = rb.targetForServiceName(sn)
}
if c.insecure {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
if len(opts) > 0 {
dialOpts = append(dialOpts, opts...)
}
cc, err := grpc.DialContext(ctx, target, dialOpts...)
return cc, err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。