client.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // TODO: enable it in v1.2.4
  20. // WithRetry = internal.WithRetry
  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. // TODO: enable it in v1.2.4
  60. // if c.Retry {
  61. // opts = append(opts, WithRetry())
  62. // }
  63. opts = append(opts, options...)
  64. var target string
  65. var err error
  66. if len(c.Endpoints) > 0 {
  67. target = internal.BuildDirectTarget(c.Endpoints)
  68. } else if len(c.Target) > 0 {
  69. target = c.Target
  70. } else {
  71. if err = c.Etcd.Validate(); err != nil {
  72. return nil, err
  73. }
  74. if c.Etcd.HasAccount() {
  75. discov.RegisterAccount(c.Etcd.Hosts, c.Etcd.User, c.Etcd.Pass)
  76. }
  77. target = internal.BuildDiscovTarget(c.Etcd.Hosts, c.Etcd.Key)
  78. }
  79. client, err := internal.NewClient(target, opts...)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return &RpcClient{
  84. client: client,
  85. }, nil
  86. }
  87. // NewClientWithTarget returns a Client with connecting to given target.
  88. func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
  89. return internal.NewClient(target, opts...)
  90. }
  91. // Conn returns the underlying grpc.ClientConn.
  92. func (rc *RpcClient) Conn() *grpc.ClientConn {
  93. return rc.client.Conn()
  94. }
  95. // SetClientSlowThreshold sets the slow threshold on client side.
  96. func SetClientSlowThreshold(threshold time.Duration) {
  97. clientinterceptors.SetSlowThreshold(threshold)
  98. }