diff --git a/esutil/bulk_indexer.go b/esutil/bulk_indexer.go index c462876562..6618611f33 100644 --- a/esutil/bulk_indexer.go +++ b/esutil/bulk_indexer.go @@ -265,6 +265,12 @@ type bulkIndexerStats struct { numRequests uint64 } +var bufPool = &sync.Pool{ + New: func() interface{} { + return new(bytes.Buffer) + }, +} + // NewBulkIndexer creates a new bulk indexer. func NewBulkIndexer(cfg BulkIndexerConfig) (BulkIndexer, error) { if cfg.Client == nil { @@ -337,6 +343,11 @@ func (bi *bulkIndexer) Close(ctx context.Context) error { bi.wg.Wait() } + for _, w := range bi.workers { + w.buf.Reset() + bufPool.Put(w.buf) + } + return nil } @@ -363,7 +374,7 @@ func (bi *bulkIndexer) init() { id: i, ch: bi.queue, bi: bi, - buf: bytes.NewBuffer(make([]byte, 0, bi.config.FlushBytes)), + buf: bufPool.Get().(*bytes.Buffer), ticker: time.NewTicker(bi.config.FlushInterval), } w.run()