diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-02-15 13:58:46 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-02-16 10:37:43 +0300 |
commit | 878197c52e0e448cba7b581ac8a648c92299abc4 (patch) | |
tree | be5b4f2431d15813d2cd80a5d5b33c01b7db47bf /streamio | |
parent | 4e5fe176e6b36866ba305e5af1ca494b031cadc8 (diff) |
streamio: Simplify implementation of ReadFrom
The ReadFrom implementation is quite hard to read as the loop depends on
state from the previous iteration. This commit refactors the code to not
do so anymore by always sending data if we've read something, even if
there was a read error.
Diffstat (limited to 'streamio')
-rw-r--r-- | streamio/stream.go | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/streamio/stream.go b/streamio/stream.go index 85a325d84..b82fb40fa 100644 --- a/streamio/stream.go +++ b/streamio/stream.go @@ -5,6 +5,7 @@ package streamio import ( + "errors" "io" "os" "strconv" @@ -155,20 +156,21 @@ func (sw *sendWriter) ReadFrom(r io.Reader) (int64, error) { var nRead int64 buf := make([]byte, WriteBufferSize) - var errRead, errSend error - for errSend == nil && errRead != io.EOF { - var n int - - n, errRead = r.Read(buf) + for { + n, err := r.Read(buf) nRead += int64(n) - if errRead != nil && errRead != io.EOF { - return nRead, errRead - } if n > 0 { - errSend = sw.sender(buf[:n]) + if err := sw.sender(buf[:n]); err != nil { + return nRead, err + } } - } - return nRead, errSend + if err != nil { + if errors.Is(err, io.EOF) { + return nRead, nil + } + return nRead, err + } + } } |