-
Notifications
You must be signed in to change notification settings - Fork 150
/
config.go
279 lines (246 loc) · 10.1 KB
/
config.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package pool
import (
"context"
"errors"
"math"
"sync"
"time"
)
const (
// DefaultMaxTotal is the default value of ObjectPoolConfig.MaxTotal
DefaultMaxTotal = 8
// DefaultMaxIdle is the default value of ObjectPoolConfig.MaxIdle
DefaultMaxIdle = 8
// DefaultMinIdle is the default value of ObjectPoolConfig.MinIdle
DefaultMinIdle = 0
// DefaultLIFO is the default value of ObjectPoolConfig.LIFO
DefaultLIFO = true
// TODO
// DEFAULT_FAIRNESS = false
// DefaultMinEvictableIdleTime is the default value of ObjectPoolConfig.MinEvictableIdleTime
DefaultMinEvictableIdleTime = 30 * time.Minute
// DefaultSoftMinEvictableIdleTime is the default value of ObjectPoolConfig.SoftMinEvictableIdleTime
DefaultSoftMinEvictableIdleTime = time.Duration(math.MaxInt64)
// DefaultNumTestsPerEvictionRun is the default value of ObjectPoolConfig.NumTestsPerEvictionRun
DefaultNumTestsPerEvictionRun = 3
// DefaultTestOnCreate is the default value of ObjectPoolConfig.TestOnCreate
DefaultTestOnCreate = false
// DefaultTestOnBorrow is the default value of ObjectPoolConfig.TestOnBorrow
DefaultTestOnBorrow = false
// DefaultTestOnReturn is the default value of ObjectPoolConfig.TestOnReturn
DefaultTestOnReturn = false
// DefaultTestWhileIdle is the default value of ObjectPoolConfig.TestWhileIdle
DefaultTestWhileIdle = false
// DefaultTimeBetweenEvictionRuns is the default value of ObjectPoolConfig.TimeBetweenEvictionRuns
DefaultTimeBetweenEvictionRuns = time.Duration(0)
// DefaultBlockWhenExhausted is the default value of ObjectPoolConfig.BlockWhenExhausted
DefaultBlockWhenExhausted = true
// DefaultEvictionPolicyName is the default value of ObjectPoolConfig.EvictionPolicyName
DefaultEvictionPolicyName = "github.com/jolestar/go-commons-pool/DefaultEvictionPolicy"
)
// ObjectPoolConfig is ObjectPool config, include cap, block, valid strategy, evict strategy etc.
type ObjectPoolConfig struct {
/**
* Whether the pool has LIFO (last in, first out) behaviour with
* respect to idle objects - always returning the most recently used object
* from the pool, or as a FIFO (first in, first out) queue, where the pool
* always returns the oldest object in the idle object pool.
*/
LIFO bool
/**
* The cap on the number of objects that can be allocated by the pool
* (checked out to clients, or idle awaiting checkout) at a given time. Use
* a negative value for no limit.
*/
MaxTotal int
/**
* The cap on the number of "idle" instances in the pool. Use a
* negative value to indicate an unlimited number of idle instances.
* If MaxIdle
* is set too low on heavily loaded systems it is possible you will see
* objects being destroyed and almost immediately new objects being created.
* This is a result of the active goroutines momentarily returning objects
* faster than they are requesting them them, causing the number of idle
* objects to rise above maxIdle. The best value for maxIdle for heavily
* loaded system will vary but the default is a good starting point.
*/
MaxIdle int
/**
* The minimum number of idle objects to maintain in
* the pool. This setting only has an effect if
* TimeBetweenEvictionRuns is greater than zero. If this
* is the case, an attempt is made to ensure that the pool has the required
* minimum number of instances during idle object eviction runs.
*
* If the configured value of MinIdle is greater than the configured value
* for MaxIdle then the value of MaxIdle will be used instead.
*
*/
MinIdle int
/**
* Whether objects created for the pool will be validated before
* being returned from the ObjectPool.BorrowObject() method. Validation is
* performed by the ValidateObject() method of the factory
* associated with the pool. If the object fails to validate, then
* ObjectPool.BorrowObject() will fail.
*/
TestOnCreate bool
/**
* Whether objects borrowed from the pool will be validated before
* being returned from the ObjectPool.BorrowObject() method. Validation is
* performed by the ValidateObject() method of the factory
* associated with the pool. If the object fails to validate, it will be
* removed from the pool and destroyed, and a new attempt will be made to
* borrow an object from the pool.
*/
TestOnBorrow bool
/**
* Whether objects borrowed from the pool will be validated when
* they are returned to the pool via the ObjectPool.ReturnObject() method.
* Validation is performed by the ValidateObject() method of
* the factory associated with the pool. Returning objects that fail validation
* are destroyed rather then being returned the pool.
*/
TestOnReturn bool
/**
* Whether objects sitting idle in the pool will be validated by the
* idle object evictor (if any - see
* TimeBetweenEvictionRuns ). Validation is performed
* by the ValidateObject() method of the factory associated
* with the pool. If the object fails to validate, it will be removed from
* the pool and destroyed. Note that setting this property has no effect
* unless the idle object evictor is enabled by setting
* TimeBetweenEvictionRuns to a positive value.
*/
TestWhileIdle bool
/**
* Whether to block when the ObjectPool.BorrowObject() method is
* invoked when the pool is exhausted (the maximum number of "active"
* objects has been reached).
*/
BlockWhenExhausted bool
//TODO support fairness config
//Fairness bool
/**
* The minimum amount of time an object may sit idle in the pool
* before it is eligible for eviction by the idle object evictor (if any -
* see TimeBetweenEvictionRuns. When non-positive,
* no objects will be evicted from the pool due to idle time alone.
*/
MinEvictableIdleTime time.Duration
/**
* The minimum amount of time an object may sit idle in the pool
* before it is eligible for eviction by the idle object evictor (if any -
* see TimeBetweenEvictionRuns),
* with the extra condition that at least MinIdle object
* instances remain in the pool. This setting is overridden by
* MinEvictableIdleTime (that is, if
* MinEvictableIdleTime is positive, then
* SoftMinEvictableIdleTime is ignored).
*/
SoftMinEvictableIdleTime time.Duration
/**
* The maximum number of objects to examine during each run (if any)
* of the idle object evictor goroutine. When positive, the number of tests
* performed for a run will be the minimum of the configured value and the
* number of idle instances in the pool. When negative, the number of tests
* performed will be math.Ceil(ObjectPool.GetNumIdle()/math.
* Abs(PoolConfig.NumTestsPerEvictionRun)) which means that when the
* value is -n roughly one nth of the idle objects will be
* tested per run.
*/
NumTestsPerEvictionRun int
/**
* The name of the EvictionPolicy implementation that is
* used by this pool. Please register policy by RegistryEvictionPolicy(name, policy)
*/
EvictionPolicyName string
/**
* The amount of time sleep between runs of the idle
* object evictor goroutine. When non-positive, no idle object evictor goroutine
* will be run.
* if this value changed after ObjectPool created, should call ObjectPool.StartEvictor to take effect.
*/
TimeBetweenEvictionRuns time.Duration
/**
* The context.Context to use when the evictor runs in the background.
*/
EvictionContext context.Context
}
// NewDefaultPoolConfig return a ObjectPoolConfig instance init with default value.
func NewDefaultPoolConfig() *ObjectPoolConfig {
return &ObjectPoolConfig{
LIFO: DefaultLIFO,
MaxTotal: DefaultMaxTotal,
MaxIdle: DefaultMaxIdle,
MinIdle: DefaultMinIdle,
MinEvictableIdleTime: DefaultMinEvictableIdleTime,
SoftMinEvictableIdleTime: DefaultSoftMinEvictableIdleTime,
NumTestsPerEvictionRun: DefaultNumTestsPerEvictionRun,
EvictionPolicyName: DefaultEvictionPolicyName,
EvictionContext: context.Background(),
TestOnCreate: DefaultTestOnCreate,
TestOnBorrow: DefaultTestOnBorrow,
TestOnReturn: DefaultTestOnReturn,
TestWhileIdle: DefaultTestWhileIdle,
TimeBetweenEvictionRuns: DefaultTimeBetweenEvictionRuns,
BlockWhenExhausted: DefaultBlockWhenExhausted}
}
// AbandonedConfig ObjectPool abandoned strategy config
type AbandonedConfig struct {
RemoveAbandonedOnBorrow bool
RemoveAbandonedOnMaintenance bool
// Timeout before an abandoned object can be removed.
RemoveAbandonedTimeout time.Duration
}
// NewDefaultAbandonedConfig return a new AbandonedConfig instance init with default.
func NewDefaultAbandonedConfig() *AbandonedConfig {
return &AbandonedConfig{RemoveAbandonedOnBorrow: false, RemoveAbandonedOnMaintenance: false, RemoveAbandonedTimeout: 5 * time.Minute}
}
// EvictionConfig is config for ObjectPool EvictionPolicy
type EvictionConfig struct {
IdleEvictTime time.Duration
IdleSoftEvictTime time.Duration
MinIdle int
Context context.Context
}
// EvictionPolicy is a interface support custom EvictionPolicy
type EvictionPolicy interface {
// Evict do evict by config
Evict(config *EvictionConfig, underTest *PooledObject, idleCount int) bool
}
// DefaultEvictionPolicy is a default EvictionPolicy impl
type DefaultEvictionPolicy struct {
}
// Evict do evict by config
func (p *DefaultEvictionPolicy) Evict(config *EvictionConfig, underTest *PooledObject, idleCount int) bool {
idleTime := underTest.GetIdleTime()
if (config.IdleSoftEvictTime < idleTime &&
config.MinIdle < idleCount) ||
config.IdleEvictTime < idleTime {
return true
}
return false
}
var (
policiesMutex sync.Mutex
policies = make(map[string]EvictionPolicy)
)
// RegistryEvictionPolicy registry a custom EvictionPolicy with gaven name.
func RegistryEvictionPolicy(name string, policy EvictionPolicy) {
if name == "" || policy == nil {
panic(errors.New("invalid argument"))
}
policiesMutex.Lock()
policies[name] = policy
policiesMutex.Unlock()
}
// GetEvictionPolicy return a EvictionPolicy by gaven name
func GetEvictionPolicy(name string) EvictionPolicy {
policiesMutex.Lock()
defer policiesMutex.Unlock()
return policies[name]
}
func init() {
RegistryEvictionPolicy(DefaultEvictionPolicyName, new(DefaultEvictionPolicy))
}