-
Notifications
You must be signed in to change notification settings - Fork 303
/
worker_pool.go
119 lines (105 loc) · 2.98 KB
/
worker_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
package processor
import (
"context"
"sync"
"time"
"github.com/rudderlabs/rudder-server/rruntime"
"github.com/rudderlabs/rudder-server/utils/logger"
)
// withWorkerPoolCleanupPeriod option sets the cleanup period for the worker pool
func withWorkerPoolCleanupPeriod(cleanupPeriod time.Duration) func(*workerPool) {
return func(wp *workerPool) {
wp.cleanupPeriod = cleanupPeriod
}
}
// withWorkerPoolIdleTimeout option sets the idle timeout for the worker pool
func withWorkerPoolIdleTimeout(idleTimeout time.Duration) func(*workerPool) {
return func(wp *workerPool) {
wp.idleTimeout = idleTimeout
}
}
// newWorkerPool creates a new worker pool
func newWorkerPool(ctx context.Context, handle workerHandle, opts ...func(*workerPool)) *workerPool {
wp := &workerPool{
handle: handle,
logger: handle.logger().Child("worker-pool"),
workers: make(map[string]*worker),
cleanupPeriod: 10 * time.Second,
idleTimeout: 5 * time.Minute,
}
for _, opt := range opts {
opt(wp)
}
wp.lifecycle.ctx, wp.lifecycle.cancel = context.WithCancel(ctx)
wp.startCleanupLoop()
return wp
}
// workerPool manages a pool of workers
type workerPool struct {
handle workerHandle
logger logger.Logger
cleanupPeriod time.Duration
idleTimeout time.Duration
workersMu sync.RWMutex
workers map[string]*worker
lifecycle struct {
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
}
}
// PingWorker pings the worker for the given partition
func (wp *workerPool) PingWorker(partition string) {
wp.worker(partition).Ping()
}
// Shutdown stops all workers in the pull and waits for them to stop
func (wp *workerPool) Shutdown() {
for _, w := range wp.workers {
w.Stop()
}
wp.lifecycle.cancel()
wp.lifecycle.wg.Wait()
}
// Size returns the number of workers in the pool
func (wp *workerPool) Size() int {
wp.workersMu.RLock()
defer wp.workersMu.RUnlock()
return len(wp.workers)
}
// worker gets or creates a worker for the given partition
func (wp *workerPool) worker(partition string) *worker {
wp.workersMu.Lock()
defer wp.workersMu.Unlock()
w, ok := wp.workers[partition]
if !ok {
wp.logger.Infof("Adding worker in the pool for partition: %s", partition)
w = newWorker(wp.lifecycle.ctx, partition, wp.handle)
wp.workers[partition] = w
}
return w
}
// startCleanupLoop starts a loop that cleans up idle workers
func (wp *workerPool) startCleanupLoop() {
wp.lifecycle.wg.Add(1)
rruntime.Go(func() {
defer wp.lifecycle.wg.Done()
for {
select {
case <-wp.lifecycle.ctx.Done():
return
case <-time.After(wp.cleanupPeriod):
}
wp.workersMu.Lock()
for partition, w := range wp.workers {
idleTime := w.IdleSince()
if !idleTime.IsZero() && time.Since(idleTime) > wp.idleTimeout {
wp.logger.Infof("Destroying idle worker for partition: %s", partition)
w.Stop()
delete(wp.workers, partition)
wp.logger.Infof("Removed idle worker from pool for partition: %s", partition)
}
}
wp.workersMu.Unlock()
}
})
}