1
0

client.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package zrpc
  2. import (
  3. "time"
  4. "github.com/wuntsong-org/go-zero-plus/core/conf"
  5. "github.com/wuntsong-org/go-zero-plus/core/logx"
  6. "github.com/wuntsong-org/go-zero-plus/zrpc/internal"
  7. "github.com/wuntsong-org/go-zero-plus/zrpc/internal/auth"
  8. "github.com/wuntsong-org/go-zero-plus/zrpc/internal/clientinterceptors"
  9. "google.golang.org/grpc"
  10. "google.golang.org/grpc/keepalive"
  11. )
  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. var config RpcClientConf
  78. if err := conf.FillDefault(&config); err != nil {
  79. return nil, err
  80. }
  81. config.Target = target
  82. return NewClient(config, opts...)
  83. }
  84. // Conn returns the underlying grpc.ClientConn.
  85. func (rc *RpcClient) Conn() *grpc.ClientConn {
  86. return rc.client.Conn()
  87. }
  88. // DontLogClientContentForMethod disable logging content for given method.
  89. func DontLogClientContentForMethod(method string) {
  90. clientinterceptors.DontLogContentForMethod(method)
  91. }
  92. // SetClientSlowThreshold sets the slow threshold on client side.
  93. func SetClientSlowThreshold(threshold time.Duration) {
  94. clientinterceptors.SetSlowThreshold(threshold)
  95. }
  96. // WithCallTimeout return a call option with given timeout to make a method call.
  97. func WithCallTimeout(timeout time.Duration) grpc.CallOption {
  98. return clientinterceptors.WithCallTimeout(timeout)
  99. }