/
tiny_pool.go
67 lines (58 loc) · 1021 Bytes
/
tiny_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
package comm
import (
"math/rand"
"time"
)
/*
TINY POOL STORE SIMPLE DATA
MAY REFER sync.Pool
*/
type TinyNewFunc func() interface{}
type TinyPool struct {
pool_size int
data chan interface{}
new_func TinyNewFunc
}
//New TinyPool
func NewTinyPool(pool_size int, new_func TinyNewFunc) *TinyPool {
pool := new(TinyPool)
pool.pool_size = pool_size
pool.data = make(chan interface{}, pool_size)
pool.new_func = new_func
go pool.serve()
return pool
}
//del mem
func (pool *TinyPool) serve() {
for {
time.Sleep(1 * time.Second)
num := len(pool.data)
if num <= 0 {
continue
}
del_count := rand.Intn(num)
for i := 0; i < del_count; i++ {
<-pool.data
}
}
}
//Get an obj
func (pool *TinyPool) Get() interface{} {
select {
case pv := <-pool.data:
return pv
default:
if pool.new_func != nil {
return pool.new_func()
} else {
return nil
}
}
}
//Put an obj
func (pool *TinyPool) Put(v interface{}) {
if len(pool.data) >= cap(pool.data) {
return
}
pool.data <- v
}