|
| 1 | +// Package grpcclient is grpc client of Xephon-K, it's in separated package to avoid import grpc |
| 2 | +// for clients that don't use grpc |
| 3 | +package grpcclient |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "net/url" |
| 8 | + |
| 9 | + "github.com/dyweb/gommon/errors" |
| 10 | + "google.golang.org/grpc" |
| 11 | + |
| 12 | + "github.com/libtsdb/libtsdb-go/libtsdb" |
| 13 | + pb "github.com/libtsdb/libtsdb-go/libtsdb/libtsdbpb" |
| 14 | + |
| 15 | + xkc "github.com/xephonhq/xephon-k/xk/client" |
| 16 | + "github.com/xephonhq/xephon-k/xk/config" |
| 17 | + rpc "github.com/xephonhq/xephon-k/xk/transport/grpc" |
| 18 | +) |
| 19 | + |
| 20 | +var _ libtsdb.WriteClient = (*Client)(nil) |
| 21 | + |
| 22 | +// TODO: support prepare and columnar format |
| 23 | +type Client struct { |
| 24 | + cfg config.ClientConfig |
| 25 | + client rpc.XephonkClient |
| 26 | + conn *grpc.ClientConn |
| 27 | + |
| 28 | + pointsInt []pb.PointIntTagged |
| 29 | + pointsDouble []pb.PointDoubleTagged |
| 30 | + seriesInt []pb.SeriesIntTagged |
| 31 | + seriesDouble []pb.SeriesDoubleTagged |
| 32 | +} |
| 33 | + |
| 34 | +func New(cfg config.ClientConfig) (*Client, error) { |
| 35 | + _, err := url.Parse(cfg.Addr) |
| 36 | + if err != nil { |
| 37 | + return nil, errors.Wrap(err, "can't parse server address") |
| 38 | + } |
| 39 | + conn, err := grpc.Dial(cfg.Addr, grpc.WithInsecure()) |
| 40 | + if err != nil { |
| 41 | + return nil, errors.Wrapf(err, "grpc dial failed %s", cfg.Addr) |
| 42 | + } |
| 43 | + client := rpc.NewClient(conn) |
| 44 | + return &Client{ |
| 45 | + cfg: cfg, |
| 46 | + client: client, |
| 47 | + conn: conn, |
| 48 | + }, nil |
| 49 | +} |
| 50 | + |
| 51 | +func (c *Client) Meta() libtsdb.Meta { |
| 52 | + return xkc.Meta() |
| 53 | +} |
| 54 | + |
| 55 | +func (c *Client) Close() error { |
| 56 | + if err := c.conn.Close(); err != nil { |
| 57 | + return errors.Wrap(err, "can't close grpc client connection") |
| 58 | + } |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func (c *Client) WriteIntPoint(p *pb.PointIntTagged) { |
| 63 | + // TODO: deal with prepare and columnar |
| 64 | + c.pointsInt = append(c.pointsInt, *p) |
| 65 | +} |
| 66 | + |
| 67 | +func (c *Client) WriteDoublePoint(p *pb.PointDoubleTagged) { |
| 68 | + // TODO: deal with prepare and columnar |
| 69 | + c.pointsDouble = append(c.pointsDouble, *p) |
| 70 | +} |
| 71 | + |
| 72 | +func (c *Client) WriteSeriesIntTagged(p *pb.SeriesIntTagged) { |
| 73 | + // TODO: deal with prepare and columnar |
| 74 | + c.seriesInt = append(c.seriesInt, *p) |
| 75 | +} |
| 76 | + |
| 77 | +func (c *Client) WriteSeriesDoubleTagged(p *pb.SeriesDoubleTagged) { |
| 78 | + // TODO: deal with prepare and columnar |
| 79 | + c.seriesDouble = append(c.seriesDouble, *p) |
| 80 | +} |
| 81 | + |
| 82 | +func (c *Client) Flush() error { |
| 83 | + return c.send() |
| 84 | +} |
| 85 | + |
| 86 | +func (c *Client) send() error { |
| 87 | + merr := errors.NewMultiErr() |
| 88 | + // NOTE: normally we assume user only use one methods, so we just use one go routine |
| 89 | + if len(c.pointsInt) != 0 || len(c.pointsDouble) != 0 { |
| 90 | + req := rpc.WritePointsReq{ |
| 91 | + Int: c.pointsInt, |
| 92 | + Double: c.pointsDouble, |
| 93 | + } |
| 94 | + _, err := c.client.WritePoints(context.Background(), &req) |
| 95 | + if err != nil { |
| 96 | + merr.Append(err) |
| 97 | + } |
| 98 | + c.pointsInt = c.pointsInt[:0] |
| 99 | + c.pointsDouble = c.pointsDouble[:0] |
| 100 | + } |
| 101 | + if len(c.seriesInt) != 0 || len(c.seriesDouble) != 0 { |
| 102 | + req := rpc.WriteSeriesReq{ |
| 103 | + Int: c.seriesInt, |
| 104 | + Double: c.seriesDouble, |
| 105 | + } |
| 106 | + _, err := c.client.WriteSeries(context.Background(), &req) |
| 107 | + if err != nil { |
| 108 | + merr.Append(err) |
| 109 | + } |
| 110 | + c.seriesInt = c.seriesInt[:0] |
| 111 | + c.seriesDouble = c.seriesDouble[:0] |
| 112 | + } |
| 113 | + return merr.ErrorOrNil() |
| 114 | +} |
0 commit comments