Skip to content

Commit

Permalink
fix: update LimitedWriter
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Dec 9, 2024
1 parent 067d4f6 commit 0076d0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
20 changes: 9 additions & 11 deletions internal/io/limitedwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import "io"

// LimitedWriter is a writer that writes to an underlying writer up to a limit.
type LimitedWriter struct {
w io.Writer
limit int64
written int64
W io.Writer // underlying writer
N int64 // remaining bytes
}

// LimitWriter returns a new LimitWriter that writes to w.
Expand All @@ -31,19 +30,18 @@ type LimitedWriter struct {
// w: the writer to write to
// limit: the maximum number of bytes to write
func LimitWriter(w io.Writer, limit int64) *LimitedWriter {
return &LimitedWriter{w: w, limit: limit}
return &LimitedWriter{W: w, N: limit}
}

// Write writes p to the underlying writer up to the limit.
func (lw *LimitedWriter) Write(p []byte) (int, error) {
remaining := lw.limit - lw.written
if remaining <= 0 {
func (l *LimitedWriter) Write(p []byte) (int, error) {
if l.N <= 0 {
return 0, io.ErrShortWrite
}
if int64(len(p)) > remaining {
p = p[:remaining]
if int64(len(p)) > l.N {
p = p[:l.N]
}
n, err := lw.w.Write(p)
lw.written += int64(n)
n, err := l.W.Write(p)
l.N -= int64(n)
return n, err
}
2 changes: 1 addition & 1 deletion plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
)

// maxPluginOutputSize is the maximum size of the plugin output.
const maxPluginOutputSize = 10 * 1024 * 1024 // 10 MiB
const maxPluginOutputSize = 64 * 1024 * 1024 // 64 MiB

var executor commander = &execCommander{} // for unit test

Expand Down

0 comments on commit 0076d0f

Please sign in to comment.