client.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.UnaryTracingInterceptor,
  58. clientinterceptors.DurationInterceptor,
  59. clientinterceptors.PrometheusInterceptor,
  60. clientinterceptors.BreakerInterceptor,
  61. clientinterceptors.TimeoutInterceptor(cliOpts.Timeout),
  62. ),
  63. WithStreamClientInterceptors(
  64. clientinterceptors.StreamTracingInterceptor,
  65. ),
  66. }
  67. return append(options, cliOpts.DialOptions...)
  68. }
  69. func (c *client) dial(server string, opts ...ClientOption) error {
  70. options := c.buildDialOptions(opts...)
  71. timeCtx, cancel := context.WithTimeout(context.Background(), dialTimeout)
  72. defer cancel()
  73. conn, err := grpc.DialContext(timeCtx, server, options...)
  74. if err != nil {
  75. service := server
  76. if errors.Is(err, context.DeadlineExceeded) {
  77. pos := strings.LastIndexByte(server, separator)
  78. // len(server) - 1 is the index of last char
  79. if 0 < pos && pos < len(server)-1 {
  80. service = server[pos+1:]
  81. }
  82. }
  83. return fmt.Errorf("rpc dial: %s, error: %s, make sure rpc service %q is already started",
  84. server, err.Error(), service)
  85. }
  86. c.conn = conn
  87. return nil
  88. }
  89. // WithDialOption returns a func to customize a ClientOptions with given dial option.
  90. func WithDialOption(opt grpc.DialOption) ClientOption {
  91. return func(options *ClientOptions) {
  92. options.DialOptions = append(options.DialOptions, opt)
  93. }
  94. }
  95. // WithTimeout returns a func to customize a ClientOptions with given timeout.
  96. func WithTimeout(timeout time.Duration) ClientOption {
  97. return func(options *ClientOptions) {
  98. options.Timeout = timeout
  99. }
  100. }
  101. // WithUnaryClientInterceptor returns a func to customize a ClientOptions with given interceptor.
  102. func WithUnaryClientInterceptor(interceptor grpc.UnaryClientInterceptor) ClientOption {
  103. return func(options *ClientOptions) {
  104. options.DialOptions = append(options.DialOptions, WithUnaryClientInterceptors(interceptor))
  105. }
  106. }