Skip to content

Commit

Permalink
Optional custom SpooledTempFile threshold (#64)
Browse files Browse the repository at this point in the history
Use ENV var `WARCMaxInMemorySize` to set a custom threshold.
  • Loading branch information
vbanos authored Jan 20, 2025
1 parent 8539cbe commit 8909fa6
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion read.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"io"
"os"
"strconv"

"github.com/CorentinB/warc/pkg/spooledtempfile"
Expand All @@ -14,6 +15,7 @@ import (
type Reader struct {
bufReader *bufio.Reader
record *Record
threshold int
}

type reader interface {
Expand All @@ -27,9 +29,18 @@ func NewReader(reader io.ReadCloser) (*Reader, error) {
return nil, err
}
bufioReader := bufio.NewReader(decReader)
thresholdString := os.Getenv("WARCMaxInMemorySize")
threshold := -1
if thresholdString != "" {
threshold, err = strconv.Atoi(thresholdString)
if err != nil {
return nil, err
}
}

return &Reader{
bufReader: bufioReader,
threshold: threshold,
}, nil
}

Expand Down Expand Up @@ -94,7 +105,7 @@ func (r *Reader) ReadRecord() (*Record, bool, error) {
}

// reading doesn't really need to be in TempDir, nor can we access it as it's on the client.
buf := spooledtempfile.NewSpooledTempFile("warc", "", -1, false, -1)
buf := spooledtempfile.NewSpooledTempFile("warc", "", r.threshold, false, -1)
_, err = io.CopyN(buf, tempReader, length)
if err != nil {
return nil, false, fmt.Errorf("copying record content: %w", err)
Expand Down

0 comments on commit 8909fa6

Please sign in to comment.