-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsync.go
241 lines (197 loc) · 4.3 KB
/
sync.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package stream
import (
"errors"
"io"
"os"
"sync"
)
// ErrRemoving is returned when requesting a Reader on a Stream which is being Removed.
var ErrRemoving = errors.New("cannot open a new reader while removing file")
// ErrCanceled indicates that stream has been canceled.
var ErrCanceled = errors.New("stream has been canceled")
type streamState int
const (
openState streamState = iota
closedState
canceledState
)
type broadcaster struct {
mu sync.RWMutex
cond *sync.Cond
state streamState
size int64
newHandleErr error
rs *readerSet
fileInUse sync.WaitGroup
}
func newBroadcaster() *broadcaster {
var b broadcaster
b.cond = sync.NewCond(b.mu.RLocker())
b.rs = newReaderSet()
b.addHandle()
return &b
}
// Wait blocks until we've written past the given offset, or until closed.
func (b *broadcaster) Wait(r *Reader, off int64) error {
b.mu.RLock()
defer b.mu.RUnlock()
for b.state == openState && off >= b.size && b.rs.has(r) {
b.cond.Wait()
}
switch b.state {
case canceledState:
return ErrCanceled
case closedState:
if off >= b.size {
return io.EOF
}
}
if !b.rs.has(r) {
return os.ErrClosed
}
return nil
}
func (b *broadcaster) Wrote(n int) {
if n > 0 {
b.mu.Lock()
b.size += int64(n)
b.mu.Unlock()
b.cond.Broadcast()
}
}
func (b *broadcaster) Close() (err error) {
b.mu.Lock()
b.setState(closedState)
b.mu.Unlock()
b.dropHandle()
return nil
}
func (b *broadcaster) Cancel() (err error) {
b.mu.Lock()
b.setState(canceledState)
b.preventNewHandles(ErrCanceled)
readersToClose := b.rs.dropAll()
b.mu.Unlock()
for _, r := range readersToClose {
r.Close()
}
return nil
}
func (b *broadcaster) PreventNewHandles(err error) {
b.mu.Lock()
b.preventNewHandles(err)
b.mu.Unlock()
}
func (b *broadcaster) preventNewHandles(err error) {
if b.newHandleErr == nil {
b.newHandleErr = err
}
}
func (b *broadcaster) WaitForZeroHandles() {
b.fileInUse.Wait()
}
func (b *broadcaster) UseHandle(do func() (int, error)) (int, error) {
b.mu.RLock()
switch b.state {
case canceledState:
b.mu.RUnlock()
return 0, ErrCanceled
}
b.mu.RUnlock()
// if !b.rs.has(r) => file is closed, => read will fail anyway
// let's not hold the read lock while doing the Read, we're not reading any state on the broadcaster there.
return do()
}
func (b *broadcaster) setState(s streamState) {
switch b.state {
case canceledState:
default:
b.state = s
b.cond.Broadcast()
}
}
func (b *broadcaster) Size() (size int64, isClosed bool) {
b.mu.RLock()
size = b.size
isClosed = b.state == closedState
b.mu.RUnlock()
return size, isClosed
}
func (b *broadcaster) addHandle() error {
b.mu.RLock()
defer b.mu.RUnlock()
if b.newHandleErr != nil {
return b.newHandleErr
}
b.fileInUse.Add(1)
return nil
}
func (b *broadcaster) dropHandle() { b.fileInUse.Done() }
func (b *broadcaster) NewReader(createReader func() (*Reader, error)) (*Reader, error) {
if err := b.addHandle(); err != nil {
return nil, err
}
r, err := createReader()
if err != nil {
b.dropHandle()
return nil, err
}
b.mu.Lock()
b.rs.add(r)
b.mu.Unlock()
return r, nil
}
func (b *broadcaster) DropReader(r *Reader) {
b.mu.Lock()
b.rs.drop(r)
isCanceled := b.state == canceledState
b.mu.Unlock()
b.dropHandle()
if isCanceled {
// we've canceled, either we've already broadcasted from Stream.Cancel() or will.
// if we have => there will be no more blocking reads, no need to broadcast here.
// if we haven't yet => we will, so no need to broadcast here.
// this avoids a broadcast storm on Cancel() when all readers call Close()
return
}
b.cond.Broadcast()
}
type onceWithErr struct {
once sync.Once
err error
}
func (co *onceWithErr) Do(closeFunc func() error) error {
co.once.Do(func() {
co.err = closeFunc()
})
return co.err
}
var (
errSeekEndAlreadySet = errors.New("seekEnd already set")
errSetAfterSeek = errors.New("seekEnd cannot be set after Seeking to End")
)
type sizeOnce struct {
once sync.Once
size int64
err error
}
func (s *sizeOnce) set(size int64) error {
err := errSeekEndAlreadySet
s.once.Do(func() {
s.size = size
err = nil
})
if s.err != nil {
return s.err
}
return err
}
func (s *sizeOnce) read() int64 {
s.once.Do(func() {
s.err = errSetAfterSeek
})
if s.err != nil {
return -1
}
return s.size
}