forked from influxdata/influxdb-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeApiBlocking.go
64 lines (56 loc) · 2.05 KB
/
writeApiBlocking.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
61
62
63
64
// Copyright 2020 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
package influxdb2
import (
"context"
"github.com/influxdata/influxdb-client-go/internal/http"
"strings"
)
// WriteApiBlocking offers blocking methods for writing time series data synchronously into an InfluxDB server.
type WriteApiBlocking interface {
// WriteRecord writes line protocol record(s) into bucket.
// WriteRecord writes without implicit batching. Batch is created from given number of records
// Non-blocking alternative is available in the WriteApi interface
WriteRecord(ctx context.Context, line ...string) error
// WritePoint data point into bucket.
// WritePoint writes without implicit batching. Batch is created from given number of points
// Non-blocking alternative is available in the WriteApi interface
WritePoint(ctx context.Context, point ...*Point) error
}
// writeApiBlockingImpl implements WriteApiBlocking interface
type writeApiBlockingImpl struct {
service writeService
}
// creates writeApiBlockingImpl for org and bucket with underlying client
func newWriteApiBlockingImpl(org string, bucket string, service http.Service, client Client) *writeApiBlockingImpl {
return &writeApiBlockingImpl{service: newWriteService(org, bucket, service, client)}
}
func (w *writeApiBlockingImpl) write(ctx context.Context, line string) error {
err := w.service.handleWrite(ctx, &batch{
batch: line,
retryInterval: w.service.Options().RetryInterval(),
})
return err
}
func (w *writeApiBlockingImpl) WriteRecord(ctx context.Context, line ...string) error {
if len(line) > 0 {
var sb strings.Builder
for _, line := range line {
b := []byte(line)
b = append(b, 0xa)
if _, err := sb.Write(b); err != nil {
return err
}
}
return w.write(ctx, sb.String())
}
return nil
}
func (w *writeApiBlockingImpl) WritePoint(ctx context.Context, point ...*Point) error {
line, err := encodePoints(w.service.Options(), point...)
if err != nil {
return err
}
return w.write(ctx, line)
}