123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package grpc_client
- import (
- "context"
- "git.sxidc.com/service-supports/ds-sdk/grpc_client/v1"
- "google.golang.org/grpc"
- "google.golang.org/grpc/credentials/insecure"
- "time"
- )
- type Client struct {
- conn *grpc.ClientConn
- sqlServiceV1 v1.SqlServiceClient
- timeout time.Duration
- }
- func NewClient(address string, timeout time.Duration) (*Client, error) {
- conn, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
- if err != nil {
- return nil, err
- }
- return &Client{
- conn: conn,
- sqlServiceV1: v1.NewSqlServiceClient(conn),
- timeout: timeout,
- }, nil
- }
- func Destroy(c *Client) error {
- if c == nil {
- return nil
- }
- err := c.conn.Close()
- if err != nil {
- return err
- }
- c.sqlServiceV1 = nil
- c.conn = nil
- c = nil
- return nil
- }
- func (c *Client) Transaction() (v1.SqlService_TransactionClient, error) {
- var ctx context.Context
- if c.timeout == 0 {
- ctx = context.Background()
- } else {
- timeoutCtx, cancel := context.WithTimeout(context.Background(), c.timeout)
- defer cancel()
- ctx = timeoutCtx
- }
- return c.sqlServiceV1.Transaction(ctx)
- }
|