Skip to content

Commit 78f5326

Browse files
committed
fix(disk-buffer): Cache length when closing buffer to avoid panic with --once
1 parent 7c40d48 commit 78f5326

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

models/buffer_disk.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ type DiskBuffer struct {
3333
// transaction. Metrics at those offsets should not be contained in new
3434
// batches.
3535
mask []int
36+
37+
// Cache the buffer length for informatory calls to Len() e.g. when running
38+
// Telegraf with --once. See https://github.com/influxdata/telegraf/issues/19248
39+
closedLength *int
3640
}
3741

3842
func NewDiskBuffer(id, path string, stats BufferStats, diskSync bool) (*DiskBuffer, error) {
@@ -60,6 +64,10 @@ func NewDiskBuffer(id, path string, stats BufferStats, diskSync bool) (*DiskBuff
6064
}
6165

6266
func (b *DiskBuffer) Len() int {
67+
if b.closedLength != nil {
68+
return *b.closedLength
69+
}
70+
6371
b.Lock()
6472
defer b.Unlock()
6573
return b.length()
@@ -262,9 +270,13 @@ func (b *DiskBuffer) Stats() BufferStats {
262270
}
263271

264272
func (b *DiskBuffer) Close() error {
273+
// Cache the length before closing the buffer
274+
b.closedLength = new(b.Len())
275+
265276
if err := b.file.Close(); err != nil {
266277
return fmt.Errorf("closing buffer failed: %w", err)
267278
}
279+
268280
return nil
269281
}
270282

0 commit comments

Comments
 (0)