/
blocking.go
268 lines (232 loc) · 5.67 KB
/
blocking.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package nasshp
import (
"errors"
"sync"
"time"
)
// Waiter is similar to sync.CondWait as it can be used exactly
// the same way, except:
// - it only supports a single waiter.
// Having more than one waiter will lead to undefined behavior.
// - it allows to configure wait timeouts.
// - it allows to propagate error failures.
type Waiter struct {
l sync.Locker
c chan bool
e error
}
// NewWaiter creates a new Waiter.
func NewWaiter(l sync.Locker) *Waiter {
return &Waiter{
l: l,
c: make(chan bool, 1),
}
}
// Fail will cause anyone waiting on Wait*() to receive the specified error.
// Do NOT hold the lock when invoking Fail().
func (w *Waiter) Fail(err error) {
w.l.Lock()
defer w.l.Unlock()
w.e = err
w.Signal()
}
// Signal will notify anyone waiting on Wait*() to check the condition again.
// It can be invoked with or without the lock held.
func (w *Waiter) Signal() {
select {
case w.c <- true:
default:
}
}
// Wait for a signal to check that the desired condition has changed.
//
// The specified lock MUST be held before calling Wait.
// Just like with CondWait(), wait can return spurious events, where
// error is nil, but none of the desired conditions has changed.
func (w *Waiter) Wait() error {
if w.e != nil {
return w.e
}
w.l.Unlock()
_ = <-w.c
w.l.Lock()
return w.e
}
// ErrorExpired is returned by WaitFor when the timer expires without events.
var ErrorExpired = errors.New("timer expired")
// WaitFor is just like Wait(), but with a timeout.
//
// The specified lock MUST be held before calling Wait.
// If the timeout expires, ErrorExpired is returned.
func (w *Waiter) WaitFor(d time.Duration) error {
w.l.Unlock()
select {
case <-w.c:
w.l.Lock()
return w.e
case <-time.After(d):
w.l.Lock()
return ErrorExpired
}
}
// BlockingSendWindow allows to split the filling and emptying of a SendWindow across different goroutines.
//
// Specifically, it assumes that there is one goroutine calling ToFill and Fill, and another goroutine
// calling ToEmpty, Empty, and possibly Reset and Acknowledge.
//
// Note that the BlockingSendWindow still supports at most one sender and at most one receiver, not more.
type BlockingSendWindow struct {
w SendWindow
l sync.Mutex
max uint64 // Maximum amount of pending bytes.
cf *Waiter // Wait for fill to be ready.
ce *Waiter // Wait for empty to be ready.
}
// BlockingReceiveWindow allows to split the filling and emptying of a ReceiveWindow across different goroutines.
//
// Specifically, it assumes that there is one goroutine calling ToFill and Fill, and another goroutine
// calling ToEmpty, Empty, and Reset.
//
// Having more than one filling or more than one empting goroutine is unsupported.
type BlockingReceiveWindow struct {
w ReceiveWindow
l sync.Mutex
max uint64 // Maximum amount of pending bytes.
cf *Waiter // Wait for fill to be ready.
ce *Waiter // Wait for empty to be ready.
}
func NewBlockingReceiveWindow(pool *BufferPool, max uint64) *BlockingReceiveWindow {
bw := &BlockingReceiveWindow{}
bw.w.pool = pool
bw.w.buffer.Init()
bw.ce = NewWaiter(&bw.l)
bw.cf = NewWaiter(&bw.l)
bw.max = max
return bw
}
func (b *BlockingReceiveWindow) WaitToFill() error {
b.l.Lock()
defer b.l.Unlock()
for (b.w.Filled - b.w.Emptied) >= b.max {
if err := b.cf.Wait(); err != nil {
return err
}
}
return nil
}
func (b *BlockingReceiveWindow) WaitToEmpty() error {
b.l.Lock()
defer b.l.Unlock()
for len(b.w.ToEmpty()) == 0 {
if err := b.ce.Wait(); err != nil {
return err
}
}
return nil
}
func (b *BlockingReceiveWindow) ToFill() []byte {
b.l.Lock()
defer b.l.Unlock()
data := b.w.ToFill()
return data
}
func (b *BlockingReceiveWindow) Fill(size int) uint64 {
b.l.Lock()
defer b.l.Unlock()
filled := b.w.Fill(size)
b.ce.Signal()
return filled
}
func (b *BlockingReceiveWindow) Reset(wu uint32) error {
b.l.Lock()
defer b.l.Unlock()
return b.w.Reset(wu)
}
func (b *BlockingReceiveWindow) ToEmpty() []byte {
b.l.Lock()
defer b.l.Unlock()
data := b.w.ToEmpty()
return data
}
func (b *BlockingReceiveWindow) Empty(size int) {
b.l.Lock()
defer b.l.Unlock()
b.w.Empty(size)
b.cf.Signal()
}
func (b *BlockingReceiveWindow) Fail(err error) {
b.ce.Fail(err)
b.cf.Fail(err)
}
func NewBlockingSendWindow(pool *BufferPool, max uint64) *BlockingSendWindow {
bw := &BlockingSendWindow{}
bw.w.pool = pool
bw.w.buffer.Init()
bw.w.pending.Init()
bw.ce = NewWaiter(&bw.l)
bw.cf = NewWaiter(&bw.l)
bw.max = max
return bw
}
func (b *BlockingSendWindow) ToFill() []byte {
b.l.Lock()
defer b.l.Unlock()
data := b.w.ToFill()
return data
}
func (b *BlockingSendWindow) Fill(size int) uint64 {
b.l.Lock()
defer b.l.Unlock()
filled := b.w.Fill(size)
b.ce.Signal()
return filled
}
func (b *BlockingSendWindow) Reset(wu uint32) error {
b.l.Lock()
defer b.l.Unlock()
b.ce.Signal()
return b.w.Reset(wu)
}
func (b *BlockingSendWindow) AcknowledgeUntil(wu uint32) (uint64, error) {
b.l.Lock()
defer b.l.Unlock()
val, err := b.w.AcknowledgeUntil(wu)
b.cf.Signal()
return val, err
}
func (b *BlockingSendWindow) WaitToFill() error {
b.l.Lock()
defer b.l.Unlock()
for (b.w.Filled - b.w.acknowledged) >= b.max {
if err := b.cf.Wait(); err != nil {
return err
}
}
return nil
}
func (b *BlockingSendWindow) WaitToEmpty(d time.Duration) error {
b.l.Lock()
defer b.l.Unlock()
for len(b.w.ToEmpty()) == 0 {
if err := b.ce.WaitFor(d); err != nil {
return err
}
}
return nil
}
func (b *BlockingSendWindow) ToEmpty() []byte {
b.l.Lock()
defer b.l.Unlock()
data := b.w.ToEmpty()
return data
}
func (b *BlockingSendWindow) Empty(size int) {
b.l.Lock()
defer b.l.Unlock()
b.w.Empty(size)
b.cf.Signal()
}
func (b *BlockingSendWindow) Fail(err error) {
b.ce.Fail(err)
b.cf.Fail(err)
}