-
Notifications
You must be signed in to change notification settings - Fork 635
/
buffer_pool.go
94 lines (83 loc) · 2.2 KB
/
buffer_pool.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
package buf
import (
"fmt"
"golang.org/x/net/context"
"golang.org/x/time/rate"
"sync"
"sync/atomic"
"github.com/chubaofs/chubaofs/util"
)
const (
HeaderBufferPoolSize = 8192
)
var tinyBuffersTotalLimit int64 = 4096
var tinyBuffersCount int64
var buffersRateLimit = rate.NewLimiter(rate.Limit(16), 16)
func NewTinyBufferPool() *sync.Pool {
return &sync.Pool{
New: func() interface{} {
if atomic.LoadInt64(&tinyBuffersCount) >= tinyBuffersTotalLimit {
ctx := context.Background()
buffersRateLimit.Wait(ctx)
}
return make([]byte, util.DefaultTinySizeLimit)
},
}
}
// BufferPool defines the struct of a buffered pool with 4 objects.
type BufferPool struct {
pools [2]chan []byte
tinyPool *sync.Pool
}
// NewBufferPool returns a new buffered pool.
func NewBufferPool() (bufferP *BufferPool) {
bufferP = &BufferPool{}
bufferP.pools[0] = make(chan []byte, HeaderBufferPoolSize)
bufferP.pools[1] = make(chan []byte, HeaderBufferPoolSize)
bufferP.tinyPool = NewTinyBufferPool()
return bufferP
}
func (bufferP *BufferPool) get(index int, size int) (data []byte) {
select {
case data = <-bufferP.pools[index]:
return
default:
return make([]byte, size)
}
}
// Get returns the data based on the given size. Different size corresponds to different object in the pool.
func (bufferP *BufferPool) Get(size int) (data []byte, err error) {
if size == util.PacketHeaderSize {
return bufferP.get(0, size), nil
} else if size == util.BlockSize {
return bufferP.get(1, size), nil
} else if size == util.DefaultTinySizeLimit {
atomic.AddInt64(&tinyBuffersCount, 1)
return bufferP.tinyPool.Get().([]byte), nil
}
return nil, fmt.Errorf("can only support 45 or 65536 bytes")
}
func (bufferP *BufferPool) put(index int, data []byte) {
select {
case bufferP.pools[index] <- data:
return
default:
return
}
}
// Put puts the given data into the buffer pool.
func (bufferP *BufferPool) Put(data []byte) {
if data == nil {
return
}
size := len(data)
if size == util.PacketHeaderSize {
bufferP.put(0, data)
} else if size == util.BlockSize {
bufferP.put(1, data)
} else if size == util.DefaultTinySizeLimit {
bufferP.tinyPool.Put(data)
atomic.AddInt64(&tinyBuffersCount, -1)
}
return
}