forked from zentures/surgemq
/
buffer.go
369 lines (308 loc) · 7.23 KB
/
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
// Copyright (c) 2014 The SurgeMQ Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package service
import (
"bufio"
"encoding/binary"
"errors"
"fmt"
"io"
"sync"
"sync/atomic"
// "time"
)
var (
bufcnt int64
DefaultBufferSize int64
DeviceInBufferSize int64
DeviceOutBufferSize int64
MasterInBufferSize int64
MasterOutBufferSize int64
)
type sequence struct {
// The current position of the producer or consumer
cursor,
// The previous known position of the consumer (if producer) or producer (if consumer)
gate,
// These are fillers to pad the cache line, which is generally 64 bytes
p2, p3, p4, p5, p6, p7 int64
}
func newSequence() *sequence {
return &sequence{}
}
func (this *sequence) get() int64 {
return atomic.LoadInt64(&this.cursor)
}
func (this *sequence) set(seq int64) {
atomic.StoreInt64(&this.cursor, seq)
}
type buffer struct {
id int64
readIndex int64 //读序号
writeIndex int64 //写序号
buf []*[]byte
size int64
mask int64
done int64
pcond *sync.Cond
ccond *sync.Cond
_server *Server
b []byte //readfrom中的临时数组,为反复使用不必创建新数组
}
func newBuffer(size int64, server *Server) (*buffer, error) {
if size < 0 {
return nil, bufio.ErrNegativeCount
}
if size == 0 {
size = DefaultBufferSize
}
if !powerOfTwo64(size) {
fmt.Printf("Size must be power of two. Try %d.", roundUpPowerOfTwo64(size))
return nil, fmt.Errorf("Size must be power of two. Try %d.", roundUpPowerOfTwo64(size))
}
return &buffer{
id: atomic.AddInt64(&bufcnt, 1),
readIndex: int64(0),
writeIndex: int64(0),
buf: make([]*[]byte, size),
size: size,
mask: size - 1,
pcond: sync.NewCond(new(sync.Mutex)),
ccond: sync.NewCond(new(sync.Mutex)),
_server: server,
b: make([]byte, 5),
}, nil
}
/**
获取当前读序号
*/
func (this *buffer) GetCurrentReadIndex() int64 {
return atomic.LoadInt64(&this.readIndex)
}
/**
获取当前写序号
*/
func (this *buffer) GetCurrentWriteIndex() int64 {
return atomic.LoadInt64(&this.writeIndex)
}
func (this *buffer) ID() int64 {
return this.id
}
func (this *buffer) Close() error {
atomic.StoreInt64(&this.done, 1)
this.pcond.L.Lock()
for i := int64(0); i < this.size; i++ {
if this.buf[i] != nil {
this._server.DestoryBytes(*(this.buf[i]))
this.buf[i] = nil
}
}
this._server = nil
this.pcond.L.Unlock()
this.pcond.L.Lock()
this.ccond.Signal()
this.pcond.L.Unlock()
this.ccond.L.Lock()
this.pcond.Signal()
this.ccond.L.Unlock()
return nil
}
/**
读取ringbuffer指定的buffer指针,返回该指针并清空ringbuffer该位置存在的指针内容,以及将读序号加1
*/
func (this *buffer) ReadBuffer() (p *[]byte, ok bool) {
if this.isDone() {
return nil, false
}
this.ccond.L.Lock()
defer func() {
this.pcond.Signal()
this.ccond.L.Unlock()
}()
ok = false
p = nil
readIndex := this.GetCurrentReadIndex()
writeIndex := this.GetCurrentWriteIndex()
for {
if this.isDone() {
return nil, false
}
writeIndex = this.GetCurrentWriteIndex()
if readIndex >= writeIndex {
this.pcond.Signal()
this.ccond.Wait()
} else {
break
}
}
index := readIndex & this.mask //替代求模
p = this.buf[index]
this.buf[index] = nil
atomic.AddInt64(&this.readIndex, int64(1))
if p != nil {
ok = true
}
return p, ok
}
/**
写入ringbuffer指针,以及将写序号加1
*/
func (this *buffer) WriteBuffer(in *[]byte) (ok bool) {
if this.isDone() {
return false
}
this.pcond.L.Lock()
defer func() {
this.ccond.Signal()
this.pcond.L.Unlock()
}()
ok = false
readIndex := this.GetCurrentReadIndex()
writeIndex := this.GetCurrentWriteIndex()
for {
if this.isDone() {
return false
}
readIndex = this.GetCurrentReadIndex()
if writeIndex >= readIndex && writeIndex-readIndex >= this.size {
this.ccond.Signal()
this.pcond.Wait()
} else {
break
}
}
index := writeIndex & this.mask //替代求模
this.buf[index] = in
atomic.AddInt64(&this.writeIndex, int64(1))
ok = true
return ok
}
/**
修改 尽量减少数据的创建
*/
func (this *buffer) ReadFrom(r io.Reader) (int64, error) {
defer this.Close()
total := int64(0)
for {
// NOTE 这个睡眠十秒是为了解决什么问题来着? 看起来似乎不是很必要
// time.Sleep(10 * time.Millisecond)
if this.isDone() {
return total, io.EOF
}
var write_bytes []byte
n, err := r.Read(this.b[0:1])
if err != nil {
return total, err
}
total += int64(n)
max_cnt := 1
for {
if this.isDone() {
return total, io.EOF
}
// If we have read 5 bytes and still not done, then there's a problem.
if max_cnt > 4 {
return 0, fmt.Errorf("sendrecv/peekMessageSize: 4th byte of remaining length has continuation bit set")
}
_, err := r.Read(this.b[max_cnt:(max_cnt + 1)])
if err != nil {
return total, err
}
if this.b[max_cnt] >= 0x80 {
max_cnt++
} else {
break
}
}
remlen, m := binary.Uvarint(this.b[1 : max_cnt+1])
remlen_tmp := int64(remlen)
start_ := int64(1) + int64(m)
total_tmp := remlen_tmp + start_
//write_bytes = make([]byte, total_tmp)
write_bytes = this._server.CreateAndGetBytes(total_tmp)
copy(write_bytes[0:m+1], this.b[0:m+1])
nlen := int64(0)
readblock := int64(1024)
for nlen < remlen_tmp {
if this.isDone() {
return total, io.EOF
}
tmpm := remlen_tmp - nlen
start__ := start_ + nlen
b_ := write_bytes[start__:]
if tmpm > readblock {
b_ = write_bytes[start__:(start__ + readblock)]
}
//b_ := make([]byte, remlen)
n, err = r.Read(b_[0:])
b_ = nil
if err != nil {
return total, err
}
nlen += int64(n)
total += int64(n)
}
ok := this.WriteBuffer(&write_bytes)
if !ok {
return total, errors.New("write ringbuffer failed")
}
}
}
func (this *buffer) WriteTo(w io.Writer) (int64, error) {
defer this.Close()
total := int64(0)
for {
if this.isDone() {
return total, io.EOF
}
p, ok := this.ReadBuffer()
if !ok {
return total, errors.New("read buffer failed")
}
// There's some data, let's process it first
if len(*p) > 0 {
_p := this._server.getRealBytes(p)
n, err := w.Write(_p)
total += int64(n)
//Log.Debugc(func() string{ return fmt.Sprintf("Wrote %d bytes, totaling %d bytes", n, total)})
//清理指针p
//p = nil
_p = nil
this._server.DestoryBytes(*p)
if err != nil {
return total, err
}
}
}
}
func (this *buffer) isDone() bool {
if this == nil || atomic.LoadInt64(&this.done) == 1 {
return true
}
return false
}
func powerOfTwo64(n int64) bool {
return n != 0 && (n&(n-1)) == 0
}
func roundUpPowerOfTwo64(n int64) int64 {
n--
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n |= n >> 32
n++
return n
}