-
Notifications
You must be signed in to change notification settings - Fork 453
/
pool.go
162 lines (135 loc) · 4.34 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package leakcheckpool
import (
"fmt"
"runtime/debug"
"sync"
"testing"
"github.com/mauricelam/genny/generic"
"github.com/stretchr/testify/require"
)
// elemType is the generic type for use with the debug pool.
type elemType generic.Type
// elemTypeEqualsFn allows users to override equality checks
// for `elemType` instances.
type elemTypeEqualsFn func(a, b elemType) bool
// elemTypeGetHookFn allows users to override properties on items
// retrieved from the backing pools before returning in the Get()
// path.
type elemTypeGetHookFn func(elemType) elemType
// elemTypePool is the backing pool wrapped by the debug pool.
type elemTypePool interface {
generic.Type
Init()
Get() elemType
Put(elemType)
}
// leakcheckElemTypePoolOpts allows users to override default behaviour.
type leakcheckElemTypePoolOpts struct {
DisallowUntrackedPuts bool
EqualsFn elemTypeEqualsFn
GetHookFn elemTypeGetHookFn
}
// newLeakcheckElemTypePool returns a new leakcheckElemTypePool.
// nolint
func newLeakcheckElemTypePool(opts leakcheckElemTypePoolOpts, backingPool elemTypePool) *leakcheckElemTypePool {
if opts.EqualsFn == nil {
// NB(prateek): fall-back to == in the worst case
opts.EqualsFn = func(a, b elemType) bool {
return a == b
}
}
return &leakcheckElemTypePool{opts: opts, elemTypePool: backingPool}
}
// leakcheckElemTypePool wraps the underlying elemTypePool to make it easier to
// track leaks/allocs.
type leakcheckElemTypePool struct {
sync.Mutex
elemTypePool
NumGets int
NumPuts int
PendingItems []leakcheckElemType
AllGetItems []leakcheckElemType
opts leakcheckElemTypePoolOpts
}
// leakcheckElemType wraps `elemType` instances along with their last Get() paths.
type leakcheckElemType struct {
Value elemType
GetStacktrace []byte // GetStacktrace is the stacktrace for the Get() of this item
}
func (p *leakcheckElemTypePool) Init() {
p.Lock()
defer p.Unlock()
p.elemTypePool.Init()
}
func (p *leakcheckElemTypePool) Get() elemType {
p.Lock()
defer p.Unlock()
e := p.elemTypePool.Get()
if fn := p.opts.GetHookFn; fn != nil {
e = fn(e)
}
p.NumGets++
item := leakcheckElemType{
Value: e,
GetStacktrace: debug.Stack(),
}
p.PendingItems = append(p.PendingItems, item)
p.AllGetItems = append(p.AllGetItems, item)
return e
}
func (p *leakcheckElemTypePool) Put(value elemType) {
p.Lock()
defer p.Unlock()
idx := -1
for i, item := range p.PendingItems {
if p.opts.EqualsFn(item.Value, value) {
idx = i
break
}
}
if idx == -1 && p.opts.DisallowUntrackedPuts {
panic(fmt.Errorf("untracked object (%v) returned to pool", value))
}
if idx != -1 {
// update slice
p.PendingItems = append(p.PendingItems[:idx], p.PendingItems[idx+1:]...)
}
p.NumPuts++
p.elemTypePool.Put(value)
}
// Check ensures there are no leaks.
func (p *leakcheckElemTypePool) Check(t *testing.T) {
p.Lock()
defer p.Unlock()
require.Equal(t, p.NumGets, p.NumPuts)
require.Empty(t, p.PendingItems)
}
type leakcheckElemTypeFn func(e leakcheckElemType)
// CheckExtended ensures there are no leaks, and executes the specified fn
func (p *leakcheckElemTypePool) CheckExtended(t *testing.T, fn leakcheckElemTypeFn) {
p.Check(t)
p.Lock()
defer p.Unlock()
for _, e := range p.AllGetItems {
fn(e)
}
}