fix: buffer CSV exports to avoid incomplete chunked encoding
Build the full CSV in memory before sending so nginx and browsers receive a Content-Length response instead of a truncated chunked stream. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
e2c5833081
commit
aea9ebd60b
|
|
@ -1,10 +1,9 @@
|
|||
package response
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
|
@ -14,9 +13,9 @@ const (
|
|||
ExportMaxRows = 50000
|
||||
)
|
||||
|
||||
// StreamCSV writes a UTF-8 CSV attachment, fetching rows in batches until a batch is empty
|
||||
// or ExportMaxRows is reached. Flushes the header row immediately so reverse proxies (nginx)
|
||||
// receive bytes before the first DB batch completes.
|
||||
// StreamCSV builds a UTF-8 CSV attachment in memory (batched DB reads) and sends it
|
||||
// with a Content-Length body. Buffered responses avoid chunked-encoding issues behind
|
||||
// nginx and other reverse proxies (ERR_INCOMPLETE_CHUNKED_ENCODING).
|
||||
func StreamCSV(
|
||||
c *fiber.Ctx,
|
||||
filename string,
|
||||
|
|
@ -24,77 +23,51 @@ func StreamCSV(
|
|||
fetchBatch func(offset, limit int32) ([][]string, error),
|
||||
onError func(error),
|
||||
) error {
|
||||
c.Set("Content-Type", "text/csv; charset=utf-8")
|
||||
c.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
||||
var buf bytes.Buffer
|
||||
if _, err := buf.Write([]byte{0xEF, 0xBB, 0xBF}); err != nil {
|
||||
return fmt.Errorf("csv export bom: %w", err)
|
||||
}
|
||||
|
||||
c.Context().SetBodyStreamWriter(func(w *bufio.Writer) {
|
||||
report := func(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
writer := csv.NewWriter(&buf)
|
||||
if err := writer.Write(headers); err != nil {
|
||||
return fmt.Errorf("csv export header: %w", err)
|
||||
}
|
||||
|
||||
var offset int32
|
||||
var written int
|
||||
for written < ExportMaxRows {
|
||||
limit := int32(ExportBatchSize)
|
||||
if remaining := ExportMaxRows - written; remaining < ExportBatchSize {
|
||||
limit = int32(remaining)
|
||||
}
|
||||
rows, err := fetchBatch(offset, limit)
|
||||
if err != nil {
|
||||
if onError != nil {
|
||||
onError(err)
|
||||
return
|
||||
}
|
||||
slog.Error("csv export stream failed", "error", err, "path", c.Path())
|
||||
return fmt.Errorf("csv export batch at offset %d: %w", offset, err)
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
break
|
||||
}
|
||||
for _, row := range rows {
|
||||
if err := writer.Write(row); err != nil {
|
||||
return fmt.Errorf("csv export row: %w", err)
|
||||
}
|
||||
}
|
||||
written += len(rows)
|
||||
if len(rows) < int(limit) {
|
||||
break
|
||||
}
|
||||
offset += int32(len(rows))
|
||||
}
|
||||
|
||||
if _, err := w.Write([]byte{0xEF, 0xBB, 0xBF}); err != nil {
|
||||
report(err)
|
||||
return
|
||||
}
|
||||
writer := csv.NewWriter(w)
|
||||
if err := writer.Write(headers); err != nil {
|
||||
report(err)
|
||||
return
|
||||
}
|
||||
writer.Flush()
|
||||
if err := writer.Error(); err != nil {
|
||||
report(err)
|
||||
return
|
||||
}
|
||||
if err := w.Flush(); err != nil {
|
||||
report(err)
|
||||
return
|
||||
}
|
||||
writer.Flush()
|
||||
if err := writer.Error(); err != nil {
|
||||
return fmt.Errorf("csv export flush: %w", err)
|
||||
}
|
||||
|
||||
var offset int32
|
||||
var written int
|
||||
for written < ExportMaxRows {
|
||||
limit := int32(ExportBatchSize)
|
||||
if remaining := ExportMaxRows - written; remaining < ExportBatchSize {
|
||||
limit = int32(remaining)
|
||||
}
|
||||
rows, err := fetchBatch(offset, limit)
|
||||
if err != nil {
|
||||
report(err)
|
||||
return
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
break
|
||||
}
|
||||
for _, row := range rows {
|
||||
if err := writer.Write(row); err != nil {
|
||||
report(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
writer.Flush()
|
||||
if err := writer.Error(); err != nil {
|
||||
report(err)
|
||||
return
|
||||
}
|
||||
if err := w.Flush(); err != nil {
|
||||
report(err)
|
||||
return
|
||||
}
|
||||
written += len(rows)
|
||||
if len(rows) < int(limit) {
|
||||
break
|
||||
}
|
||||
offset += int32(len(rows))
|
||||
}
|
||||
})
|
||||
|
||||
return nil
|
||||
c.Set("Content-Type", "text/csv; charset=utf-8")
|
||||
c.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
||||
return c.Send(buf.Bytes())
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user