Skip to content

Commit c43453e

Browse files
mhmtszrarp242
authored andcommitted
Fix memory leak in bulk indexer
elastic#701
1 parent 9d781a4 commit c43453e

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

esutil/bulk_indexer.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ type bulkIndexerStats struct {
279279
flushedBytes uint64
280280
}
281281

282+
var bufPool = &sync.Pool{
283+
New: func() any { return new(bytes.Buffer) },
284+
}
285+
282286
// NewBulkIndexer creates a new bulk indexer.
283287
func NewBulkIndexer(cfg BulkIndexerConfig) (BulkIndexer, error) {
284288
if cfg.Client == nil {
@@ -351,6 +355,11 @@ func (bi *bulkIndexer) Close(ctx context.Context) error {
351355
bi.wg.Wait()
352356
}
353357

358+
for _, w := range bi.workers {
359+
w.buf.Reset()
360+
bufPool.Put(w.buf)
361+
}
362+
354363
return nil
355364
}
356365

@@ -379,7 +388,7 @@ func (bi *bulkIndexer) init() {
379388
id: i,
380389
ch: bi.queue,
381390
bi: bi,
382-
buf: bytes.NewBuffer(make([]byte, 0, bi.config.FlushBytes)),
391+
buf: bufPool.Get().(*bytes.Buffer),
383392
ticker: time.NewTicker(bi.config.FlushInterval),
384393
}
385394
w.run()

0 commit comments

Comments
 (0)