Skip to content

Commit e9c7d8a

Browse files
committed
feat: make SkipWriterReaderSeeker an io.Closer
1 parent c3789f2 commit e9c7d8a

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

v2/internal/io/skip_writer_read_seeker.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type SkipWriterReaderSeeker struct {
2525
type ReWriter func(ctx context.Context, skip uint64, w io.Writer) (uint64, error)
2626

2727
var _ io.ReadSeeker = (*SkipWriterReaderSeeker)(nil)
28+
var _ io.Closer = (*SkipWriterReaderSeeker)(nil)
2829

2930
// NewSkipWriterReaderSeeker creates an io.ReadSeeker around a ReWriter.
3031
func NewSkipWriterReaderSeeker(ctx context.Context, size uint64, cons ReWriter) *SkipWriterReaderSeeker {
@@ -37,7 +38,7 @@ func NewSkipWriterReaderSeeker(ctx context.Context, size uint64, cons ReWriter)
3738
}
3839

3940
// Note: not threadsafe
40-
func (c *SkipWriterReaderSeeker) Read(p []byte) (n int, err error) {
41+
func (c *SkipWriterReaderSeeker) Read(p []byte) (int, error) {
4142
// Check if there's already a write in progress
4243
if c.reader == nil {
4344
// No write in progress, start a new write from the current offset
@@ -90,19 +91,25 @@ func (c *SkipWriterReaderSeeker) Seek(offset int64, whence int) (int64, error) {
9091
// Cancel any ongoing write and wait for it to complete
9192
// TODO: if we're fast-forwarding with 'SeekCurrent', we may be able to read from the current reader instead.
9293
if c.reader != nil {
93-
c.writeCancel()
94-
95-
// Seek and Read should not be called concurrently so this is safe
96-
c.reader.Close()
97-
98-
select {
99-
case <-c.parentCtx.Done():
100-
return 0, c.parentCtx.Err()
101-
case <-c.writeComplete:
102-
}
103-
94+
err := c.Close()
10495
c.reader = nil
96+
if err != nil {
97+
return 0, err
98+
}
10599
}
106100

107101
return int64(c.offset), nil
108102
}
103+
104+
func (c *SkipWriterReaderSeeker) Close() error {
105+
c.writeCancel()
106+
// Seek and Read should not be called concurrently so this is safe
107+
c.reader.Close()
108+
109+
select {
110+
case <-c.parentCtx.Done():
111+
return c.parentCtx.Err()
112+
case <-c.writeComplete:
113+
}
114+
return nil
115+
}

0 commit comments

Comments
 (0)