You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/rpcx/internal/discovclient.go

37 lines
747 B
Go

package internal
import (
"zero/core/discov"
"zero/rpcx/internal/balancer/roundrobin"
"zero/rpcx/internal/resolver"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
)
type DiscovClient struct {
conn *grpc.ClientConn
}
func NewDiscovClient(etcd discov.EtcdConf, opts ...ClientOption) (*DiscovClient, error) {
resolver.RegisterResolver(etcd)
opts = append(opts, WithDialOption(grpc.WithBalancerName(roundrobin.Name)))
conn, err := dial("discov:///", opts...)
if err != nil {
return nil, err
}
return &DiscovClient{
conn: conn,
}, nil
}
func (c *DiscovClient) Next() (*grpc.ClientConn, bool) {
state := c.conn.GetState()
if state == connectivity.Ready {
return c.conn, true
} else {
return nil, false
}
}