Skip to content

Commit

Permalink
Merge pull request #121 from fpetkovski/timing-closer-writer
Browse files Browse the repository at this point in the history
Add support for WriterTo in TimingReader
  • Loading branch information
MichaHoffmann authored Sep 7, 2024
2 parents de861b4 + 46e7fe0 commit 7adf08b
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions objstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,6 @@ func newTimingReader(r io.Reader, closeReader bool, op string, dur *prometheus.H

_, isSeeker := r.(io.Seeker)
_, isReaderAt := r.(io.ReaderAt)

if isSeeker && isReaderAt {
// The assumption is that in most cases when io.ReaderAt() is implemented then
// io.Seeker is implemented too (e.g. os.File).
Expand All @@ -737,6 +736,9 @@ func newTimingReader(r io.Reader, closeReader bool, op string, dur *prometheus.H
if isSeeker {
return &timingReaderSeeker{timingReader: trc}
}
if _, isWriterTo := r.(io.WriterTo); isWriterTo {
return &timingReaderWriterTo{timingReader: trc}
}

return &trc
}
Expand Down Expand Up @@ -772,19 +774,23 @@ func (r *timingReader) Close() error {

func (r *timingReader) Read(b []byte) (n int, err error) {
n, err = r.Reader.Read(b)
r.updateMetrics(n, err)
return n, err
}

func (r *timingReader) updateMetrics(n int, err error) {
if r.fetchedBytes != nil {
r.fetchedBytes.WithLabelValues(r.op).Add(float64(n))
}

r.readBytes += int64(n)

// Report metric just once.
if !r.alreadyGotErr && err != nil && err != io.EOF {
if !r.isFailureExpected(err) && !errors.Is(err, context.Canceled) {
r.failed.WithLabelValues(r.op).Inc()
}
r.alreadyGotErr = true
}
return n, err
}

type timingReaderSeeker struct {
Expand All @@ -802,3 +808,13 @@ type timingReaderSeekerReaderAt struct {
func (rsc *timingReaderSeekerReaderAt) ReadAt(p []byte, off int64) (int, error) {
return (rsc.Reader).(io.ReaderAt).ReadAt(p, off)
}

type timingReaderWriterTo struct {
timingReader
}

func (t *timingReaderWriterTo) WriteTo(w io.Writer) (n int64, err error) {
n, err = (t.Reader).(io.WriterTo).WriteTo(w)
t.timingReader.updateMetrics(int(n), err)
return n, err
}

0 comments on commit 7adf08b

Please sign in to comment.