client.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. ClientOptions struct {
  22. Timeout time.Duration
  23. DialOptions []grpc.DialOption
  24. }
  25. ClientOption func(options *ClientOptions)
  26. client struct {
  27. conn *grpc.ClientConn
  28. interceptors []grpc.UnaryClientInterceptor
  29. }
  30. )
  31. func NewClient(target string, opts ...ClientOption) (*client, error) {
  32. var cli client
  33. opts = append(opts, WithDialOption(grpc.WithBalancerName(p2c.Name)))
  34. if err := cli.dial(target, opts...); err != nil {
  35. return nil, err
  36. }
  37. return &cli, nil
  38. }
  39. func (c *client) AddInterceptor(interceptor grpc.UnaryClientInterceptor) {
  40. c.interceptors = append(c.interceptors, interceptor)
  41. }
  42. func (c *client) Conn() *grpc.ClientConn {
  43. return c.conn
  44. }
  45. func (c *client) buildDialOptions(opts ...ClientOption) []grpc.DialOption {
  46. var clientOptions ClientOptions
  47. for _, opt := range opts {
  48. opt(&clientOptions)
  49. }
  50. options := []grpc.DialOption{
  51. grpc.WithInsecure(),
  52. grpc.WithBlock(),
  53. WithUnaryClientInterceptors(
  54. clientinterceptors.TracingInterceptor,
  55. clientinterceptors.DurationInterceptor,
  56. clientinterceptors.BreakerInterceptor,
  57. clientinterceptors.PrometheusInterceptor,
  58. clientinterceptors.TimeoutInterceptor(clientOptions.Timeout),
  59. ),
  60. }
  61. for _, interceptor := range c.interceptors {
  62. options = append(options, WithUnaryClientInterceptors(interceptor))
  63. }
  64. return append(options, clientOptions.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. func WithDialOption(opt grpc.DialOption) ClientOption {
  87. return func(options *ClientOptions) {
  88. options.DialOptions = append(options.DialOptions, opt)
  89. }
  90. }
  91. func WithTimeout(timeout time.Duration) ClientOption {
  92. return func(options *ClientOptions) {
  93. options.Timeout = timeout
  94. }
  95. }