Skip to content

Commit

Permalink
fix: gracefully handle read errors when retrying documents (#137)
Browse files Browse the repository at this point in the history
* fix: gracefully handle read errors when retrying documents

* fix: always size the copyBuf appropriately
  • Loading branch information
kruskall authored Mar 14, 2024
1 parent 5a759bb commit b3aa972
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions bulk_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ func (b *bulkIndexer) Flush(ctx context.Context) (BulkIndexerResponseStat, error
b.copyBuf = slices.Grow(b.copyBuf, b.buf.Len()-cap(b.copyBuf))
b.copyBuf = b.copyBuf[:cap(b.copyBuf)]
}
copy(b.copyBuf, b.buf.Bytes())
n := copy(b.copyBuf, b.buf.Bytes())
b.copyBuf = b.copyBuf[:n]
}

req := esapi.BulkRequest{
Expand Down Expand Up @@ -338,7 +339,10 @@ func (b *bulkIndexer) Flush(ctx context.Context) (BulkIndexerResponseStat, error
if b.gzipw != nil {
// First loop, read from the gzip reader
if len(buf) == 0 {
n, _ := gr.Read(buf[:cap(buf)])
n, err := gr.Read(buf[:cap(buf)])
if err != nil && err != io.EOF {
return resp, fmt.Errorf("failed to read from compressed buffer: %w", err)
}
buf = buf[:n]
}

Expand All @@ -348,7 +352,10 @@ func (b *bulkIndexer) Flush(ctx context.Context) (BulkIndexerResponseStat, error
// loop until we've seen the start newline
for seen+newlines < startln {
seen += newlines
n, _ := gr.Read(buf[:cap(buf)])
n, err := gr.Read(buf[:cap(buf)])
if err != nil && err != io.EOF {
return resp, fmt.Errorf("failed to read from compressed buffer: %w", err)
}
buf = buf[:n]
newlines = bytes.Count(buf, []byte{'\n'})
}
Expand All @@ -364,7 +371,10 @@ func (b *bulkIndexer) Flush(ctx context.Context) (BulkIndexerResponseStat, error
// loop until we've seen the end newline
for seen+newlines < endln {
seen += newlines
n, _ := gr.Read(buf[:cap(buf)])
n, err := gr.Read(buf[:cap(buf)])
if err != nil && err != io.EOF {
return resp, fmt.Errorf("failed to read from compressed buffer: %w", err)
}
buf = buf[:n]
newlines = bytes.Count(buf, []byte{'\n'})
if seen+newlines < endln {
Expand Down

0 comments on commit b3aa972

Please sign in to comment.