8
8
"context"
9
9
"strings"
10
10
"sync"
11
+ "sync/atomic"
11
12
12
13
http2 "github.com/influxdata/influxdb-client-go/v2/api/http"
13
14
"github.com/influxdata/influxdb-client-go/v2/api/write"
@@ -51,9 +52,10 @@ type WriteAPIBlocking interface {
51
52
type writeAPIBlocking struct {
52
53
service * iwrite.Service
53
54
writeOptions * write.Options
54
- batching bool
55
- batch []string
56
- mu sync.Mutex
55
+ // more appropriate Bool type from sync/atomic cannot be used because it is available since go 1.19
56
+ batching int32
57
+ batch []string
58
+ mu sync.Mutex
57
59
}
58
60
59
61
// NewWriteAPIBlocking creates new instance of blocking write client for writing data to bucket belonging to org
@@ -69,28 +71,26 @@ func NewWriteAPIBlockingWithBatching(org string, bucket string, service http2.Se
69
71
}
70
72
71
73
func (w * writeAPIBlocking ) EnableBatching () {
72
- w .mu .Lock ()
73
- defer w .mu .Unlock ()
74
- if ! w .batching {
75
- w .batching = true
74
+ if atomic .LoadInt32 (& w .batching ) == 0 {
75
+ w .mu .Lock ()
76
+ w .batching = 1
76
77
w .batch = make ([]string , 0 , w .writeOptions .BatchSize ())
78
+ w .mu .Unlock ()
77
79
}
78
80
}
79
81
80
82
func (w * writeAPIBlocking ) write (ctx context.Context , line string ) error {
81
- w .mu .Lock ()
82
- defer w .mu .Unlock ()
83
- body := line
84
- if w .batching {
83
+ if atomic .LoadInt32 (& w .batching ) > 0 {
84
+ w .mu .Lock ()
85
+ defer w .mu .Unlock ()
85
86
w .batch = append (w .batch , line )
86
87
if len (w .batch ) == int (w .writeOptions .BatchSize ()) {
87
- body = strings .Join (w .batch , "\n " )
88
- w .batch = w .batch [:0 ]
88
+ return w .flush (ctx )
89
89
} else {
90
90
return nil
91
91
}
92
92
}
93
- err := w .service .WriteBatch (ctx , iwrite .NewBatch (body , w .writeOptions .MaxRetryTime ()))
93
+ err := w .service .WriteBatch (ctx , iwrite .NewBatch (line , w .writeOptions .MaxRetryTime ()))
94
94
if err != nil {
95
95
return err
96
96
}
@@ -112,13 +112,23 @@ func (w *writeAPIBlocking) WritePoint(ctx context.Context, point ...*write.Point
112
112
return w .write (ctx , line )
113
113
}
114
114
115
- func ( w * writeAPIBlocking ) Flush ( ctx context. Context ) error {
116
- w . mu . Lock ()
117
- defer w . mu . Unlock ()
118
- if w . batching && len (w .batch ) > 0 {
115
+ // flush is unsychronized helper for creating and sending batch
116
+ // Must be called from synchronized block
117
+ func ( w * writeAPIBlocking ) flush ( ctx context. Context ) error {
118
+ if len (w .batch ) > 0 {
119
119
body := strings .Join (w .batch , "\n " )
120
120
w .batch = w .batch [:0 ]
121
- return w .service .WriteBatch (ctx , iwrite .NewBatch (body , w .writeOptions .MaxRetryTime ()))
121
+ b := iwrite .NewBatch (body , w .writeOptions .MaxRetryTime ())
122
+ return w .service .WriteBatch (ctx , b )
123
+ }
124
+ return nil
125
+ }
126
+
127
+ func (w * writeAPIBlocking ) Flush (ctx context.Context ) error {
128
+ if atomic .LoadInt32 (& w .batching ) > 0 {
129
+ w .mu .Lock ()
130
+ defer w .mu .Unlock ()
131
+ return w .flush (ctx )
122
132
}
123
133
return nil
124
134
}
0 commit comments