Adds some TODOs and small fixes to pkg/util/workqueue

Adds a new unit test for queue.
This commit is contained in:
Joe Beda 2016-10-21 18:29:36 -07:00
parent e0c6bf13b1
commit 16b3485817
No known key found for this signature in database
GPG Key ID: 4296898C63A3591D
5 changed files with 46 additions and 5 deletions

View File

@ -68,6 +68,9 @@ type delayingType struct {
stopCh chan struct{} stopCh chan struct{}
// heartbeat ensures we wait no more than maxWait before firing // heartbeat ensures we wait no more than maxWait before firing
//
// TODO: replace with Ticker (and add to clock) so this can be cleaned up.
// clock.Tick will leak.
heartbeat <-chan time.Time heartbeat <-chan time.Time
// waitingForAdd is an ordered slice of items to be added to the contained work queue // waitingForAdd is an ordered slice of items to be added to the contained work queue
@ -192,6 +195,9 @@ func (q *delayingType) waitingLoop() {
// inserts the given entry into the sorted entries list // inserts the given entry into the sorted entries list
// same semantics as append()... the given slice may be modified, // same semantics as append()... the given slice may be modified,
// and the returned value should be used // and the returned value should be used
//
// TODO: This should probably be converted to use container/heap to improve
// running time for a large number of items.
func insert(entries []waitFor, knownEntries map[t]time.Time, entry waitFor) []waitFor { func insert(entries []waitFor, knownEntries map[t]time.Time, entry waitFor) []waitFor {
// if the entry is already in our retry list and the existing time is before the new one, just skip it // if the entry is already in our retry list and the existing time is before the new one, just skip it
existingTime, exists := knownEntries[entry.data] existingTime, exists := knownEntries[entry.data]

View File

@ -33,6 +33,10 @@ func Parallelize(workers, pieces int, doWorkPiece DoWorkPieceFunc) {
} }
close(toProcess) close(toProcess)
if pieces < workers {
workers = pieces
}
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(workers) wg.Add(workers)
for i := 0; i < workers; i++ { for i := 0; i < workers; i++ {

View File

@ -154,7 +154,7 @@ func (q *Type) Done(item interface{}) {
} }
} }
// Shutdown will cause q to ignore all new items added to it. As soon as the // ShutDown will cause q to ignore all new items added to it. As soon as the
// worker goroutines have drained the existing items in the queue, they will be // worker goroutines have drained the existing items in the queue, they will be
// instructed to exit. // instructed to exit.
func (q *Type) ShutDown() { func (q *Type) ShutDown() {

View File

@ -129,3 +129,33 @@ func TestLen(t *testing.T) {
t.Errorf("Expected %v, got %v", e, a) t.Errorf("Expected %v, got %v", e, a)
} }
} }
func TestReinsert(t *testing.T) {
q := workqueue.New()
q.Add("foo")
// Start processing
i, _ := q.Get()
if i != "foo" {
t.Errorf("Expected %v, got %v", "foo", i)
}
// Add it back while processing
q.Add(i)
// Finish it up
q.Done(i)
// It should be back on the queue
i, _ = q.Get()
if i != "foo" {
t.Errorf("Expected %v, got %v", "foo", i)
}
// Finish that one up
q.Done(i)
if a := q.Len(); a != 0 {
t.Errorf("Expected queue to be empty. Has %v items", a)
}
}

View File

@ -16,10 +16,10 @@ limitations under the License.
package workqueue package workqueue
// RateLimitingInterface is an Interface that can Add an item at a later time. This makes it easier to // RateLimitingInterface is an interface that rate limits items being added to the queue.
// requeue items after failures without ending up in a hot-loop.
type RateLimitingInterface interface { type RateLimitingInterface interface {
DelayingInterface DelayingInterface
// AddRateLimited adds an item to the workqueue after the rate limiter says its ok // AddRateLimited adds an item to the workqueue after the rate limiter says its ok
AddRateLimited(item interface{}) AddRateLimited(item interface{})
@ -27,6 +27,7 @@ type RateLimitingInterface interface {
// or for success, we'll stop the rate limiter from tracking it. This only clears the `rateLimiter`, you // or for success, we'll stop the rate limiter from tracking it. This only clears the `rateLimiter`, you
// still have to call `Done` on the queue. // still have to call `Done` on the queue.
Forget(item interface{}) Forget(item interface{})
// NumRequeues returns back how many times the item was requeued // NumRequeues returns back how many times the item was requeued
NumRequeues(item interface{}) int NumRequeues(item interface{}) int
} }