Skip to content

Commit 28eb389

Browse files
committed
perf: ensure full write buffer for *File.ReadFrom
In `*File.ReadFrom`, ensure that the buffer is filled to capacity (`maxPacket` length) before performing the write. Prior to this change, the amount of data read into the buffer was dictated by the `io.Reader`'s `Read` implementation, and write performance would suffer when the Read would return less than maxPacket bytes of data. An example source would be a `net/http` response `Body` from a TLS server, which seems to cap each read to 16384 bytes -- half the default max packet size of 32768 bytes.
1 parent 19bfb49 commit 28eb389

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

client.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1842,7 +1842,8 @@ func (f *File) readFromWithConcurrency(r io.Reader, concurrency int) (read int64
18421842
off := f.offset
18431843

18441844
for {
1845-
n, err := r.Read(b)
1845+
// Fill the entire buffer.
1846+
n, err := io.ReadFull(r, b)
18461847

18471848
if n > 0 {
18481849
read += int64(n)
@@ -1868,7 +1869,7 @@ func (f *File) readFromWithConcurrency(r io.Reader, concurrency int) (read int64
18681869
}
18691870

18701871
if err != nil {
1871-
if err != io.EOF {
1872+
if !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
18721873
errCh <- rwErr{off, err}
18731874
}
18741875
return
@@ -2022,7 +2023,8 @@ func (f *File) ReadFrom(r io.Reader) (int64, error) {
20222023

20232024
var read int64
20242025
for {
2025-
n, err := r.Read(b)
2026+
// Fill the entire buffer.
2027+
n, err := io.ReadFull(r, b)
20262028
if n < 0 {
20272029
panic("sftp.File: reader returned negative count from Read")
20282030
}
@@ -2039,7 +2041,7 @@ func (f *File) ReadFrom(r io.Reader) (int64, error) {
20392041
}
20402042

20412043
if err != nil {
2042-
if err == io.EOF {
2044+
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
20432045
return read, nil // return nil explicitly.
20442046
}
20452047

0 commit comments

Comments
 (0)