client.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package internal
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/tal-tech/go-zero/zrpc/internal/balancer/p2c"
  9. "github.com/tal-tech/go-zero/zrpc/internal/clientinterceptors"
  10. "github.com/tal-tech/go-zero/zrpc/internal/resolver"
  11. "google.golang.org/grpc"
  12. )
  13. const (
  14. dialTimeout = time.Second * 3
  15. separator = '/'
  16. )
  17. func init() {
  18. resolver.RegisterResolver()
  19. }
  20. type (
  21. // Client interface wraps the Conn method.
  22. Client interface {
  23. Conn() *grpc.ClientConn
  24. }
  25. // A ClientOptions is a client options.
  26. ClientOptions struct {
  27. Timeout time.Duration
  28. DialOptions []grpc.DialOption
  29. }
  30. // ClientOption defines the method to customize a ClientOptions.
  31. ClientOption func(options *ClientOptions)
  32. client struct {
  33. conn *grpc.ClientConn
  34. }
  35. )
  36. // NewClient returns a Client.
  37. func NewClient(target string, opts ...ClientOption) (Client, error) {
  38. var cli client
  39. opts = append([]ClientOption{WithDialOption(grpc.WithBalancerName(p2c.Name))}, opts...)
  40. if err := cli.dial(target, opts...); err != nil {
  41. return nil, err
  42. }
  43. return &cli, nil
  44. }
  45. func (c *client) Conn() *grpc.ClientConn {
  46. return c.conn
  47. }
  48. func (c *client) buildDialOptions(opts ...ClientOption) []grpc.DialOption {
  49. var cliOpts ClientOptions
  50. for _, opt := range opts {
  51. opt(&cliOpts)
  52. }
  53. options := []grpc.DialOption{
  54. grpc.WithInsecure(),
  55. grpc.WithBlock(),
  56. WithUnaryClientInterceptors(
  57. clientinterceptors.TracingInterceptor,
  58. clientinterceptors.DurationInterceptor,
  59. clientinterceptors.PrometheusInterceptor,
  60. clientinterceptors.BreakerInterceptor,
  61. clientinterceptors.TimeoutInterceptor(cliOpts.Timeout),
  62. clientinterceptors.OpenTracingInterceptor(),
  63. ),
  64. WithStreamClientInterceptors(
  65. clientinterceptors.StreamOpenTracingInterceptor(),
  66. ),
  67. }
  68. return append(options, cliOpts.DialOptions...)
  69. }
  70. func (c *client) dial(server string, opts ...ClientOption) error {
  71. options := c.buildDialOptions(opts...)
  72. timeCtx, cancel := context.WithTimeout(context.Background(), dialTimeout)
  73. defer cancel()
  74. conn, err := grpc.DialContext(timeCtx, server, options...)
  75. if err != nil {
  76. service := server
  77. if errors.Is(err, context.DeadlineExceeded) {
  78. pos := strings.LastIndexByte(server, separator)
  79. // len(server) - 1 is the index of last char
  80. if 0 < pos && pos < len(server)-1 {
  81. service = server[pos+1:]
  82. }
  83. }
  84. return fmt.Errorf("rpc dial: %s, error: %s, make sure rpc service %q is already started",
  85. server, err.Error(), service)
  86. }
  87. c.conn = conn
  88. return nil
  89. }
  90. // WithDialOption returns a func to customize a ClientOptions with given dial option.
  91. func WithDialOption(opt grpc.DialOption) ClientOption {
  92. return func(options *ClientOptions) {
  93. options.DialOptions = append(options.DialOptions, opt)
  94. }
  95. }
  96. // WithTimeout returns a func to customize a ClientOptions with given timeout.
  97. func WithTimeout(timeout time.Duration) ClientOption {
  98. return func(options *ClientOptions) {
  99. options.Timeout = timeout
  100. }
  101. }
  102. // WithUnaryClientInterceptor returns a func to customize a ClientOptions with given interceptor.
  103. func WithUnaryClientInterceptor(interceptor grpc.UnaryClientInterceptor) ClientOption {
  104. return func(options *ClientOptions) {
  105. options.DialOptions = append(options.DialOptions, WithUnaryClientInterceptors(interceptor))
  106. }
  107. }