client.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // WithTlsClientFromUnilateral is an alias of internal.WithTlsClientFromUnilateral
  19. WithTlsClientFromUnilateral = internal.WithTlsClientFromUnilateral
  20. // WithTlsClientFromMutual is an alias of internal.WithTlsClientFromMutual
  21. WithTlsClientFromMutual = internal.WithTlsClientFromMutual
  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.Timeout > 0 {
  51. opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
  52. }
  53. if c.Retry {
  54. opts = append(opts, WithRetry())
  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. target = internal.BuildDiscovTarget(c.Etcd.Hosts, c.Etcd.Key)
  68. }
  69. client, err := internal.NewClient(target, opts...)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return &RpcClient{
  74. client: client,
  75. }, nil
  76. }
  77. // NewClientWithTarget returns a Client with connecting to given target.
  78. func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
  79. return internal.NewClient(target, opts...)
  80. }
  81. // Conn returns the underlying grpc.ClientConn.
  82. func (rc *RpcClient) Conn() *grpc.ClientConn {
  83. return rc.client.Conn()
  84. }