-
Notifications
You must be signed in to change notification settings - Fork 1
/
nb-chan.go
399 lines (342 loc) · 10.4 KB
/
nb-chan.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"sync"
"github.com/haraldrudell/parl/perrors"
"github.com/haraldrudell/parl/pruntime"
"github.com/haraldrudell/parl/pslices"
)
// NBChan is a non-blocking send channel with trillion-size buffer.
//
// - NBChan can be used as an error channel where the thread does not
// block from a delayed or missing reader.
// - errors can be read from the channel or fetched all at once using GetAll
// - NBChan is initialization-free, thread-safe, idempotent, deferrable and observable.
// - Ch(), Send(), Close() CloseNow() IsClosed() Count() are not blocked by channel send
// and are panic-free.
// - Close() CloseNow() are deferrable.
// - WaitForClose() waits until the underlying channel has been closed.
// - NBChan implements a thread-safe error store perrors.ParlError.
// - NBChan.GetError() returns thread panics and close errors.
// - No errors are added to the error store after the channel has closed.
// - NBChan does not generate errors. When it does, errors are thread panics
// or a close error. Neither is expected to occur
// - the underlying channel is closed after Close is invoked and the channel is emptied
// - cautious consumers may collect errors using the GetError method when:
// - — the Ch receive-only channel is detected as being closed or
// - — await using WaitForClose returns or
// - — IsClosed method returns true
//
// Usage:
//
// var errCh parl.NBChan[error]
// go thread(&errCh)
// err, ok := <-errCh.Ch()
// errCh.WaitForClose()
// errCh.GetError()
// …
// func thread(errCh *parl.NBChan[error]) {
// defer errCh.Close() // non-blocking close effective on send complete
// var err error
// defer parl.Recover(parl.Annotation(), &err, errCh.AddErrorProc)
// errCh.Ch() <- err // non-blocking
// if err = someFunc(); err != nil {
// err = perrors.Errorf("someFunc: %w", err)
// return
type NBChan[T any] struct {
closableChan ClosableChan[T]
stateLock sync.Mutex
unsentCount int // inside lock. One item may be with sendThread
sendQueue []T // inside lock. One item may be with sendThread
isRunningThread bool // inside lock
// closesOnThreadSend is created inside the lock every time a value is provided
// to the send thread
// - closesOnThreadSend will close immediately after the thread sends
// - from inside the lock, this allows for the thread’s value to be collected
closesOnThreadSend chan struct{} // write inside lock
isCloseInvoked AtomicBool // Set inside lock
isWaitForCloseInitialized AtomicBool // Set inside lock
waitForClose sync.WaitGroup // valid when isWaitForCloseInitialized is true
perrors.ParlError // thread panics
}
// NewNBChan instantiates a non-blocking trillion-size buffer channel.
// NewNBChan allows initialization based on an existing channel.
// NewNBChan does not need initialization and can be used like:
//
// var nbChan NBChan[error]
// go thread(&nbChan)
func NewNBChan[T any](ch ...chan T) (nbChan *NBChan[T]) {
nb := NBChan[T]{}
if len(ch) > 0 {
nb.closableChan = *NewClosableChan(ch[0]) // store ch if present
}
return &nb
}
// Ch obtains the receive-only channel
func (nb *NBChan[T]) Ch() (ch <-chan T) {
return nb.closableChan.Ch()
}
// Send sends non-blocking on the channel
func (nb *NBChan[T]) Send(value T) {
if nb.isCloseInvoked.IsTrue() {
return // no send after Close(), atomic performance
}
nb.stateLock.Lock()
defer nb.stateLock.Unlock()
if nb.isCloseInvoked.IsTrue() {
return // no send after Close()
}
nb.unsentCount++
// if thread is running, append to send queue
if nb.isRunningThread {
nb.sendQueue = append(nb.sendQueue, value)
return
}
// send using new thread
nb.startThread(value)
}
// Send sends non-blocking on the channel
func (nb *NBChan[T]) SendMany(values []T) {
if nb.isCloseInvoked.IsTrue() {
return // no send after Close(), atomic performance
}
var length int
if length = len(values); length == 0 {
return // nothing to do return
}
nb.stateLock.Lock()
defer nb.stateLock.Unlock()
if nb.isCloseInvoked.IsTrue() {
return // no send after Close()
}
nb.unsentCount += length
// if thread is running, add to send queue
if nb.isRunningThread {
nb.sendQueue = append(nb.sendQueue, values...)
return
}
// get next value to send, append remainign to send queue
var value T
if len(nb.sendQueue) > 0 {
value = nb.sendQueue[0]
pslices.TrimLeft(&nb.sendQueue, 1)
nb.sendQueue = append(nb.sendQueue, values...)
} else {
value = values[0]
nb.sendQueue = append(nb.sendQueue, values[1:]...)
}
nb.startThread(value)
}
// Get returns a slice of n or default all available items held by the channel.
// - if channel is empty, 0 items are returned
// - Get is non-blocking
// - n > 0: max this many items
// - n == 0 (or <0): all items
func (nb *NBChan[T]) Get(n ...int) (allItems []T) {
nb.stateLock.Lock()
defer nb.stateLock.Unlock()
// n0: 0 for all items, >0 that many items
var n0 int
if len(n) > 0 {
n0 = n[0]
}
if n0 < 0 {
n0 = 0
}
// get possible item from send thread
var item T
var itemValid bool
if nb.isRunningThread {
select {
case <-nb.closesOnThreadSend:
case item, itemValid = <-nb.closableChan.ch:
}
}
// allocate and populate allItems
var itemLength int
if itemValid {
itemLength = 1
}
nq := len(nb.sendQueue)
// cap n to set n0
if n0 != 0 && nq+itemLength > n0 {
nq = n0 - itemLength
}
allItems = make([]T, nq+itemLength)
// possible item from channel
if itemValid {
allItems[0] = item
}
// items from sendQueue
if nq > 0 {
copy(allItems[itemLength:], nb.sendQueue)
pslices.TrimLeft(&nb.sendQueue, nq)
nb.unsentCount -= nq
}
return
}
// Count returns number of unsent values
func (nb *NBChan[T]) Count() (unsentCount int) {
nb.stateLock.Lock()
defer nb.stateLock.Unlock()
return nb.unsentCount
}
// Close orders the channel to close once pending sends complete.
// Close is thread-safe, non-blocking and panic-free.
func (nb *NBChan[T]) Close() (didClose bool) {
if nb.isCloseInvoked.IsTrue() {
return // Close was already invoked atomic performance
}
nb.stateLock.Lock()
defer nb.stateLock.Unlock()
if !nb.isCloseInvoked.Set() {
return // Close was already invoked
}
if nb.isRunningThread {
return // there is a pending thread that will execute close on exit
}
didClose, _ = nb.close()
return
}
// DidClose indicates if Close was invoked
// - the channel may remain open until the last item has been read
// - or CloseNow is invoked
func (nb *NBChan[T]) DidClose() (didClose bool) {
return nb.isCloseInvoked.IsTrue()
}
// IsClosed indicates whether the channel has actually closed.
func (nb *NBChan[T]) IsClosed() (isClosed bool) {
return nb.closableChan.IsClosed()
}
// WaitForClose block if until the channel is closed and empty
// - if Close is not invoked, WaitForClose blocks indefinitely
func (nb *NBChan[T]) WaitForClose() {
if nb.closableChan.IsClosed() {
return
}
if !nb.isWaitForCloseInitialized.IsTrue() {
nb.initWaitForClose() // ensure waitForClose state is valid
}
nb.waitForClose.Wait()
}
// CloseNow closes without waiting for sends to complete.
// Close does not panic.
// Close is thread-safe.
// Close does not return until the channel is closed.
// Upon return, all invocations have a possible close error in err.
// if errp is non-nil, it is updated with error status
func (nb *NBChan[T]) CloseNow(errp ...*error) (didClose bool, err error) {
if nb.closableChan.IsClosed() {
return // channel is already closed
}
nb.stateLock.Lock()
defer nb.stateLock.Unlock()
nb.isCloseInvoked.Set()
// discard pending data
var length = len(nb.sendQueue)
nb.sendQueue = nil
if length > 0 {
nb.unsentCount -= length
}
// empty the channel
if nb.isRunningThread {
select {
case <-nb.closesOnThreadSend:
case <-nb.Ch():
}
}
didClose, err = nb.close()
if len(errp) > 0 {
if errp0 := errp[0]; errp0 != nil {
*errp0 = err
}
}
return
}
// startThread launches the send thread
// - must be invoked inside the lock
func (nb *NBChan[T]) startThread(value T) {
nb.isRunningThread = true
nb.closesOnThreadSend = make(chan struct{})
go nb.sendThread(value) // send err in new thread
}
// sendThread operates outside the lock for as long as there are items to send
func (nb *NBChan[T]) sendThread(value T) {
defer Recover(Annotation(), nil, func(err error) {
if pruntime.IsSendOnClosedChannel(err) {
return // ignore if the channel was or became closed
}
nb.AddError(err)
})
var ok bool
for {
nb.threadSend(value)
if value, ok = nb.valueToSend(); !ok {
return
}
}
}
func (nb *NBChan[T]) threadSend(value T) {
defer close(nb.closesOnThreadSend)
nb.closableChan.Ch() <- value // may block or panic
}
func (nb *NBChan[T]) valueToSend() (value T, ok bool) {
nb.stateLock.Lock()
defer nb.stateLock.Unlock()
// clear isRunningThread inside the lock
// possibly invoke close
defer nb.sendThreadDefer(&ok)
// count the item just sent
nb.unsentCount--
// no more values: end thread
if ok = len(nb.sendQueue) != 0; !ok {
return // thread to exit: ok == false return
}
// send next value in queue
value = nb.sendQueue[0]
pslices.TrimLeft(&nb.sendQueue, 1)
// closesOnThreadSend is re-created every time prior to exiting the lock
// when a value is provided for the send thread
nb.closesOnThreadSend = make(chan struct{})
return
}
// sendThreadDefer is invoked when send thread is about to exit
// - sendThreadDefer is invoked inside the lock
func (nb *NBChan[T]) sendThreadDefer(ok *bool) {
if *ok {
return // thread is not exiting
}
nb.isRunningThread = false
if nb.isCloseInvoked.IsTrue() { // Close() was invoked after thread started
nb.close()
}
}
// close closes the underlying channel
// - close is invoked inside the lock
func (nb *NBChan[T]) close() (didClose bool, err error) {
if didClose, err = nb.closableChan.Close(); !didClose {
return
}
if err != nil {
nb.AddError(err) // store possible close error
}
if nb.isWaitForCloseInitialized.IsTrue() {
nb.waitForClose.Done()
}
return
}
// initWaitForClose ensures that waitForClose is valid
func (nb *NBChan[T]) initWaitForClose() {
nb.stateLock.Lock()
defer nb.stateLock.Unlock()
if nb.closableChan.IsClosed() {
return // channel already closed
}
if !nb.isWaitForCloseInitialized.Set() {
return // was already initialized return
}
nb.waitForClose.Add(1) // has to wait for close to occur
}