client.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // WithTransportCredentials return a func to make the gRPC calls secured with given credentials.
  19. WithTransportCredentials = internal.WithTransportCredentials
  20. // WithUnaryClientInterceptor is an alias of internal.WithUnaryClientInterceptor.
  21. WithUnaryClientInterceptor = internal.WithUnaryClientInterceptor
  22. )
  23. type (
  24. // Client is an alias of internal.Client.
  25. Client = internal.Client
  26. // ClientOption is an alias of internal.ClientOption.
  27. ClientOption = internal.ClientOption
  28. // A RpcClient is a rpc client.
  29. RpcClient struct {
  30. client Client
  31. }
  32. )
  33. // MustNewClient returns a Client, exits on any error.
  34. func MustNewClient(c RpcClientConf, options ...ClientOption) Client {
  35. cli, err := NewClient(c, options...)
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. return cli
  40. }
  41. // NewClient returns a Client.
  42. func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
  43. var opts []ClientOption
  44. if c.HasCredential() {
  45. opts = append(opts, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
  46. App: c.App,
  47. Token: c.Token,
  48. })))
  49. }
  50. if c.NonBlock {
  51. opts = append(opts, WithNonBlock())
  52. }
  53. if c.Timeout > 0 {
  54. opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
  55. }
  56. opts = append(opts, options...)
  57. var target string
  58. var err error
  59. if len(c.Endpoints) > 0 {
  60. target = internal.BuildDirectTarget(c.Endpoints)
  61. } else if len(c.Target) > 0 {
  62. target = c.Target
  63. } else {
  64. if err = c.Etcd.Validate(); err != nil {
  65. return nil, err
  66. }
  67. if c.Etcd.HasAccount() {
  68. discov.RegisterAccount(c.Etcd.Hosts, c.Etcd.User, c.Etcd.Pass)
  69. }
  70. target = internal.BuildDiscovTarget(c.Etcd.Hosts, c.Etcd.Key)
  71. }
  72. client, err := internal.NewClient(target, opts...)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return &RpcClient{
  77. client: client,
  78. }, nil
  79. }
  80. // NewClientWithTarget returns a Client with connecting to given target.
  81. func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
  82. return internal.NewClient(target, opts...)
  83. }
  84. // Conn returns the underlying grpc.ClientConn.
  85. func (rc *RpcClient) Conn() *grpc.ClientConn {
  86. return rc.client.Conn()
  87. }
  88. // SetClientSlowThreshold sets the slow threshold on client side.
  89. func SetClientSlowThreshold(threshold time.Duration) {
  90. clientinterceptors.SetSlowThreshold(threshold)
  91. }