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/directclient.go

33 lines
653 B
Go

4 years ago
package internal
4 years ago
import (
"google.golang.org/grpc"
"google.golang.org/grpc/balancer/roundrobin"
"google.golang.org/grpc/connectivity"
)
type DirectClient struct {
conn *grpc.ClientConn
}
func NewDirectClient(server string, opts ...ClientOption) (*DirectClient, error) {
opts = append(opts, WithDialOption(grpc.WithBalancerName(roundrobin.Name)))
conn, err := dial(server, opts...)
if err != nil {
return nil, err
}
return &DirectClient{
conn: conn,
}, nil
}
func (c *DirectClient) Next() (*grpc.ClientConn, bool) {
state := c.conn.GetState()
if state == connectivity.Ready {
return c.conn, true
} else {
return nil, false
}
}