-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
423 lines (370 loc) · 9.42 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package ignite
import (
"context"
"errors"
"fmt"
"sync"
"time"
"go.uber.org/atomic"
)
var (
ErrPoolExhausted = errors.New("pool is exhausted")
ErrPoolClosed = errors.New("pool is closed")
ErrInvalidConfig = errors.New("invalid pool configuration")
)
type Pool[T any] interface {
Get(ctx context.Context) (*ObjectWrapper[T], error)
Put(*ObjectWrapper[T])
Close(ctx context.Context) error
Len() int64
Stats() Stats
Resize(newSize int) error
UpdateConfig(newConfig Config[T]) error
}
type ObjectWrapper[T any] struct {
Object T
CreateTime time.Time
LastUseTime atomic.Time
UsageCount atomic.Int64
}
type Config[T any] struct {
InitialSize int
MaxSize int
MinSize int
MaxIdleTime time.Duration
Factory func() (T, error)
Reset func(T) error
Validate func(T) error
HealthCheck func(T) error
HealthCheckInterval time.Duration
}
type Stats struct {
CurrentSize atomic.Int64
AvailableObjects atomic.Int64
InUseObjects atomic.Int64
TotalCreated atomic.Int64
TotalDestroyed atomic.Int64
TotalResets atomic.Int64
MaxUsage atomic.Int64
}
type pool[T any] struct {
config Config[T]
objects chan *ObjectWrapper[T]
closed atomic.Bool
stats Stats
mu sync.RWMutex
configUpdateCh chan Config[T]
stopCh chan struct{}
wrapperPool sync.Pool
}
func NewPool[T any](config Config[T]) (Pool[T], error) {
if err := validateConfig(config); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
p := &pool[T]{
config: config,
objects: make(chan *ObjectWrapper[T], config.MaxSize),
configUpdateCh: make(chan Config[T]),
stopCh: make(chan struct{}),
wrapperPool: sync.Pool{
New: func() interface{} {
return &ObjectWrapper[T]{}
},
},
}
for i := 0; i < config.InitialSize; i++ {
if err := p.addObject(); err != nil {
if err = p.Close(context.Background()); err != nil {
return nil, err
}
return nil, fmt.Errorf("failed to create initial objects: %w", err)
}
}
go p.mainLoop()
return p, nil
}
func (p *pool[T]) mainLoop() {
cleanupTicker := time.NewTicker(p.config.MaxIdleTime / 2)
defer cleanupTicker.Stop()
var healthCheckTicker *time.Ticker
if p.config.HealthCheck != nil && p.config.HealthCheckInterval > 0 {
healthCheckTicker = time.NewTicker(p.config.HealthCheckInterval)
defer healthCheckTicker.Stop()
}
for {
select {
case <-p.stopCh:
return
case newConfig := <-p.configUpdateCh:
p.updateConfig(newConfig)
cleanupTicker.Reset(newConfig.MaxIdleTime / 2)
if healthCheckTicker != nil {
healthCheckTicker.Stop()
}
if newConfig.HealthCheck != nil && newConfig.HealthCheckInterval > 0 {
healthCheckTicker = time.NewTicker(newConfig.HealthCheckInterval)
}
case <-cleanupTicker.C:
p.cleanup()
//case <-healthCheckTicker.C:
// p.performHealthCheck()
}
}
}
func (p *pool[T]) updateConfig(newConfig Config[T]) {
p.mu.Lock()
defer p.mu.Unlock()
p.config = newConfig
if newConfig.MaxSize < len(p.objects) {
excess := len(p.objects) - newConfig.MaxSize
for i := 0; i < excess; i++ {
select {
case obj := <-p.objects:
p.destroyObject(obj)
default:
return
}
}
}
}
func (p *pool[T]) Get(ctx context.Context) (*ObjectWrapper[T], error) {
if p.closed.Load() {
return nil, ErrPoolClosed
}
for {
select {
case obj := <-p.objects:
p.stats.AvailableObjects.Dec()
if err := p.prepareObject(obj); err != nil {
// 如果物件準備失敗,我們繼續嘗試獲取下一個物件
continue
}
return obj, nil
case <-ctx.Done():
return nil, ctx.Err()
default:
// 如果通道為空,我們嘗試添加一個新物件
if p.stats.CurrentSize.Load() < int64(p.config.MaxSize) {
if err := p.addObject(); err != nil {
return nil, fmt.Errorf("failed to create new object: %w", err)
}
// 添加成功後,繼續循環嘗試獲取物件
continue
}
// 如果池已滿,返回錯誤
return nil, ErrPoolExhausted
}
}
}
func (p *pool[T]) prepareObject(obj *ObjectWrapper[T]) error {
if time.Since(obj.LastUseTime.Load()) > p.config.MaxIdleTime {
if err := p.resetObject(obj); err != nil {
p.destroyObject(obj)
return fmt.Errorf("failed to reset object: %w", err)
}
}
if p.config.Validate != nil {
if err := p.config.Validate(obj.Object); err != nil {
p.destroyObject(obj)
return fmt.Errorf("object validation failed: %w", err)
}
}
obj.LastUseTime.Store(time.Now())
obj.UsageCount.Inc()
p.stats.InUseObjects.Inc()
p.updateMaxUsage(obj.UsageCount.Load())
return nil
}
func (p *pool[T]) Put(obj *ObjectWrapper[T]) {
if p.closed.Load() {
p.destroyObject(obj)
return
}
obj.LastUseTime.Store(time.Now())
p.stats.InUseObjects.Dec()
if time.Since(obj.LastUseTime.Load()) > p.config.MaxIdleTime {
p.destroyObject(obj)
return
}
select {
case p.objects <- obj:
p.stats.AvailableObjects.Inc()
default:
p.destroyObject(obj)
}
}
func (p *pool[T]) Close(ctx context.Context) error {
if !p.closed.CompareAndSwap(false, true) {
return nil
}
close(p.stopCh)
// 使用帶超時的上下文進行關閉操作
closeCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
done := make(chan struct{})
go func() {
close(p.objects)
for obj := range p.objects {
p.destroyObject(obj)
}
close(done)
}()
select {
case <-done:
return nil
case <-closeCtx.Done():
return closeCtx.Err()
}
}
func (p *pool[T]) Len() int64 {
return p.stats.CurrentSize.Load()
}
func (p *pool[T]) Stats() Stats {
return Stats{
CurrentSize: p.stats.CurrentSize,
AvailableObjects: atomic.Int64{},
InUseObjects: p.stats.InUseObjects,
TotalCreated: p.stats.TotalCreated,
TotalDestroyed: p.stats.TotalDestroyed,
TotalResets: p.stats.TotalResets,
MaxUsage: p.stats.MaxUsage,
}
}
func (p *pool[T]) Resize(newSize int) error {
p.mu.Lock()
defer p.mu.Unlock()
if newSize < p.config.MinSize || newSize > p.config.MaxSize {
return fmt.Errorf("new size must be between %d and %d", p.config.MinSize, p.config.MaxSize)
}
currentSize := p.stats.CurrentSize.Load()
if newSize > int(currentSize) {
for i := currentSize; i < int64(newSize); i++ {
if err := p.addObject(); err != nil {
return fmt.Errorf("failed to add object during resize: %w", err)
}
}
} else if newSize < int(currentSize) {
for i := currentSize; i > int64(newSize); i-- {
select {
case obj := <-p.objects:
p.destroyObject(obj)
default:
return nil
}
}
}
return nil
}
func (p *pool[T]) UpdateConfig(newConfig Config[T]) error {
if err := validateConfig(newConfig); err != nil {
return err
}
p.configUpdateCh <- newConfig
return nil
}
func (p *pool[T]) addObject() error {
obj, err := p.config.Factory()
if err != nil {
return err
}
wrapper := p.wrapperPool.Get().(*ObjectWrapper[T])
wrapper.Object = obj
wrapper.CreateTime = time.Now()
wrapper.LastUseTime.Store(time.Now())
wrapper.UsageCount.Store(0)
select {
case p.objects <- wrapper:
p.stats.CurrentSize.Inc()
p.stats.TotalCreated.Inc()
p.stats.AvailableObjects.Inc()
return nil
default:
p.wrapperPool.Put(wrapper)
if p.config.Reset != nil {
_ = p.config.Reset(obj)
}
return errors.New("failed to add object to pool: channel full")
}
}
func (p *pool[T]) cleanup() {
var activeObjs []*ObjectWrapper[T]
batchSize := 100 // 可以根據實際情況調整
for i := 0; i < batchSize; i++ {
select {
case obj := <-p.objects:
if time.Since(obj.LastUseTime.Load()) > p.config.MaxIdleTime && p.stats.CurrentSize.Load() > int64(p.config.MinSize) {
p.destroyObject(obj)
} else {
activeObjs = append(activeObjs, obj)
}
default:
break
}
}
for _, obj := range activeObjs {
p.objects <- obj
}
}
func (p *pool[T]) performHealthCheck() {
var healthyObjs []*ObjectWrapper[T]
batchSize := 100 // 可以根據實際情況調整
for i := 0; i < batchSize; i++ {
select {
case obj := <-p.objects:
if err := p.config.HealthCheck(obj.Object); err != nil {
p.destroyObject(obj)
} else {
healthyObjs = append(healthyObjs, obj)
}
default:
break
}
}
for _, obj := range healthyObjs {
p.objects <- obj
}
}
func validateConfig[T any](config Config[T]) error {
if config.InitialSize < 0 || config.MaxSize <= 0 || config.InitialSize > config.MaxSize || config.MinSize > config.MaxSize {
return ErrInvalidConfig
}
if config.MaxIdleTime <= 0 {
return errors.New("MaxIdleTime must be positive")
}
if config.Factory == nil {
return errors.New("factory function must be provided")
}
if config.HealthCheck != nil && config.HealthCheckInterval <= 0 {
return errors.New("HealthCheckInterval must be positive when HealthCheck is provided")
}
return nil
}
func (p *pool[T]) destroyObject(obj *ObjectWrapper[T]) {
if p.config.Reset != nil {
_ = p.config.Reset(obj.Object)
}
p.wrapperPool.Put(obj)
p.stats.CurrentSize.Dec()
p.stats.TotalDestroyed.Inc()
p.stats.AvailableObjects.Dec()
}
func (p *pool[T]) resetObject(obj *ObjectWrapper[T]) error {
if p.config.Reset != nil {
if err := p.config.Reset(obj.Object); err != nil {
return fmt.Errorf("failed to reset object: %w", err)
}
}
p.stats.TotalResets.Inc()
return nil
}
func (p *pool[T]) updateMaxUsage(currentUsage int64) {
for {
maxUsage := p.stats.MaxUsage.Load()
if currentUsage <= maxUsage {
return
}
if p.stats.MaxUsage.CompareAndSwap(maxUsage, currentUsage) {
return
}
}
}