This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
iox_client.go
60 lines (51 loc) · 1.64 KB
/
iox_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package influxdbiox
import (
"context"
"github.com/apache/arrow/go/v10/arrow/flight"
ingester "github.com/influxdata/influxdb-iox-client-go/v2/internal/ingester"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
)
// Client is the primary handle to interact with InfluxDB/IOx.
type Client struct {
config *ClientConfig
grpcClient *grpc.ClientConn
flightClient flight.FlightServiceClient
ingesterWriteInfoClient ingester.WriteInfoServiceClient
}
// NewClient instantiates a connection with the InfluxDB/IOx gRPC services.
//
// The gRPC client does not establish a connection here, unless
// ClientConfig.DialOptions includes grpc.WithBlock.
// For use of the context.Context object in this function, see grpc.DialContext.
func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
c := &Client{
config: config,
}
if err := c.Reconnect(ctx); err != nil {
return nil, err
}
return c, nil
}
// Reconnect closes the gRPC connection, if open, and creates a new connection.
func (c *Client) Reconnect(ctx context.Context) error {
if c.grpcClient != nil {
_ = c.grpcClient.Close()
}
grpcClient, err := c.config.newGRPCClient(ctx)
if err != nil {
return err
}
c.grpcClient = grpcClient
c.flightClient = flight.NewFlightServiceClient(grpcClient)
c.ingesterWriteInfoClient = ingester.NewWriteInfoServiceClient(grpcClient)
return nil
}
// GetState gets the state of the wrapped gRPC client.
func (c *Client) GetState() connectivity.State {
return c.grpcClient.GetState()
}
// Close closes the instance of Client.
func (c *Client) Close() error {
return c.grpcClient.Close()
}