-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeker.go
101 lines (90 loc) · 1.85 KB
/
seeker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package sfstreams
import (
"errors"
"io"
"sync"
)
type parentSeeker struct {
io.ReadSeekCloser
underlying io.ReadSeekCloser
mutex *sync.Mutex
closeWg *sync.WaitGroup
}
func newParentSeeker(src io.ReadSeekCloser, downstreamReaders int) *parentSeeker {
wg := new(sync.WaitGroup)
wg.Add(downstreamReaders)
go func() {
wg.Wait()
_ = src.Close()
}()
return &parentSeeker{
underlying: src,
mutex: new(sync.Mutex),
closeWg: wg,
}
}
func (p *parentSeeker) Read(b []byte) (int, error) {
return p.underlying.Read(b)
}
func (p *parentSeeker) Seek(offset int64, whence int) (int64, error) {
return p.underlying.Seek(offset, whence)
}
func (p *parentSeeker) Close() error {
return p.underlying.Close()
}
type downstreamSeeker struct {
io.ReadSeekCloser
parent *parentSeeker
pos int64
eof bool
eofPos int64
closed bool
}
func newSyncSeeker(parent *parentSeeker) *downstreamSeeker {
return &downstreamSeeker{
parent: parent,
pos: 0,
eof: false,
eofPos: 0,
closed: false,
}
}
func (s *downstreamSeeker) Read(b []byte) (int, error) {
if s.closed {
return 0, io.ErrClosedPipe
}
s.parent.mutex.Lock()
defer s.parent.mutex.Unlock()
if s.eof && s.pos == s.eofPos {
return 0, io.EOF
}
offset, err := s.parent.Seek(s.pos, io.SeekStart)
if err != nil {
return 0, err
}
i, err := s.parent.Read(b)
s.pos = offset + int64(i)
if err != nil && errors.Is(err, io.EOF) {
s.eof = true
s.eofPos = s.pos
}
return i, err
}
func (s *downstreamSeeker) Seek(offset int64, whence int) (int64, error) {
if s.closed {
return 0, io.ErrClosedPipe
}
s.parent.mutex.Lock()
defer s.parent.mutex.Unlock()
offset, err := s.parent.Seek(offset, whence)
if err != nil {
return s.pos, err
}
s.pos = offset
return s.pos, nil
}
func (s *downstreamSeeker) Close() error {
s.parent.closeWg.Done()
s.closed = true
return nil
}