-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring_buffer.go
582 lines (466 loc) · 14.4 KB
/
ring_buffer.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package ringbuffer
import (
"context"
"errors"
"io"
"log"
"reflect"
"runtime"
"unsafe"
)
var (
ErrTooManyDataToWrite = errors.New("too many data to write")
ErrIsEmpty = errors.New("ringbuffer is empty")
ErrAcquireLock = errors.New("no lock to acquire")
ErrNotEnoughData = errors.New("not enough data")
ErrReadFromInProgress = errors.New("ReadFrom in progress")
ErrWriteToInProgress = errors.New("WriteTo in progress")
)
type RingBuffer struct {
buf []byte
size int // Capacity of underlying buffer.
l int // Available bytes to read.
r int // Next position to read.
w int // Next position to write.
isRF bool // Is ReadFrom() in progress.
isWT bool // Is WriteTo() in progress.
isRCNotifyBlocks bool // is WriteTo notify blocking, useful if you want to controll how frequently data is written to supplied io.Writer.
isWCNotifyBlocks bool // is ReadFrom() notify blocking, useful if you want to controll how frequently data is read from supplied io.Reader.
WC chan interface{} // ReadFrom() notify about new data with this channel. (Public)
RC chan interface{} // WriteTo() notify about releasing space with this channel. (Public)
rC chan interface{} // Readers notify about releasing space with this channel.
wC chan interface{} // Writers notify about new data with this channel.
mu Mutex
}
// New returns a new RingBuffer whose buffer has the given size.
func New(size int) *RingBuffer {
return NewExtended(size, false, false)
}
// New returns a new RingBuffer whose buffer has the given size.
// Also you can specify if notify is blocking for ReadFrom and WriteTo.
func NewExtended(size int, isRCNotifyBlocks bool, isWCNotifyBlocks bool) *RingBuffer {
return &RingBuffer{
buf: make([]byte, size*2), // Double the size.
size: size,
isRCNotifyBlocks: isRCNotifyBlocks,
isWCNotifyBlocks: isWCNotifyBlocks,
WC: make(chan interface{}, 1),
RC: make(chan interface{}, 1),
rC: make(chan interface{}, 1),
wC: make(chan interface{}, 1),
}
}
// Reset the read pointer and writer pointer to zero.
func (r *RingBuffer) Reset() {
r.mu.Lock()
defer r.mu.Unlock()
if r.isRF {
log.Panic("reseting buffer while incomplete ReadFrom()")
}
if r.isWT {
log.Panic("reseting buffer while incomplete WriteTo()")
}
r.l = 0
r.r = 0
r.w = 0
// Clear pending channels data, if any.
for {
select {
case <-r.WC:
case <-r.RC:
case <-r.rC:
case <-r.wC:
default:
return
}
}
}
func (r *RingBuffer) noticeFromRead() {
// Non blocking.
select {
case r.rC <- nil:
default:
}
}
func (r *RingBuffer) noticeFromWriteTo(ctx context.Context) error {
// If we could notify without blocking - do it.
select {
case r.RC <- nil:
default:
if r.isRCNotifyBlocks {
select {
case r.RC <- nil:
case <-ctx.Done():
}
}
}
return ctx.Err()
}
func (r *RingBuffer) noticeFromWrite() {
// Non blocking.
select {
case r.wC <- nil:
default:
}
}
func (r *RingBuffer) noticeFromReadFrom(ctx context.Context) error {
// If we could notify without blocking - do it.
select {
case r.WC <- nil:
default:
if r.isWCNotifyBlocks {
select {
case r.WC <- nil:
case <-ctx.Done():
}
}
}
return ctx.Err()
}
// Lock locks mutex.
// Should ONLY be used with functions like BytesNoLock().
func (r *RingBuffer) Lock() {
r.mu.Lock()
}
// TryLock attempts to lock mutex, returns true if succeed.
// Should ONLY be used with functions like BytesNoLock().
func (r *RingBuffer) TryLock() bool {
return r.mu.TryLock()
}
// Unlock unlocks mutex.
// Should ONLY be used with functions like BytesNoLock().
func (r *RingBuffer) Unlock() {
r.mu.Unlock()
}
// Capacity returns the size of the underlying buffer.
func (r *RingBuffer) Capacity() int {
return r.size
}
// Length returns the length of available read bytes.
func (r *RingBuffer) Length() int {
r.mu.Lock()
defer r.mu.Unlock()
return r.l
}
// IsEmpty returns true if there is nothing to read.
func (r *RingBuffer) IsEmpty() bool {
r.mu.Lock()
defer r.mu.Unlock()
return r.l == 0
}
// Available returns the length of available bytes to write.
func (r *RingBuffer) Available() int {
r.mu.Lock()
defer r.mu.Unlock()
return r.available()
}
func (r *RingBuffer) available() int {
return r.size - r.l
}
// IsFull returns true if there is no more space for write.
func (r *RingBuffer) IsFull() bool {
r.mu.Lock()
defer r.mu.Unlock()
return r.available() == 0
}
// Converting string to bytes without allocating memory.
func stringToBytes(s string) []byte {
// create an actual slice
bytes := make([]byte, 0, 0)
// create the string and slice headers by casting. Obtain pointers to the
// headers to be able to change the slice header properties in the next step
stringHeader := (*reflect.StringHeader)(unsafe.Pointer(&s))
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
// set the slice's length and capacity temporarily to zero (this is actually
// unnecessary here because the slice is already initialized as zero, but if
// you are reusing a different slice this is important
sliceHeader.Len = 0
sliceHeader.Cap = 0
// change the slice header data address
sliceHeader.Data = stringHeader.Data
// set the slice capacity and length to the string length
sliceHeader.Cap = stringHeader.Len
sliceHeader.Len = stringHeader.Len
// use the keep alive dummy function to make sure the original string s is not
// freed up until this point
runtime.KeepAlive(s)
return bytes
}
// WriteString writes the contents of the string s to buffer, which accepts a slice of bytes.
func (r *RingBuffer) WriteString(s string) (n int, err error) {
return r.Write(stringToBytes(s))
}
// TryWriteString writes the contents of the string s to buffer, which accepts a slice of bytes.
// It is not blocking.
// If it has not succeeded to acquire the lock, it return 0 as n and ErrAcquireLock.
func (r *RingBuffer) TryWriteString(s string) (n int, err error) {
return r.TryWrite(stringToBytes(s))
}
// Write writes len(p) bytes from p to the underlying buf.
// It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early.
// Write returns a non-nil error if it returns n < len(p).
// Write must not modify the slice data, even temporarily.
func (r *RingBuffer) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
r.mu.Lock()
defer r.mu.Unlock()
return r.write(p, false)
}
// Same as Write but we do not perform short write, we write all or nothing.
func (r *RingBuffer) WriteFull(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
r.mu.Lock()
defer r.mu.Unlock()
return r.write(p, true)
}
// TryWrite writes len(p) bytes from p to the underlying buf like Write, but it is not blocking.
// If it has not succeeded to acquire the lock, it return 0 as n and ErrAcquireLock.
func (r *RingBuffer) TryWrite(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
ok := r.mu.TryLock()
if !ok {
return 0, ErrAcquireLock
}
defer r.mu.Unlock()
return r.write(p, false)
}
func (r *RingBuffer) write(p []byte, full bool) (n int, err error) {
if r.isRF {
return 0, ErrReadFromInProgress
}
avail := r.available()
if avail == 0 {
return 0, ErrTooManyDataToWrite
}
n = len(p) // How much bytes we want to write.
// Check if we have enough space for write.
if n > avail {
err = ErrTooManyDataToWrite
// If full is true then we do not perform short write, we write all or nothing.
if full {
return 0, err
}
n = avail
p = p[:n]
}
for n_tmp := n; n_tmp > 0; {
bytesToWrite := r.size - r.w
if bytesToWrite > n_tmp {
bytesToWrite = n_tmp
}
copy(r.buf[r.w:], p[:bytesToWrite])
copy(r.buf[r.w+r.size:], p[:bytesToWrite])
r.w = (r.w + bytesToWrite) % r.size
r.l += bytesToWrite
p = p[bytesToWrite:]
n_tmp -= bytesToWrite
}
r.noticeFromWrite() // Let WriteTo know what we got some data.
return n, err
}
// ReadFrom reads data from rd until EOF or error.
// The return value n is the number of bytes read.
// Any error except EOF encountered during the read is also returned.
func (r *RingBuffer) ReadFromWithContext(ctx context.Context, rd io.Reader) (int64, error) {
r.mu.Lock()
if r.isRF {
defer r.mu.Unlock()
return 0, ErrReadFromInProgress
}
r.isRF = true // Let other writers know they can't do anything until we done.
r.mu.Unlock()
defer func() {
r.mu.Lock()
r.isRF = false // Release isRF when we finally leave function.
r.mu.Unlock()
}()
var len int64
for {
avail := r.Available()
// Check if we have space for writing.
if avail == 0 {
// We does not have space, wait for reader to release some space or context cancelation.
select {
case <-r.rC:
continue // Reader possibly released space, lets check it.
case <-ctx.Done():
return len, ctx.Err()
}
}
// We access 'r.w' without lock since other writers should not touch it because r.isRF is still true.
wrap := r.size - r.w // How much bytes we can write before wrapping.
if wrap > avail {
wrap = avail // Cap wrap by available bytes.
}
b := r.buf[r.w : r.w+wrap] // Buffer for read.
n, rErr := rd.Read(b)
copy(r.buf[r.w+r.size:], b[:n]) // Copy what we read to the second part of underlying buffer.
len += int64(n)
r.mu.Lock()
r.w = (r.w + n) % r.size
r.l += n
r.noticeFromWrite() // Let WriteTo know what we got some data.
r.mu.Unlock()
// May block.
if err := r.noticeFromReadFrom(ctx); err != nil {
return len, err
}
if rErr == io.EOF {
return len, nil
} else if rErr != nil {
return len, rErr
}
}
}
// ReadFrom reads data from rd until EOF or error.
// The return value n is the number of bytes read.
// Any error except EOF encountered during the read is also returned.
func (r *RingBuffer) ReadFrom(rd io.Reader) (int64, error) {
return r.ReadFromWithContext(context.Background(), rd)
}
// Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p))
// and any error encountered. Even if Read returns n < len(p), it may use all of p as scratch space during the call.
// If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.
// When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes,
// it returns the number of bytes read. It may return the (non-nil) error from the same call or
// return the error (and n == 0) from a subsequent call.
// Callers should always process the n > 0 bytes returned before considering the error err.
// Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors.
func (r *RingBuffer) Read(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
r.mu.Lock()
defer r.mu.Unlock()
return r.read(p)
}
// TryRead read up to len(p) bytes into p like Read but it is not blocking.
// If it has not succeeded to acquire the lock, it return 0 as n and ErrAcquireLock.
func (r *RingBuffer) TryRead(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
ok := r.mu.TryLock()
if !ok {
return 0, ErrAcquireLock
}
defer r.mu.Unlock()
return r.read(p)
}
func (r *RingBuffer) read(p []byte) (n int, err error) {
if r.isWT {
return 0, ErrWriteToInProgress
}
if r.l == 0 {
return 0, ErrIsEmpty
}
n = r.l
if n > len(p) {
n = len(p) // Read no more than user requested with his buffer.
}
// We do not have to do wrapping for reading since we use two times more memory than usual ring buffer,
// so it simple as this.
copy(p, r.buf[r.r:r.r+n])
r.r = (r.r + n) % r.size
r.l -= n
r.noticeFromRead() // Let ReadFrom know we release some space for writing.
return n, err
}
// Discard skips the next n bytes for reading.
func (r *RingBuffer) Discard(n int) (err error) {
if n == 0 {
return nil
}
r.mu.Lock()
defer r.mu.Unlock()
if r.isWT {
return ErrWriteToInProgress
}
if r.l < n {
return ErrNotEnoughData
}
r.r = (r.r + n) % r.size
r.l -= n
r.noticeFromRead() // Let ReadFrom know we release some space for writing.
return nil
}
func (r *RingBuffer) WriteToWithContext(ctx context.Context, wr io.Writer) (int64, error) {
r.mu.Lock()
if r.isWT {
defer r.mu.Unlock()
return 0, ErrWriteToInProgress
}
r.isWT = true // Let other readers know they can't do anything until we done.
r.mu.Unlock()
defer func() {
r.mu.Lock()
r.isWT = false // Release isWT when we finally leave function.
r.mu.Unlock()
}()
var len int64
for {
l := r.Length()
// Check if we have data for reading.
if l == 0 {
// We does not have data, wait for writer or context cancelation.
select {
case <-r.wC:
continue // Writer tell us what we have data, lets check it.
case <-ctx.Done():
return len, ctx.Err()
}
}
// We access 'r.r' without lock since other readers should not touch it because r.isWT is still true.
b := r.buf[r.r : r.r+l]
n, wErr := wr.Write(b)
len += int64(n)
r.mu.Lock()
r.r = (r.r + n) % r.size
r.l -= n
r.noticeFromRead() // Let ReadFrom know we release some space for writing.
r.mu.Unlock()
// May block.
if err := r.noticeFromWriteTo(ctx); err != nil {
return len, err
}
if wErr != nil {
return len, wErr
}
}
}
func (r *RingBuffer) WriteTo(wr io.Writer) (n int64, err error) {
return r.WriteToWithContext(context.Background(), wr)
}
// Bytes returns all available read bytes. It does not move the read pointer and only copy the available data.
func (r *RingBuffer) Bytes() []byte {
r.mu.Lock()
defer r.mu.Unlock()
buf := make([]byte, r.l)
copy(buf, r.buf[r.r:r.r+r.l])
return buf
}
// BytesNoLock returns all available read bytes. It does not move the read pointer.
// Unlike Bytes() it does not make a copy and returns underlying buffer.
// It does not use locking so you MUST lock ringbuffer yourself!!!
func (r *RingBuffer) BytesNoLock() []byte {
return r.buf[r.r : r.r+r.l]
}
// BytesOneReader returns all available read bytes. It does not move the read pointer.
// Unlike Bytes() it does not make a copy and returns underlying buffer.
// BytesOneReader() expects what you do NOT USE ANY Read() or Discard() while you use returned buffer.
// It is safe to use Write() while you use returned buffer.
func (r *RingBuffer) BytesOneReader() []byte {
r.mu.Lock()
defer r.mu.Unlock()
// Minor protection.
if r.isWT {
log.Panic(ErrWriteToInProgress)
}
return r.buf[r.r : r.r+r.l]
}