forked from agones-dev/agones
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkerqueue.go
More file actions
174 lines (150 loc) · 4.67 KB
/
Copy pathworkerqueue.go
File metadata and controls
174 lines (150 loc) · 4.67 KB
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
// Copyright 2018 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package workerqueue extends client-go's workqueue
// functionality into an opinionated queue + worker model that
// is reusable
package workerqueue
import (
"fmt"
"sync"
"time"
"agones.dev/agones/pkg/util/runtime"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
)
const (
workFx = time.Second
)
// Handler is the handler for processing the work queue
// This is usually a syncronisation handler for a controller or related
type Handler func(string) error
// WorkerQueue is an opinionated queue + worker for use
// with controllers and related and processing Kubernetes watched
// events and synchronising resources
type WorkerQueue struct {
logger *logrus.Entry
queue workqueue.RateLimitingInterface
// SyncHandler is exported to make testing easier (hack)
SyncHandler Handler
mu sync.Mutex
workers int
running int
}
// NewWorkerQueue returns a new worker queue for a given name
func NewWorkerQueue(handler Handler, logger *logrus.Entry, name string) *WorkerQueue {
return &WorkerQueue{
logger: logger.WithField("queue", name),
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), name),
SyncHandler: handler,
}
}
// Enqueue puts the name of the runtime.Object in the
// queue to be processed. If you need to send through an
// explicit key, use an cache.ExplicitKey
func (wq *WorkerQueue) Enqueue(obj interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil {
err = errors.Wrap(err, "Error creating key for object")
runtime.HandleError(wq.logger.WithField("obj", obj), err)
return
}
wq.logger.WithField("key", key).Info("Enqueuing key")
wq.queue.AddRateLimited(key)
}
// runWorker is a long-running function that will continually call the
// processNextWorkItem function in order to read and process a message on the
// workqueue.
func (wq *WorkerQueue) runWorker() {
for wq.processNextWorkItem() {
}
}
// processNextWorkItem processes the next work item.
// pretty self explanatory :)
func (wq *WorkerQueue) processNextWorkItem() bool {
obj, quit := wq.queue.Get()
if quit {
return false
}
defer wq.queue.Done(obj)
wq.logger.WithField("obj", obj).Info("Processing obj")
var key string
var ok bool
if key, ok = obj.(string); !ok {
runtime.HandleError(wq.logger.WithField("obj", obj), errors.Errorf("expected string in queue, but got %T", obj))
// this is a bad entry, we don't want to reprocess
wq.queue.Forget(obj)
return true
}
if err := wq.SyncHandler(key); err != nil {
// we don't forget here, because we want this to be retried via the queue
runtime.HandleError(wq.logger.WithField("obj", obj), err)
wq.queue.AddRateLimited(obj)
return true
}
wq.queue.Forget(obj)
return true
}
// Run the WorkerQueue processing via the Handler. Will block until stop is closed.
// Runs a certain number workers to process the rate limited queue
func (wq *WorkerQueue) Run(workers int, stop <-chan struct{}) {
wq.setWorkerCount(workers)
wq.logger.WithField("workers", workers).Info("Starting workers...")
for i := 0; i < workers; i++ {
go wq.run(stop)
}
<-stop
wq.logger.Info("...shutting down workers")
wq.queue.ShutDown()
}
func (wq *WorkerQueue) run(stop <-chan struct{}) {
wq.inc()
defer wq.dec()
wait.Until(wq.runWorker, workFx, stop)
}
// Healthy reports whether all the worker goroutines are running.
func (wq *WorkerQueue) Healthy() error {
wq.mu.Lock()
defer wq.mu.Unlock()
want := wq.workers
got := wq.running
if want != got {
return fmt.Errorf("want %d worker goroutine(s), got %d", want, got)
}
return nil
}
// RunCount reports the number of running worker goroutines started by Run.
func (wq *WorkerQueue) RunCount() int {
wq.mu.Lock()
defer wq.mu.Unlock()
return wq.running
}
func (wq *WorkerQueue) setWorkerCount(n int) {
wq.mu.Lock()
defer wq.mu.Unlock()
wq.workers = n
}
func (wq *WorkerQueue) inc() {
wq.mu.Lock()
defer wq.mu.Unlock()
wq.running++
}
func (wq *WorkerQueue) dec() {
wq.mu.Lock()
defer wq.mu.Unlock()
wq.running--
}