client.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package zrpc
  2. import (
  3. "log"
  4. "time"
  5. "github.com/zeromicro/go-zero/zrpc/internal"
  6. "github.com/zeromicro/go-zero/zrpc/internal/auth"
  7. "github.com/zeromicro/go-zero/zrpc/internal/clientinterceptors"
  8. "google.golang.org/grpc"
  9. )
  10. var (
  11. // WithDialOption is an alias of internal.WithDialOption.
  12. WithDialOption = internal.WithDialOption
  13. // WithNonBlock sets the dialing to be nonblock.
  14. WithNonBlock = internal.WithNonBlock
  15. // WithStreamClientInterceptor is an alias of internal.WithStreamClientInterceptor.
  16. WithStreamClientInterceptor = internal.WithStreamClientInterceptor
  17. // WithTimeout is an alias of internal.WithTimeout.
  18. WithTimeout = internal.WithTimeout
  19. // WithTransportCredentials return a func to make the gRPC calls secured with given credentials.
  20. WithTransportCredentials = internal.WithTransportCredentials
  21. // WithUnaryClientInterceptor is an alias of internal.WithUnaryClientInterceptor.
  22. WithUnaryClientInterceptor = internal.WithUnaryClientInterceptor
  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.NonBlock {
  52. opts = append(opts, WithNonBlock())
  53. }
  54. if c.Timeout > 0 {
  55. opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
  56. }
  57. opts = append(opts, options...)
  58. target, err := c.BuildTarget()
  59. if err != nil {
  60. return nil, err
  61. }
  62. client, err := internal.NewClient(target, opts...)
  63. if err != nil {
  64. return nil, err
  65. }
  66. return &RpcClient{
  67. client: client,
  68. }, nil
  69. }
  70. // NewClientWithTarget returns a Client with connecting to given target.
  71. func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
  72. return internal.NewClient(target, opts...)
  73. }
  74. // Conn returns the underlying grpc.ClientConn.
  75. func (rc *RpcClient) Conn() *grpc.ClientConn {
  76. return rc.client.Conn()
  77. }
  78. // SetClientSlowThreshold sets the slow threshold on client side.
  79. func SetClientSlowThreshold(threshold time.Duration) {
  80. clientinterceptors.SetSlowThreshold(threshold)
  81. }