forked from viki-org/bytepool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharbitrarypool.go
More file actions
43 lines (37 loc) · 902 Bytes
/
arbitrarypool.go
File metadata and controls
43 lines (37 loc) · 902 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
package bytepool
import (
"sync/atomic"
)
type ArbitraryPool struct {
misses int32
list chan *ArbitraryItem
createNew func() interface{}
resetItem func(item interface{}) error
}
func NewArbitrary(count int, createNew func() interface{}, resetItem func(item interface{}) error) *ArbitraryPool {
p := &ArbitraryPool{
list: make(chan *ArbitraryItem, count),
createNew: createNew,
resetItem: resetItem,
}
for i := 0; i < count; i++ {
p.list <- newArbitraryItem(p, p.createNew())
}
return p
}
func (pool *ArbitraryPool) Checkout() *ArbitraryItem {
var item *ArbitraryItem
select {
case item = <-pool.list:
default:
atomic.AddInt32(&pool.misses, 1)
item = newArbitraryItem(nil, pool.createNew())
}
return item
}
func (pool *ArbitraryPool) Len() int {
return len(pool.list)
}
func (pool *ArbitraryPool) Misses() int32 {
return atomic.LoadInt32(&pool.misses)
}