directclient.go 653 B

1234567891011121314151617181920212223242526272829303132
  1. package internal
  2. import (
  3. "google.golang.org/grpc"
  4. "google.golang.org/grpc/balancer/roundrobin"
  5. "google.golang.org/grpc/connectivity"
  6. )
  7. type DirectClient struct {
  8. conn *grpc.ClientConn
  9. }
  10. func NewDirectClient(server string, opts ...ClientOption) (*DirectClient, error) {
  11. opts = append(opts, WithDialOption(grpc.WithBalancerName(roundrobin.Name)))
  12. conn, err := dial(server, opts...)
  13. if err != nil {
  14. return nil, err
  15. }
  16. return &DirectClient{
  17. conn: conn,
  18. }, nil
  19. }
  20. func (c *DirectClient) Next() (*grpc.ClientConn, bool) {
  21. state := c.conn.GetState()
  22. if state == connectivity.Ready {
  23. return c.conn, true
  24. } else {
  25. return nil, false
  26. }
  27. }