Skip to content

Commit 7c8e5d6

Browse files
authored
Avoid unnecessary reopening of HTTP streams in GetObject() (#1908)
Sometimes consumers of Seek() might use it for purposes other than chaging the current read/write offset. For example, using whence = io.SeekCurrent to get the current seek position. However, minio-go invalidates the underlying HTTP stream unconditionally when Seek() is called, resulting in massive performance degradation in these scenarios. This commit avoids these issues by only reopening the stream if the seek position is actually changed.
1 parent 54e115c commit 7c8e5d6

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

api-get-object.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,8 @@ func (o *Object) Seek(offset int64, whence int) (n int64, err error) {
550550
}
551551
}
552552

553+
newOffset := o.currOffset
554+
553555
// Switch through whence.
554556
switch whence {
555557
default:
@@ -558,12 +560,12 @@ func (o *Object) Seek(offset int64, whence int) (n int64, err error) {
558560
if o.objectInfo.Size > -1 && offset > o.objectInfo.Size {
559561
return 0, io.EOF
560562
}
561-
o.currOffset = offset
563+
newOffset = offset
562564
case 1:
563565
if o.objectInfo.Size > -1 && o.currOffset+offset > o.objectInfo.Size {
564566
return 0, io.EOF
565567
}
566-
o.currOffset += offset
568+
newOffset += offset
567569
case 2:
568570
// If we don't know the object size return an error for io.SeekEnd
569571
if o.objectInfo.Size < 0 {
@@ -579,16 +581,17 @@ func (o *Object) Seek(offset int64, whence int) (n int64, err error) {
579581
if o.objectInfo.Size+offset < 0 {
580582
return 0, errInvalidArgument(fmt.Sprintf("Seeking at negative offset not allowed for %d", whence))
581583
}
582-
o.currOffset = o.objectInfo.Size + offset
584+
newOffset = o.objectInfo.Size + offset
583585
}
584586
// Reset the saved error since we successfully seeked, let the Read
585587
// and ReadAt decide.
586588
if o.prevErr == io.EOF {
587589
o.prevErr = nil
588590
}
589591

590-
// Ask lower level to fetch again from source
591-
o.seekData = true
592+
// Ask lower level to fetch again from source when necessary
593+
o.seekData = (newOffset != o.currOffset) || o.seekData
594+
o.currOffset = newOffset
592595

593596
// Return the effective offset.
594597
return o.currOffset, nil

0 commit comments

Comments
 (0)