-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
64 lines (55 loc) · 1.52 KB
/
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
// Package refpool implements a resource pool with reference counting.
// Based on sync.Pool and sync/atomic
package refpool
import (
"sync"
"sync/atomic"
)
// Element is a resource that holds a reference counter
type Element interface {
// Counter should return pointer to an integer for atomic reference counting.
// Pool methods will use sync.Atomic functions to modify the counter.
Counter() *int64
}
// Refpool is the main package type
type Refpool struct {
p sync.Pool
}
// IncElement adds n to e.Counter() and return the updated value.
func (rp *Refpool) IncElement(e Element, n int64) int64 {
return atomic.AddInt64(e.Counter(), n)
}
// SetElement value of counter
func (rp *Refpool) SetElement(e Element, n int64) {
atomic.StoreInt64(e.Counter(), n)
}
// New returns a new Refpool.
// The argument new should be a function which allocates a new element to be returned from the pool if it's empty.
func New(new func() Element) *Refpool {
return &Refpool{
p: sync.Pool{
New: func() interface{} {
return new()
},
},
}
}
// Get an element from the pool, allocated if needed.
// The element's counter is set to 0.
func (rp *Refpool) Get() Element {
e := rp.p.Get().(Element)
rp.SetElement(e, 0)
return e
}
// Put an element back into the pool.
// First lower the counter by 1, and only put it if the value is 0 or lower.
func (rp *Refpool) Put(e Element) {
if rp.IncElement(e, -1) > 0 {
return
}
rp.p.Put(e)
}
// Drop put the element back regardless its counter.
func (rp *Refpool) Drop(e Element) {
rp.p.Put(e)
}