client.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "github.com/tal-tech/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. // WithTimeout is an alias of internal.WithTimeout.
  16. WithTimeout = internal.WithTimeout
  17. // WithTransportCredentials return a func to make the gRPC calls secured with given credentials.
  18. WithTransportCredentials = internal.WithTransportCredentials
  19. // WithUnaryClientInterceptor is an alias of internal.WithUnaryClientInterceptor.
  20. WithUnaryClientInterceptor = internal.WithUnaryClientInterceptor
  21. )
  22. type (
  23. // Client is an alias of internal.Client.
  24. Client = internal.Client
  25. // ClientOption is an alias of internal.ClientOption.
  26. ClientOption = internal.ClientOption
  27. // A RpcClient is a rpc client.
  28. RpcClient struct {
  29. client Client
  30. }
  31. )
  32. // MustNewClient returns a Client, exits on any error.
  33. func MustNewClient(c RpcClientConf, options ...ClientOption) Client {
  34. cli, err := NewClient(c, options...)
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. return cli
  39. }
  40. // NewClient returns a Client.
  41. func NewClient(c RpcClientConf, options ...ClientOption) (Client, error) {
  42. var opts []ClientOption
  43. if c.HasCredential() {
  44. opts = append(opts, WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{
  45. App: c.App,
  46. Token: c.Token,
  47. })))
  48. }
  49. if c.NonBlock {
  50. opts = append(opts, WithNonBlock())
  51. }
  52. if c.Timeout > 0 {
  53. opts = append(opts, WithTimeout(time.Duration(c.Timeout)*time.Millisecond))
  54. }
  55. opts = append(opts, options...)
  56. target, err := c.BuildTarget()
  57. if err != nil {
  58. return nil, err
  59. }
  60. client, err := internal.NewClient(target, opts...)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return &RpcClient{
  65. client: client,
  66. }, nil
  67. }
  68. // NewClientWithTarget returns a Client with connecting to given target.
  69. func NewClientWithTarget(target string, opts ...ClientOption) (Client, error) {
  70. return internal.NewClient(target, opts...)
  71. }
  72. // Conn returns the underlying grpc.ClientConn.
  73. func (rc *RpcClient) Conn() *grpc.ClientConn {
  74. return rc.client.Conn()
  75. }
  76. // SetClientSlowThreshold sets the slow threshold on client side.
  77. func SetClientSlowThreshold(threshold time.Duration) {
  78. clientinterceptors.SetSlowThreshold(threshold)
  79. }