-
Notifications
You must be signed in to change notification settings - Fork 261
/
ring_buffer.go
190 lines (162 loc) · 4.68 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
package wknet
import (
"io"
rbPool "github.com/WuKongIM/WuKongIM/pkg/pool/ringbuffer"
"github.com/WuKongIM/WuKongIM/pkg/ring"
)
// RingBuffer is the elastic wrapper of ring.Buffer.
type RingBuffer struct {
rb *ring.Buffer
}
func (b *RingBuffer) instance() *ring.Buffer {
if b.rb == nil {
b.rb = rbPool.Get()
}
return b.rb
}
// Done checks and returns the internal ring-buffer to pool.
func (b *RingBuffer) Done() {
if b.rb != nil {
rbPool.Put(b.rb)
b.rb = nil
}
}
func (b *RingBuffer) done() {
if b.rb != nil && b.rb.IsEmpty() {
rbPool.Put(b.rb)
b.rb = nil
}
}
// Peek returns the next n bytes without advancing the read pointer,
// it returns all bytes when n <= 0.
func (b *RingBuffer) Peek(n int) (head []byte, tail []byte) {
if b.rb == nil {
return nil, nil
}
return b.rb.Peek(n)
}
// Discard skips the next n bytes by advancing the read pointer.
func (b *RingBuffer) Discard(n int) (int, error) {
if b.rb == nil {
return 0, ring.ErrIsEmpty
}
defer b.done()
return b.rb.Discard(n)
}
// 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 (b *RingBuffer) Read(p []byte) (int, error) {
if b.rb == nil {
return 0, ring.ErrIsEmpty
}
defer b.done()
return b.rb.Read(p)
}
// ReadByte reads and returns the next byte from the input or ErrIsEmpty.
func (b *RingBuffer) ReadByte() (byte, error) {
if b.rb == nil {
return 0, ring.ErrIsEmpty
}
defer b.done()
return b.rb.ReadByte()
}
// Write writes len(p) bytes from p to the underlying buf.
// It returns the number of bytes written from p (n == len(p) > 0) and any error encountered that caused the write to
// stop early.
// If the length of p is greater than the writable capacity of this ring-buffer, it will allocate more memory to
// this ring-buffer.
// Write must not modify the slice data, even temporarily.
func (b *RingBuffer) Write(p []byte) (int, error) {
if len(p) == 0 {
return 0, nil
}
return b.instance().Write(p)
}
// WriteByte writes one byte into buffer.
func (b *RingBuffer) WriteByte(c byte) error {
return b.instance().WriteByte(c)
}
// Buffered returns the length of available bytes to read.
func (b *RingBuffer) Buffered() int {
if b.rb == nil {
return 0
}
return b.rb.Buffered()
}
// Len returns the length of the underlying buffer.
func (b *RingBuffer) Len() int {
if b.rb == nil {
return 0
}
return b.rb.Len()
}
// Cap returns the size of the underlying buffer.
func (b *RingBuffer) Cap() int {
if b.rb == nil {
return 0
}
return b.rb.Cap()
}
// Available returns the length of available bytes to write.
func (b *RingBuffer) Available() int {
if b.rb == nil {
return 0
}
return b.rb.Available()
}
// WriteString writes the contents of the string s to buffer, which accepts a slice of bytes.
func (b *RingBuffer) WriteString(s string) (int, error) {
if len(s) == 0 {
return 0, nil
}
return b.instance().WriteString(s)
}
// Bytes returns all available read bytes. It does not move the read pointer and only copy the available data.
func (b *RingBuffer) Bytes() []byte {
if b.rb == nil {
return nil
}
return b.rb.Bytes()
}
// ReadFrom implements io.ReaderFrom.
func (b *RingBuffer) ReadFrom(r io.Reader) (int64, error) {
return b.instance().ReadFrom(r)
}
// WriteTo implements io.WriterTo.
func (b *RingBuffer) WriteTo(w io.Writer) (int64, error) {
if b.rb == nil {
return 0, ring.ErrIsEmpty
}
defer b.done()
return b.instance().WriteTo(w)
}
// IsFull tells if this ring-buffer is full.
func (b *RingBuffer) IsFull() bool {
if b.rb == nil {
return false
}
return b.rb.IsFull()
}
// IsEmpty tells if this ring-buffer is empty.
func (b *RingBuffer) IsEmpty() bool {
if b.rb == nil {
return true
}
return b.rb.IsEmpty()
}
// Reset the read pointer and write pointer to zero.
func (b *RingBuffer) Reset() {
if b.rb == nil {
return
}
b.rb.Reset()
}