2016-03-24 18:53:28 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 The Kubernetes Authors 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 workqueue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
|
|
|
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DelayingInterface is an Interface that can Add an item at a later time. This makes it easier to
|
|
|
|
// requeue items after failures without ending up in a hot-loop.
|
|
|
|
type DelayingInterface interface {
|
|
|
|
Interface
|
|
|
|
// AddAfter adds an item to the workqueue after the indicated duration has passed
|
|
|
|
AddAfter(item interface{}, duration time.Duration)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDelayingQueue constructs a new workqueue with delayed queuing ability
|
|
|
|
func NewDelayingQueue() DelayingInterface {
|
|
|
|
return newDelayingQueue(util.RealClock{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDelayingQueue(clock util.Clock) DelayingInterface {
|
|
|
|
ret := &delayingType{
|
2016-04-06 13:23:12 +00:00
|
|
|
Interface: New(),
|
|
|
|
clock: clock,
|
|
|
|
heartbeat: time.Tick(maxWait),
|
|
|
|
stopCh: make(chan struct{}),
|
|
|
|
waitingForAddCh: make(chan waitFor, 1000),
|
2016-03-24 18:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go ret.waitingLoop()
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
// delayingType wraps an Interface and provides delayed re-enquing
|
2016-03-24 18:53:28 +00:00
|
|
|
type delayingType struct {
|
2016-04-06 13:23:12 +00:00
|
|
|
Interface
|
2016-03-24 18:53:28 +00:00
|
|
|
|
|
|
|
// clock tracks time for delayed firing
|
|
|
|
clock util.Clock
|
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
// stopCh lets us signal a shutdown to the waiting loop
|
|
|
|
stopCh chan struct{}
|
|
|
|
|
|
|
|
// heartbeat ensures we wait no more than maxWait before firing
|
|
|
|
heartbeat <-chan time.Time
|
|
|
|
|
2016-03-24 18:53:28 +00:00
|
|
|
// waitingForAdd is an ordered slice of items to be added to the contained work queue
|
|
|
|
waitingForAdd []waitFor
|
2016-04-06 13:23:12 +00:00
|
|
|
// waitingForAddCh is a buffered channel that feeds waitingForAdd
|
|
|
|
waitingForAddCh chan waitFor
|
2016-03-24 18:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// waitFor holds the data to add and the time it should be added
|
|
|
|
type waitFor struct {
|
|
|
|
data t
|
|
|
|
readyAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShutDown gives a way to shut off this queue
|
|
|
|
func (q *delayingType) ShutDown() {
|
2016-04-06 13:23:12 +00:00
|
|
|
q.Interface.ShutDown()
|
|
|
|
close(q.stopCh)
|
2016-03-24 18:53:28 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
// AddAfter adds the given item to the work queue after the given delay
|
2016-03-24 18:53:28 +00:00
|
|
|
func (q *delayingType) AddAfter(item interface{}, duration time.Duration) {
|
2016-04-06 13:23:12 +00:00
|
|
|
// don't add if we're already shutting down
|
|
|
|
if q.ShuttingDown() {
|
|
|
|
return
|
|
|
|
}
|
2016-03-24 18:53:28 +00:00
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
// immediately add things with no delay
|
|
|
|
if duration <= 0 {
|
|
|
|
q.Add(item)
|
|
|
|
return
|
|
|
|
}
|
2016-03-24 18:53:28 +00:00
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
select {
|
|
|
|
case <-q.stopCh:
|
|
|
|
// unblock if ShutDown() is called
|
|
|
|
case q.waitingForAddCh <- waitFor{data: item, readyAt: q.clock.Now().Add(duration)}:
|
|
|
|
}
|
2016-03-24 18:53:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// maxWait keeps a max bound on the wait time. It's just insurance against weird things happening.
|
|
|
|
// Checking the queue every 10 seconds isn't expensive and we know that we'll never end up with an
|
|
|
|
// expired item sitting for more than 10 seconds.
|
|
|
|
const maxWait = 10 * time.Second
|
|
|
|
|
|
|
|
// waitingLoop runs until the workqueue is shutdown and keeps a check on the list of items to be added.
|
|
|
|
func (q *delayingType) waitingLoop() {
|
|
|
|
defer utilruntime.HandleCrash()
|
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
// Make a placeholder channel to use when there are no items in our list
|
|
|
|
never := make(<-chan time.Time)
|
|
|
|
|
2016-03-24 18:53:28 +00:00
|
|
|
for {
|
2016-04-06 13:23:12 +00:00
|
|
|
if q.Interface.ShuttingDown() {
|
|
|
|
// discard waiting entries
|
|
|
|
q.waitingForAdd = nil
|
2016-03-24 18:53:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
now := q.clock.Now()
|
2016-03-24 18:53:28 +00:00
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
// Add ready entries
|
|
|
|
readyEntries := 0
|
|
|
|
for _, entry := range q.waitingForAdd {
|
|
|
|
if entry.readyAt.After(now) {
|
|
|
|
break
|
2016-03-24 18:53:28 +00:00
|
|
|
}
|
2016-04-06 13:23:12 +00:00
|
|
|
q.Add(entry.data)
|
|
|
|
readyEntries++
|
|
|
|
}
|
|
|
|
q.waitingForAdd = q.waitingForAdd[readyEntries:]
|
2016-03-24 18:53:28 +00:00
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
// Set up a wait for the first item's readyAt (if one exists)
|
|
|
|
nextReadyAt := never
|
|
|
|
if len(q.waitingForAdd) > 0 {
|
|
|
|
nextReadyAt = q.clock.After(q.waitingForAdd[0].readyAt.Sub(now))
|
|
|
|
}
|
2016-03-24 18:53:28 +00:00
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
select {
|
|
|
|
case <-q.stopCh:
|
|
|
|
return
|
2016-03-24 18:53:28 +00:00
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
case <-q.heartbeat:
|
|
|
|
// continue the loop, which will add ready items
|
2016-03-24 18:53:28 +00:00
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
case <-nextReadyAt:
|
|
|
|
// continue the loop, which will add ready items
|
2016-03-24 18:53:28 +00:00
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
case waitEntry := <-q.waitingForAddCh:
|
|
|
|
if waitEntry.readyAt.After(q.clock.Now()) {
|
|
|
|
q.waitingForAdd = insert(q.waitingForAdd, waitEntry)
|
|
|
|
} else {
|
|
|
|
q.Add(waitEntry.data)
|
|
|
|
}
|
2016-03-24 18:53:28 +00:00
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
drained := false
|
|
|
|
for !drained {
|
|
|
|
select {
|
|
|
|
case waitEntry := <-q.waitingForAddCh:
|
|
|
|
if waitEntry.readyAt.After(q.clock.Now()) {
|
|
|
|
q.waitingForAdd = insert(q.waitingForAdd, waitEntry)
|
|
|
|
} else {
|
|
|
|
q.Add(waitEntry.data)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
drained = true
|
2016-03-24 18:53:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
// inserts the given entry into the sorted entries list
|
|
|
|
// same semantics as append()... the given slice may be modified,
|
|
|
|
// and the returned value should be used
|
|
|
|
func insert(entries []waitFor, entry waitFor) []waitFor {
|
|
|
|
insertionIndex := sort.Search(len(entries), func(i int) bool {
|
|
|
|
return entry.readyAt.Before(entries[i].readyAt)
|
|
|
|
})
|
2016-03-24 18:53:28 +00:00
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
// grow by 1
|
|
|
|
entries = append(entries, waitFor{})
|
|
|
|
// shift items from the insertion point to the end
|
|
|
|
copy(entries[insertionIndex+1:], entries[insertionIndex:])
|
|
|
|
// insert the record
|
|
|
|
entries[insertionIndex] = entry
|
2016-03-24 18:53:28 +00:00
|
|
|
|
2016-04-06 13:23:12 +00:00
|
|
|
return entries
|
2016-03-24 18:53:28 +00:00
|
|
|
}
|