initial import
This commit is contained in:
32
core/rpc/directclient.go
Normal file
32
core/rpc/directclient.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package rpc
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user