-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
work_queue.go
51 lines (39 loc) · 1.22 KB
/
work_queue.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
package metrics
import "k8s.io/client-go/util/workqueue"
type workersBusyRateLimiterWorkQueue struct {
workqueue.RateLimitingInterface
workerType string
metrics *Metrics
}
func (m *Metrics) RateLimiterWithBusyWorkers(workQueue workqueue.RateLimiter, queueName string) workqueue.RateLimitingInterface {
m.newWorker(queueName)
return workersBusyRateLimiterWorkQueue{
RateLimitingInterface: workqueue.NewNamedRateLimitingQueue(workQueue, queueName),
workerType: queueName,
metrics: m,
}
}
func (m *Metrics) newWorker(workerType string) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.workersBusy[workerType] = getWorkersBusy(workerType)
}
func (m *Metrics) workerBusy(workerType string) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.workersBusy[workerType].Inc()
}
func (m *Metrics) workerFree(workerType string) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.workersBusy[workerType].Dec()
}
func (w workersBusyRateLimiterWorkQueue) Get() (interface{}, bool) {
item, shutdown := w.RateLimitingInterface.Get()
w.metrics.workerBusy(w.workerType)
return item, shutdown
}
func (w workersBusyRateLimiterWorkQueue) Done(item interface{}) {
w.RateLimitingInterface.Done(item)
w.metrics.workerFree(w.workerType)
}