-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecyclable_chan.go
248 lines (192 loc) · 4.15 KB
/
recyclable_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
package base
import (
"errors"
"go.uber.org/atomic"
"io"
)
var (
ErrorChannelIsFull = errors.New("channel is full")
)
type RecyclableChan struct {
closed bool
desc string
extData any
filledChan chan []byte
pendingBuffer []byte
pendingData []byte
isReadingAsPkt bool
shouldDropWhileError bool
readReady bool
onReadReady func()
flag1 atomic.Int32
flag2 atomic.Int32
}
func NewRecyclableChan(desc string, maxPktCnt uint32, extData any) *RecyclableChan {
return &RecyclableChan{
false, desc, extData, make(chan []byte, maxPktCnt),
nil, nil, false, false, false, nil,
atomic.Int32{}, atomic.Int32{},
}
}
func (c *RecyclableChan) Desc() string {
return c.desc
}
func (c *RecyclableChan) ExtData() any {
return c.extData
}
func (c *RecyclableChan) SetExtData(extData any) {
c.extData = extData
}
func (c *RecyclableChan) SetReadingAsPkt(asPkt bool) *RecyclableChan {
c.isReadingAsPkt = asPkt
return c
}
func (c *RecyclableChan) SetShouldDropWhileError(should bool) *RecyclableChan {
c.shouldDropWhileError = should
return c
}
func (c *RecyclableChan) SetOnReadReady(callback func()) *RecyclableChan {
c.onReadReady = callback
return c
}
func (c *RecyclableChan) ReadStream(b []byte) (n int, err error) {
if err = c.preRead(); err != nil {
return
}
return c.readStream2Buffer(b)
}
func (c *RecyclableChan) ReadPacket() (b []byte, err error) {
if err = c.preRead(); err != nil {
return
}
return c.readPkt()
}
func (c *RecyclableChan) IsClosed() bool {
return c.closed
}
func (c *RecyclableChan) IsReadReady() bool {
return c.readReady
}
func (c *RecyclableChan) Flag1() int32 {
return c.flag1.Load()
}
func (c *RecyclableChan) SetFlag1(flag1 int32) {
c.flag1.Store(flag1)
}
func (c *RecyclableChan) Flag2() int32 {
return c.flag2.Load()
}
func (c *RecyclableChan) SetFlag2(flag2 int32) {
c.flag2.Store(flag2)
}
//////////////////////////// implementation of io.Writer
func (c *RecyclableChan) Write(data []byte) (n int, err error) {
if c.closed {
return 0, io.EOF
}
if len(c.filledChan) == cap(c.filledChan) {
return 0, ErrorChannelIsFull
}
n = len(data)
buffer := make([]byte, 0, n)
buffer = append(buffer, data...)
c.filledChan <- buffer
return
}
//////////////////////////// implementation of io.Reader
func (c *RecyclableChan) Read(b []byte) (n int, err error) {
if err = c.preRead(); err != nil {
return
}
if c.isReadingAsPkt {
return c.readPkt2Buffer(b)
} else {
return c.readStream2Buffer(b)
}
}
//////////////////////////// implementation of io.Closer
func (c *RecyclableChan) Close() error {
if c != nil && !c.closed {
c.closed = true
//buffer := make([]byte, 0, 1)
//c.filledChan <- buffer
close(c.filledChan)
}
return nil
}
//////////////////////////// private
func (c *RecyclableChan) preRead() (err error) {
if c.closed {
return io.EOF
}
if !c.readReady {
c.readReady = true
if c.onReadReady != nil {
c.onReadReady()
}
}
if c.closed {
return io.EOF
} else {
return nil
}
}
func (c *RecyclableChan) readPkt() (b []byte, err error) {
if c.closed {
return nil, io.EOF
}
buffer := <-c.filledChan
if c.closed {
return nil, io.EOF
}
b = append(b, buffer...)
return
}
func (c *RecyclableChan) readPkt2Buffer(b []byte) (n int, err error) {
if c.closed {
return 0, io.EOF
}
var buffer []byte
if c.pendingBuffer == nil {
buffer = <-c.filledChan
} else {
buffer = c.pendingBuffer
c.pendingBuffer = nil
}
if c.closed {
return 0, io.EOF
}
if len(b) < len(buffer) {
if !c.shouldDropWhileError {
c.pendingBuffer = buffer
}
return 0, io.ErrShortBuffer
}
n = copy(b, buffer)
return
}
func (c *RecyclableChan) readStream2Buffer(b []byte) (n int, err error) {
if c.closed {
return 0, io.EOF
}
var buffer []byte
if c.pendingData != nil {
buffer = c.pendingData
} else {
buffer = <-c.filledChan
if c.closed {
return 0, io.EOF
}
}
n = copy(b, buffer)
if n == len(buffer) { // buffer or pendingBuffer has been used up
c.pendingData = nil
c.pendingBuffer = nil
} else { // there is still some pending bytes in buffer
if c.pendingBuffer == nil {
c.pendingBuffer = buffer
}
c.pendingData = buffer[n:]
}
return
}