-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbyteChanPool.go
More file actions
49 lines (44 loc) · 978 Bytes
/
Copy pathbyteChanPool.go
File metadata and controls
49 lines (44 loc) · 978 Bytes
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
package pool
//ByteChanPool is a simple pool of []byte using a channel
type ByteChanPool struct {
sliceSize int
poolSize int
pool chan []byte
}
//NewByteChanPool...
func NewByteChanPool(poolSize, sliceSize int) *ByteChanPool {
return &ByteChanPool{
sliceSize: sliceSize,
poolSize: poolSize,
pool: make(chan []byte, poolSize),
}
}
//Get returns a cleared []byte
func (b *ByteChanPool) Get() (value []byte) {
select {
case value = <-b.pool:
//this will be optimied into a memclr call by the compiler
for i, _ := range value {
value[i] = 0
}
//resize the slice, this will panic if the slice doesn't have the capacity
value = value[:b.sliceSize]
return value
default:
return make([]byte, b.sliceSize)
}
}
//Put adds a slice to the pool
func (b *ByteChanPool) Put(value []byte) {
select {
case b.pool <- value:
//put on pool
default:
//drop value
}
return
}
//Size...
func (b ByteChanPool) Size() int {
return len(b.pool)
}