client.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package zrpc
  2. import (
  3. "log"
  4. "time"
  5. "github.com/tal-tech/go-zero/zrpc/internal"
  6. "github.com/tal-tech/go-zero/zrpc/internal/auth"
  7. "google.golang.org/grpc"
  8. )
  9. var (
  10. // WithDialOption is an alias of internal.WithDialOption.
  11. WithDialOption = internal.WithDialOption
  12. // WithTimeout is an alias of internal.WithTimeout.
  13. WithTimeout = internal.WithTimeout
  14. // WithRetry is an alias of internal.WithRetry.
  15. WithRetry = internal.WithRetry
  16. // WithUnaryClientInterceptor is an alias of internal.WithUnaryClientInterceptor.
  17. WithUnaryClientInterceptor = internal.WithUnaryClientInterceptor
  18. // WithInsecure is an alias of internal.WithInsecure.
  19. WithInsecure = internal.WithInsecure
  20. // WithTlsClientFromUnilateralism is an alias of internal.WithTlsClientFromUnilateralism
  21. WithTlsClientFromUnilateralism = internal.WithTlsClientFromUnilateralism
  22. // WithTlsClientFromMutual is an alias of internal.WithTlsClientFromMutual
  23. WithTlsClientFromMutual = internal.WithTlsClientFromMutual
  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.Timeout > 0 {
  53. opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
  54. }
  55. if c.Retry {
  56. opts = append(opts, WithRetry())
  57. }
  58. opts = append(opts, options...)
  59. if !c.HasSslVerify() {
  60. opts = append(opts, WithInsecure())
  61. }
  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. target = internal.BuildDiscovTarget(c.Etcd.Hosts, c.Etcd.Key)
  73. }
  74. client, err := internal.NewClient(target, opts...)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return &RpcClient{
  79. client: client,
  80. }, nil
  81. }
  82. // NewClientWithTarget returns a Client with connecting to given target.
  83. func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
  84. return internal.NewClient(target, opts...)
  85. }
  86. // Conn returns the underlying grpc.ClientConn.
  87. func (rc *RpcClient) Conn() *grpc.ClientConn {
  88. return rc.client.Conn()
  89. }