grpc_client.go 853 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package grpc_client
  2. import (
  3. "context"
  4. "git.sxidc.com/service-supports/ds-sdk/grpc_client/v1"
  5. "google.golang.org/grpc"
  6. "google.golang.org/grpc/credentials/insecure"
  7. )
  8. type Client struct {
  9. conn *grpc.ClientConn
  10. sqlServiceV1 v1.SqlServiceClient
  11. }
  12. func NewClient(address string) (*Client, error) {
  13. conn, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
  14. if err != nil {
  15. return nil, err
  16. }
  17. return &Client{
  18. conn: conn,
  19. sqlServiceV1: v1.NewSqlServiceClient(conn),
  20. }, nil
  21. }
  22. func Destroy(c *Client) error {
  23. if c == nil {
  24. return nil
  25. }
  26. err := c.conn.Close()
  27. if err != nil {
  28. return err
  29. }
  30. c.sqlServiceV1 = nil
  31. c.conn = nil
  32. c = nil
  33. return nil
  34. }
  35. func (c *Client) Transaction() (v1.SqlService_TransactionClient, error) {
  36. return c.sqlServiceV1.Transaction(context.Background())
  37. }