client.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package zrpc
  2. import (
  3. "log"
  4. "time"
  5. "git.i2edu.net/i2/go-zero/core/discov"
  6. "git.i2edu.net/i2/go-zero/zrpc/internal"
  7. "git.i2edu.net/i2/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. // WithUnaryClientInterceptor is an alias of internal.WithUnaryClientInterceptor.
  16. WithUnaryClientInterceptor = internal.WithUnaryClientInterceptor
  17. )
  18. type (
  19. // Client is an alias of internal.Client.
  20. Client = internal.Client
  21. // ClientOption is an alias of internal.ClientOption.
  22. ClientOption = internal.ClientOption
  23. // A RpcClient is a rpc client.
  24. RpcClient struct {
  25. client Client
  26. }
  27. )
  28. // MustNewClient returns a Client, exits on any error.
  29. func MustNewClient(c RpcClientConf, options ...ClientOption) Client {
  30. cli, err := NewClient(c, options...)
  31. if err != nil {
  32. log.Fatal(err)
  33. }
  34. return cli
  35. }
  36. // NewClient returns a Client.
  37. func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
  38. var opts []ClientOption
  39. if c.HasCredential() {
  40. opts = append(opts, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
  41. App: c.App,
  42. Token: c.Token,
  43. })))
  44. }
  45. if c.Timeout > 0 {
  46. opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
  47. }
  48. opts = append(opts, options...)
  49. var client Client
  50. var err error
  51. if len(c.Endpoints) > 0 {
  52. client, err = internal.NewClient(internal.BuildDirectTarget(c.Endpoints), opts...)
  53. } else if err = c.Etcd.Validate(); err == nil {
  54. client, err = internal.NewClient(internal.BuildDiscovTarget(c.Etcd.Hosts, c.Etcd.Key), opts...)
  55. }
  56. if err != nil {
  57. return nil, err
  58. }
  59. return &RpcClient{
  60. client: client,
  61. }, nil
  62. }
  63. // NewClientNoAuth returns a Client without authentication.
  64. func NewClientNoAuth(c discov.EtcdConf, opts ...ClientOption) (Client, error) {
  65. client, err := internal.NewClient(internal.BuildDiscovTarget(c.Hosts, c.Key), opts...)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return &RpcClient{
  70. client: client,
  71. }, nil
  72. }
  73. // NewClientWithTarget returns a Client with connecting to given target.
  74. func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
  75. return internal.NewClient(target, opts...)
  76. }
  77. // Conn returns the underlying grpc.ClientConn.
  78. func (rc *RpcClient) Conn() *grpc.ClientConn {
  79. return rc.client.Conn()
  80. }