Skip to content
Prev Previous commit
Next Next commit
runutil: inform the user if exhaustion fails
  • Loading branch information
GiedriusS committed Jul 19, 2019
commit a1cad0cc5a6dc9bb74ac31450af438f85d08a90e
14 changes: 12 additions & 2 deletions pkg/runutil/runutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ func CloseWithLogOnErr(logger log.Logger, closer io.Closer, format string, a ...

// ExhaustCloseWithLogOnErr closes the io.ReadCloser with a log message on error but exhausts the reader before.
func ExhaustCloseWithLogOnErr(logger log.Logger, r io.ReadCloser, format string, a ...interface{}) {
_, _ = io.Copy(ioutil.Discard, r)
_, err := io.Copy(ioutil.Discard, r)
level.Warn(logger).Log("msg", "failed to exhaust reader, performance may be impeded", "err", err)

CloseWithLogOnErr(logger, r, format, a...)
}

Expand All @@ -134,6 +136,14 @@ func CloseWithErrCapture(err *error, closer io.Closer, format string, a ...inter

// ExhaustCloseWithErrCapture closes the io.ReadCloser with error capture but exhausts the reader before.
func ExhaustCloseWithErrCapture(err *error, r io.ReadCloser, format string, a ...interface{}) {
_, _ = io.Copy(ioutil.Discard, r)
_, copyErr := io.Copy(ioutil.Discard, r)

CloseWithErrCapture(err, r, format, a...)

// Prepend the io.Copy error.
merr := tsdberrors.MultiError{}
merr.Add(copyErr)
merr.Add(*err)

*err = merr.Err()
}