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
|
package response
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bytes"
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
@ -14,9 +13,9 @@ const (
|
||||||
ExportMaxRows = 50000
|
ExportMaxRows = 50000
|
||||||
)
|
)
|
||||||
|
|
||||||
// StreamCSV writes a UTF-8 CSV attachment, fetching rows in batches until a batch is empty
|
// StreamCSV builds a UTF-8 CSV attachment in memory (batched DB reads) and sends it
|
||||||
// or ExportMaxRows is reached. Flushes the header row immediately so reverse proxies (nginx)
|
// with a Content-Length body. Buffered responses avoid chunked-encoding issues behind
|
||||||
// receive bytes before the first DB batch completes.
|
// nginx and other reverse proxies (ERR_INCOMPLETE_CHUNKED_ENCODING).
|
||||||
func StreamCSV(
|
func StreamCSV(
|
||||||
c *fiber.Ctx,
|
c *fiber.Ctx,
|
||||||
filename string,
|
filename string,
|
||||||
|
|
@ -24,77 +23,51 @@ func StreamCSV(
|
||||||
fetchBatch func(offset, limit int32) ([][]string, error),
|
fetchBatch func(offset, limit int32) ([][]string, error),
|
||||||
onError func(error),
|
onError func(error),
|
||||||
) error {
|
) error {
|
||||||
c.Set("Content-Type", "text/csv; charset=utf-8")
|
var buf bytes.Buffer
|
||||||
c.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
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) {
|
writer := csv.NewWriter(&buf)
|
||||||
report := func(err error) {
|
if err := writer.Write(headers); err != nil {
|
||||||
if err == nil {
|
return fmt.Errorf("csv export header: %w", err)
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
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 {
|
if onError != nil {
|
||||||
onError(err)
|
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 {
|
writer.Flush()
|
||||||
report(err)
|
if err := writer.Error(); err != nil {
|
||||||
return
|
return fmt.Errorf("csv export flush: %w", err)
|
||||||
}
|
}
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
var offset int32
|
c.Set("Content-Type", "text/csv; charset=utf-8")
|
||||||
var written int
|
c.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
||||||
for written < ExportMaxRows {
|
return c.Send(buf.Bytes())
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user