forked from kyma-project/control-plane
/
queue.go
83 lines (70 loc) · 1.74 KB
/
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
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
package process
import (
"sync"
"time"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/workqueue"
)
const (
workersAmount = 5
)
type Executor interface {
Execute(operationID string) (time.Duration, error)
}
type Queue struct {
queue workqueue.RateLimitingInterface
executor Executor
}
func NewQueue(executor Executor) *Queue {
return &Queue{
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "operations"),
executor: executor,
}
}
func (q *Queue) Add(processId string) {
q.queue.Add(processId)
}
func (q *Queue) Run(stop <-chan struct{}) {
var waitGroup sync.WaitGroup
for i := 0; i < workersAmount; i++ {
createWorker(q.queue, q.executor.Execute, stop, &waitGroup)
}
}
func createWorker(queue workqueue.RateLimitingInterface, process func(id string) (time.Duration, error), stopCh <-chan struct{}, waitGroup *sync.WaitGroup) {
waitGroup.Add(1)
go func() {
wait.Until(worker(queue, process), time.Second, stopCh)
waitGroup.Done()
}()
}
func worker(queue workqueue.RateLimitingInterface, process func(key string) (time.Duration, error)) func() {
return func() {
exit := false
for !exit {
exit = func() bool {
key, quit := queue.Get()
if quit {
return true
}
defer func() {
if err := recover(); err != nil {
logrus.Errorf("panic error from process: %s", err)
}
queue.Done(key)
}()
when, err := process(key.(string))
if err == nil && when != 0 {
logrus.Infof("Adding %q item after %s", key.(string), when)
queue.AddAfter(key, when)
return false
}
if err != nil {
logrus.Errorf("Error from process: %s", err)
}
queue.Forget(key)
return false
}()
}
}
}