@@ -154,10 +154,7 @@ func (c *Controller) Start(stop <-chan struct{}) error {
154
154
log .Info ("Starting workers" , "controller" , c .Name , "worker count" , c .MaxConcurrentReconciles )
155
155
for i := 0 ; i < c .MaxConcurrentReconciles ; i ++ {
156
156
// Process work items
157
- go wait .Until (func () {
158
- for c .processNextWorkItem () {
159
- }
160
- }, c .JitterPeriod , stop )
157
+ go wait .Until (c .worker , c .JitterPeriod , stop )
161
158
}
162
159
163
160
c .Started = true
@@ -168,17 +165,16 @@ func (c *Controller) Start(stop <-chan struct{}) error {
168
165
return nil
169
166
}
170
167
168
+ // worker runs a worker thread that just dequeues items, processes them, and marks them done.
169
+ // It enforces that the reconcileHandler is never invoked concurrently with the same object.
170
+ func (c * Controller ) worker () {
171
+ for c .processNextWorkItem () {
172
+ }
173
+ }
174
+
171
175
// processNextWorkItem will read a single work item off the workqueue and
172
- // attempt to process it, by calling the syncHandler .
176
+ // attempt to process it, by calling the reconcileHandler .
173
177
func (c * Controller ) processNextWorkItem () bool {
174
- // This code copy-pasted from the sample-Controller.
175
-
176
- // Update metrics after processing each item
177
- reconcileStartTS := time .Now ()
178
- defer func () {
179
- c .updateMetrics (time .Now ().Sub (reconcileStartTS ))
180
- }()
181
-
182
178
obj , shutdown := c .Queue .Get ()
183
179
if shutdown {
184
180
// Stop working
@@ -192,6 +188,17 @@ func (c *Controller) processNextWorkItem() bool {
192
188
// put back on the workqueue and attempted again after a back-off
193
189
// period.
194
190
defer c .Queue .Done (obj )
191
+
192
+ return c .reconcileHandler (obj )
193
+ }
194
+
195
+ func (c * Controller ) reconcileHandler (obj interface {}) bool {
196
+ // Update metrics after processing each item
197
+ reconcileStartTS := time .Now ()
198
+ defer func () {
199
+ c .updateMetrics (time .Now ().Sub (reconcileStartTS ))
200
+ }()
201
+
195
202
var req reconcile.Request
196
203
var ok bool
197
204
if req , ok = obj .(reconcile.Request ); ! ok {
@@ -204,7 +211,6 @@ func (c *Controller) processNextWorkItem() bool {
204
211
// Return true, don't take a break
205
212
return true
206
213
}
207
-
208
214
// RunInformersAndControllers the syncHandler, passing it the namespace/Name string of the
209
215
// resource to be synced.
210
216
if result , err := c .Do .Reconcile (req ); err != nil {
0 commit comments