client.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package zrpc
  2. import (
  3. "log"
  4. "time"
  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. if err != nil {
  40. log.Fatal(err)
  41. }
  42. return cli
  43. }
  44. // NewClient returns a Client.
  45. func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
  46. var opts []ClientOption
  47. if c.HasCredential() {
  48. opts = append(opts, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
  49. App: c.App,
  50. Token: c.Token,
  51. })))
  52. }
  53. if c.NonBlock {
  54. opts = append(opts, WithNonBlock())
  55. }
  56. if c.Timeout > 0 {
  57. opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
  58. }
  59. if c.KeepaliveTime > 0 {
  60. opts = append(opts, WithDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
  61. Time: c.KeepaliveTime,
  62. })))
  63. }
  64. opts = append(opts, options...)
  65. target, err := c.BuildTarget()
  66. if err != nil {
  67. return nil, err
  68. }
  69. client, err := internal.NewClient(target, c.Middlewares, opts...)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return &RpcClient{
  74. client: client,
  75. }, nil
  76. }
  77. // NewClientWithTarget returns a Client with connecting to given target.
  78. func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
  79. middlewares := ClientMiddlewaresConf{
  80. Trace: true,
  81. Duration: true,
  82. Prometheus: true,
  83. Breaker: true,
  84. Timeout: true,
  85. }
  86. opts = append([]ClientOption{
  87. WithDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
  88. Time: defaultClientKeepaliveTime,
  89. })),
  90. }, opts...)
  91. return internal.NewClient(target, middlewares, opts...)
  92. }
  93. // Conn returns the underlying grpc.ClientConn.
  94. func (rc *RpcClient) Conn() *grpc.ClientConn {
  95. return rc.client.Conn()
  96. }
  97. // DontLogClientContentForMethod disable logging content for given method.
  98. func DontLogClientContentForMethod(method string) {
  99. clientinterceptors.DontLogContentForMethod(method)
  100. }
  101. // SetClientSlowThreshold sets the slow threshold on client side.
  102. func SetClientSlowThreshold(threshold time.Duration) {
  103. clientinterceptors.SetSlowThreshold(threshold)
  104. }