Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion esutil/bulk_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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()
Expand Down