client.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package zrpc
  2. import (
  3. "time"
  4. "github.com/zeromicro/go-zero/core/logx"
  5. "github.com/zeromicro/go-zero/zrpc/internal"
  6. "github.com/zeromicro/go-zero/zrpc/internal/auth"
  7. "github.com/zeromicro/go-zero/zrpc/internal/clientinterceptors"
  8. "google.golang.org/grpc"
  9. "google.golang.org/grpc/keepalive"
  10. )
  11. const defaultClientKeepaliveTime = 20 * time.Second
  12. var (
  13. // WithDialOption is an alias of internal.WithDialOption.
  14. WithDialOption = internal.WithDialOption
  15. // WithNonBlock sets the dialing to be nonblock.
  16. WithNonBlock = internal.WithNonBlock
  17. // WithStreamClientInterceptor is an alias of internal.WithStreamClientInterceptor.
  18. WithStreamClientInterceptor = internal.WithStreamClientInterceptor
  19. // WithTimeout is an alias of internal.WithTimeout.
  20. WithTimeout = internal.WithTimeout
  21. // WithTransportCredentials return a func to make the gRPC calls secured with given credentials.
  22. WithTransportCredentials = internal.WithTransportCredentials
  23. // WithUnaryClientInterceptor is an alias of internal.WithUnaryClientInterceptor.
  24. WithUnaryClientInterceptor = internal.WithUnaryClientInterceptor
  25. )
  26. type (
  27. // Client is an alias of internal.Client.
  28. Client = internal.Client
  29. // ClientOption is an alias of internal.ClientOption.
  30. ClientOption = internal.ClientOption
  31. // A RpcClient is a rpc client.
  32. RpcClient struct {
  33. client Client
  34. }
  35. )
  36. // MustNewClient returns a Client, exits on any error.
  37. func MustNewClient(c RpcClientConf, options ...ClientOption) Client {
  38. cli, err := NewClient(c, options...)
  39. logx.Must(err)
  40. return cli
  41. }
  42. // NewClient returns a Client.
  43. func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
  44. var opts []ClientOption
  45. if c.HasCredential() {
  46. opts = append(opts, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
  47. App: c.App,
  48. Token: c.Token,
  49. })))
  50. }
  51. if c.NonBlock {
  52. opts = append(opts, WithNonBlock())
  53. }
  54. if c.Timeout > 0 {
  55. opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
  56. }
  57. if c.KeepaliveTime > 0 {
  58. opts = append(opts, WithDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
  59. Time: c.KeepaliveTime,
  60. })))
  61. }
  62. opts = append(opts, options...)
  63. target, err := c.BuildTarget()
  64. if err != nil {
  65. return nil, err
  66. }
  67. client, err := internal.NewClient(target, c.Middlewares, opts...)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return &RpcClient{
  72. client: client,
  73. }, nil
  74. }
  75. // NewClientWithTarget returns a Client with connecting to given target.
  76. func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
  77. middlewares := ClientMiddlewaresConf{
  78. Trace: true,
  79. Duration: true,
  80. Prometheus: true,
  81. Breaker: true,
  82. Timeout: true,
  83. }
  84. opts = append([]ClientOption{
  85. WithDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
  86. Time: defaultClientKeepaliveTime,
  87. })),
  88. }, opts...)
  89. return internal.NewClient(target, middlewares, opts...)
  90. }
  91. // Conn returns the underlying grpc.ClientConn.
  92. func (rc *RpcClient) Conn() *grpc.ClientConn {
  93. return rc.client.Conn()
  94. }
  95. // DontLogClientContentForMethod disable logging content for given method.
  96. func DontLogClientContentForMethod(method string) {
  97. clientinterceptors.DontLogContentForMethod(method)
  98. }
  99. // SetClientSlowThreshold sets the slow threshold on client side.
  100. func SetClientSlowThreshold(threshold time.Duration) {
  101. clientinterceptors.SetSlowThreshold(threshold)
  102. }