Skip to content

Commit

Permalink
feat: Batcher.Add made variadic to allow passing of multiple points
Browse files Browse the repository at this point in the history
  • Loading branch information
vlastahajek committed Oct 1, 2024
1 parent e115711 commit bf9fb71
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
6 changes: 3 additions & 3 deletions influxdb3/batching/batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ func NewBatcher(options ...Option) *Batcher {
return b
}

// Add a metric to the batcher and call the given callbacks if any
func (b *Batcher) Add(p *influxdb3.Point) {
// Add metric(s) to the batcher and call the given callbacks if any
func (b *Batcher) Add(p ...*influxdb3.Point) {
b.Lock()
defer b.Unlock()

// Add the point
b.points = append(b.points, p)
b.points = append(b.points, p...)

// Call callbacks if a new batch is ready
if b.isReady() {
Expand Down
9 changes: 4 additions & 5 deletions influxdb3/batching/batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ func TestReady(t *testing.T) {
WithSize(batchSize),
)

for range batchSize {
b.Add(&influxdb3.Point{})
}
points := []*influxdb3.Point{{}, {}, {}, {}, {}}
b.Add(points...)

assert.True(t, b.Ready(), "Batcher should be ready when the batch size is reached")
}
Expand Down Expand Up @@ -115,13 +114,13 @@ func TestPartialEmit(t *testing.T) {
}),
)

b.Add(&influxdb3.Point{})
b.Add(&influxdb3.Point{}, &influxdb3.Point{})
b.Add(&influxdb3.Point{})

points := b.Emit()

assert.False(t, emitted, "Emit callback should not have been called automatically")
assert.Len(t, points, 2, "Emit should return all points when batch size is not reached")
assert.Len(t, points, 3, "Emit should return all points when batch size is not reached")
}

func TestThreadSafety(t *testing.T) {
Expand Down

0 comments on commit bf9fb71

Please sign in to comment.