client.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package zrpc
  2. import (
  3. "log"
  4. "time"
  5. "github.com/tal-tech/go-zero/core/discov"
  6. "github.com/tal-tech/go-zero/zrpc/internal"
  7. "github.com/tal-tech/go-zero/zrpc/internal/auth"
  8. "github.com/tal-tech/go-zero/zrpc/internal/clientinterceptors"
  9. "google.golang.org/grpc"
  10. )
  11. var (
  12. // WithDialOption is an alias of internal.WithDialOption.
  13. WithDialOption = internal.WithDialOption
  14. // WithNonBlock sets the dialing to be nonblock.
  15. WithNonBlock = internal.WithNonBlock
  16. // WithTimeout is an alias of internal.WithTimeout.
  17. WithTimeout = internal.WithTimeout
  18. // WithRetry is an alias of internal.WithRetry.
  19. WithRetry = internal.WithRetry
  20. // WithTransportCredentials return a func to make the gRPC calls secured with given credentials.
  21. WithTransportCredentials = internal.WithTransportCredentials
  22. // WithUnaryClientInterceptor is an alias of internal.WithUnaryClientInterceptor.
  23. WithUnaryClientInterceptor = internal.WithUnaryClientInterceptor
  24. )
  25. type (
  26. // Client is an alias of internal.Client.
  27. Client = internal.Client
  28. // ClientOption is an alias of internal.ClientOption.
  29. ClientOption = internal.ClientOption
  30. // A RpcClient is a rpc client.
  31. RpcClient struct {
  32. client Client
  33. }
  34. )
  35. // MustNewClient returns a Client, exits on any error.
  36. func MustNewClient(c RpcClientConf, options ...ClientOption) Client {
  37. cli, err := NewClient(c, options...)
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. return cli
  42. }
  43. // NewClient returns a Client.
  44. func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
  45. var opts []ClientOption
  46. if c.HasCredential() {
  47. opts = append(opts, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
  48. App: c.App,
  49. Token: c.Token,
  50. })))
  51. }
  52. if c.NonBlock {
  53. opts = append(opts, WithNonBlock())
  54. }
  55. if c.Timeout > 0 {
  56. opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
  57. }
  58. if c.Retry {
  59. opts = append(opts, WithRetry())
  60. }
  61. opts = append(opts, options...)
  62. var target string
  63. var err error
  64. if len(c.Endpoints) > 0 {
  65. target = internal.BuildDirectTarget(c.Endpoints)
  66. } else if len(c.Target) > 0 {
  67. target = c.Target
  68. } else {
  69. if err = c.Etcd.Validate(); err != nil {
  70. return nil, err
  71. }
  72. if c.Etcd.HasAccount() {
  73. discov.RegisterAccount(c.Etcd.Hosts, c.Etcd.User, c.Etcd.Pass)
  74. }
  75. target = internal.BuildDiscovTarget(c.Etcd.Hosts, c.Etcd.Key)
  76. }
  77. client, err := internal.NewClient(target, opts...)
  78. if err != nil {
  79. return nil, err
  80. }
  81. return &RpcClient{
  82. client: client,
  83. }, nil
  84. }
  85. // NewClientWithTarget returns a Client with connecting to given target.
  86. func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
  87. return internal.NewClient(target, opts...)
  88. }
  89. // Conn returns the underlying grpc.ClientConn.
  90. func (rc *RpcClient) Conn() *grpc.ClientConn {
  91. return rc.client.Conn()
  92. }
  93. // SetClientSlowThreshold sets the slow threshold on client side.
  94. func SetClientSlowThreshold(threshold time.Duration) {
  95. clientinterceptors.SetSlowThreshold(threshold)
  96. }