client.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "google.golang.org/grpc"
  9. )
  10. var (
  11. // WithDialOption is an alias of internal.WithDialOption.
  12. WithDialOption = internal.WithDialOption
  13. // WithTimeout is an alias of internal.WithTimeout.
  14. WithTimeout = internal.WithTimeout
  15. // WithRetry is an alias of internal.WithRetry.
  16. WithRetry = internal.WithRetry
  17. // WithUnaryClientInterceptor is an alias of internal.WithUnaryClientInterceptor.
  18. WithUnaryClientInterceptor = internal.WithUnaryClientInterceptor
  19. // WithTlsClientFromUnilateral is an alias of internal.WithTlsClientFromUnilateral
  20. WithTlsClientFromUnilateral = internal.WithTlsClientFromUnilateral
  21. // WithTlsClientFromMutual is an alias of internal.WithTlsClientFromMutual
  22. WithTlsClientFromMutual = internal.WithTlsClientFromMutual
  23. )
  24. type (
  25. // Client is an alias of internal.Client.
  26. Client = internal.Client
  27. // ClientOption is an alias of internal.ClientOption.
  28. ClientOption = internal.ClientOption
  29. // A RpcClient is a rpc client.
  30. RpcClient struct {
  31. client Client
  32. }
  33. )
  34. // MustNewClient returns a Client, exits on any error.
  35. func MustNewClient(c RpcClientConf, options ...ClientOption) Client {
  36. cli, err := NewClient(c, options...)
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  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.Timeout > 0 {
  52. opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
  53. }
  54. if c.Retry {
  55. opts = append(opts, WithRetry())
  56. }
  57. opts = append(opts, options...)
  58. var target string
  59. var err error
  60. if len(c.Endpoints) > 0 {
  61. target = internal.BuildDirectTarget(c.Endpoints)
  62. } else if len(c.Target) > 0 {
  63. target = c.Target
  64. } else {
  65. if err = c.Etcd.Validate(); err != nil {
  66. return nil, err
  67. }
  68. if c.Etcd.HasAccount() {
  69. discov.RegisterAccount(c.Etcd.Hosts, c.Etcd.User, c.Etcd.Pass)
  70. }
  71. target = internal.BuildDiscovTarget(c.Etcd.Hosts, c.Etcd.Key)
  72. }
  73. client, err := internal.NewClient(target, opts...)
  74. if err != nil {
  75. return nil, err
  76. }
  77. return &RpcClient{
  78. client: client,
  79. }, nil
  80. }
  81. // NewClientWithTarget returns a Client with connecting to given target.
  82. func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
  83. return internal.NewClient(target, opts...)
  84. }
  85. // Conn returns the underlying grpc.ClientConn.
  86. func (rc *RpcClient) Conn() *grpc.ClientConn {
  87. return rc.client.Conn()
  88. }