client.go 3.1 KB

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