client.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }
  29. )
  30. func NewClient(target string, opts ...ClientOption) (*client, error) {
  31. var cli client
  32. opts = append([]ClientOption{WithDialOption(grpc.WithBalancerName(p2c.Name))}, opts...)
  33. if err := cli.dial(target, opts...); err != nil {
  34. return nil, err
  35. }
  36. return &cli, nil
  37. }
  38. func (c *client) Conn() *grpc.ClientConn {
  39. return c.conn
  40. }
  41. func (c *client) buildDialOptions(opts ...ClientOption) []grpc.DialOption {
  42. var cliOpts ClientOptions
  43. for _, opt := range opts {
  44. opt(&cliOpts)
  45. }
  46. options := []grpc.DialOption{
  47. grpc.WithInsecure(),
  48. grpc.WithBlock(),
  49. WithUnaryClientInterceptors(
  50. clientinterceptors.TracingInterceptor,
  51. clientinterceptors.DurationInterceptor,
  52. clientinterceptors.BreakerInterceptor,
  53. clientinterceptors.PrometheusInterceptor,
  54. clientinterceptors.TimeoutInterceptor(cliOpts.Timeout),
  55. ),
  56. }
  57. return append(options, cliOpts.DialOptions...)
  58. }
  59. func (c *client) dial(server string, opts ...ClientOption) error {
  60. options := c.buildDialOptions(opts...)
  61. timeCtx, cancel := context.WithTimeout(context.Background(), dialTimeout)
  62. defer cancel()
  63. conn, err := grpc.DialContext(timeCtx, server, options...)
  64. if err != nil {
  65. service := server
  66. if errors.Is(err, context.DeadlineExceeded) {
  67. pos := strings.LastIndexByte(server, separator)
  68. // len(server) - 1 is the index of last char
  69. if 0 < pos && pos < len(server)-1 {
  70. service = server[pos+1:]
  71. }
  72. }
  73. return fmt.Errorf("rpc dial: %s, error: %s, make sure rpc service %q is alread started",
  74. server, err.Error(), service)
  75. }
  76. c.conn = conn
  77. return nil
  78. }
  79. func WithDialOption(opt grpc.DialOption) ClientOption {
  80. return func(options *ClientOptions) {
  81. options.DialOptions = append(options.DialOptions, opt)
  82. }
  83. }
  84. func WithTimeout(timeout time.Duration) ClientOption {
  85. return func(options *ClientOptions) {
  86. options.Timeout = timeout
  87. }
  88. }
  89. func WithUnaryClientInterceptor(interceptor grpc.UnaryClientInterceptor) ClientOption {
  90. return func(options *ClientOptions) {
  91. options.DialOptions = append(options.DialOptions, WithUnaryClientInterceptors(interceptor))
  92. }
  93. }