Skip to content

Commit 70b1de5

Browse files
committed
fix: use mutex only if necessary (#352)
1 parent 21f5a82 commit 70b1de5

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
## [unreleased]
2+
### Bug fixes
3+
- [#354](https://github.com/influxdata/influxdb-client-go/pull/354) More efficient synchronization in WriteAPIBlocking.
4+
25

36
## 2.10.0 [2022-08-25]
47
### Features

api/writeAPIBlocking.go

+33-22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"strings"
1010
"sync"
11+
"sync/atomic"
1112

1213
http2 "github.com/influxdata/influxdb-client-go/v2/api/http"
1314
"github.com/influxdata/influxdb-client-go/v2/api/write"
@@ -51,7 +52,7 @@ type WriteAPIBlocking interface {
5152
type writeAPIBlocking struct {
5253
service *iwrite.Service
5354
writeOptions *write.Options
54-
batching bool
55+
batching int32
5556
batch []string
5657
mu sync.Mutex
5758
}
@@ -69,28 +70,28 @@ func NewWriteAPIBlockingWithBatching(org string, bucket string, service http2.Se
6970
}
7071

7172
func (w *writeAPIBlocking) EnableBatching() {
72-
w.mu.Lock()
73-
defer w.mu.Unlock()
74-
if !w.batching {
75-
w.batching = true
73+
if atomic.LoadInt32(&w.batching) == 0 {
74+
w.mu.Lock()
75+
w.batching = 1
7676
w.batch = make([]string, 0, w.writeOptions.BatchSize())
77+
w.mu.Unlock()
7778
}
7879
}
7980

8081
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 {
85-
w.batch = append(w.batch, line)
86-
if len(w.batch) == int(w.writeOptions.BatchSize()) {
87-
body = strings.Join(w.batch, "\n")
88-
w.batch = w.batch[:0]
89-
} else {
90-
return nil
91-
}
82+
if atomic.LoadInt32(&w.batching) > 0 {
83+
return func(b string) error {
84+
w.mu.Lock()
85+
defer w.mu.Unlock()
86+
w.batch = append(w.batch, b)
87+
if len(w.batch) == int(w.writeOptions.BatchSize()) {
88+
return w.flush(ctx)
89+
} else {
90+
return nil
91+
}
92+
}(line)
9293
}
93-
err := w.service.WriteBatch(ctx, iwrite.NewBatch(body, w.writeOptions.MaxRetryTime()))
94+
err := w.service.WriteBatch(ctx, iwrite.NewBatch(line, w.writeOptions.MaxRetryTime()))
9495
if err != nil {
9596
return err
9697
}
@@ -112,13 +113,23 @@ func (w *writeAPIBlocking) WritePoint(ctx context.Context, point ...*write.Point
112113
return w.write(ctx, line)
113114
}
114115

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 {
116+
// flush is unsychronized helper for creating and sending batch
117+
// Must be called from synchronized block
118+
func (w *writeAPIBlocking) flush(ctx context.Context) error {
119+
if len(w.batch) > 0 {
119120
body := strings.Join(w.batch, "\n")
120121
w.batch = w.batch[:0]
121-
return w.service.WriteBatch(ctx, iwrite.NewBatch(body, w.writeOptions.MaxRetryTime()))
122+
b := iwrite.NewBatch(body, w.writeOptions.MaxRetryTime())
123+
return w.service.WriteBatch(ctx, b)
124+
}
125+
return nil
126+
}
127+
128+
func (w *writeAPIBlocking) Flush(ctx context.Context) error {
129+
if atomic.LoadInt32(&w.batching) > 0 {
130+
w.mu.Lock()
131+
defer w.mu.Unlock()
132+
return w.flush(ctx)
122133
}
123134
return nil
124135
}

0 commit comments

Comments
 (0)