1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
7 /* A WorkQueue is an asynchronous thread-safe queue manager. It
8 provides a channel from which items can be read off the queue, and
9 permits replacing the contents of the queue at any time.
11 The overall work flow for a WorkQueue is as follows:
13 1. A WorkQueue is created with NewWorkQueue(). This
14 function instantiates a new WorkQueue and starts a manager
15 goroutine. The manager listens on an input channel
16 (manager.newlist) and an output channel (manager.NextItem).
18 2. The manager first waits for a new list of requests on the
19 newlist channel. When another goroutine calls
20 manager.ReplaceQueue(lst), it sends lst over the newlist
21 channel to the manager. The manager goroutine now has
22 ownership of the list.
24 3. Once the manager has this initial list, it listens on both the
25 input and output channels for one of the following to happen:
27 a. A worker attempts to read an item from the NextItem
28 channel. The manager sends the next item from the list
29 over this channel to the worker, and loops.
31 b. New data is sent to the manager on the newlist channel.
32 This happens when another goroutine calls
33 manager.ReplaceItem() with a new list. The manager
34 discards the current list, replaces it with the new one,
35 and begins looping again.
37 c. The input channel is closed. The manager closes its
38 output channel (signalling any workers to quit) and
41 Tasks currently handled by WorkQueue:
47 // Any kind of user-defined type can be used with the
49 type FrobRequest struct {
54 froblist := NewWorkQueue()
56 // Start a concurrent worker to read items from the NextItem
57 // channel until it is closed, deleting each one.
58 go func(list WorkQueue) {
59 for i := range list.NextItem {
60 req := i.(FrobRequest)
65 // Set up a HTTP handler for PUT /frob
66 router.HandleFunc(`/frob`,
67 func(w http.ResponseWriter, req *http.Request) {
68 // Parse the request body into a list.List
69 // of FrobRequests, and give this list to the
71 newfrobs := parseBody(req.Body)
72 froblist.ReplaceQueue(newfrobs)
75 Methods available on a WorkQueue:
78 Replaces the current item list with a new one. The list
79 manager discards any unprocessed items on the existing
80 list and replaces it with the new one. If the worker is
81 processing a list item when ReplaceQueue is called, it
82 finishes processing before receiving items from the new
85 Shuts down the manager goroutine. When Close is called,
86 the manager closes the NextItem channel.
89 import "container/list"
91 // WorkQueue definition
92 type WorkQueue struct {
93 getStatus chan WorkQueueStatus
94 newlist chan *list.List
95 // Workers get work items by reading from this channel.
96 NextItem <-chan interface{}
97 // Each worker must send struct{}{} to DoneItem exactly once
98 // for each work item received from NextItem, when it stops
99 // working on that item (regardless of whether the work was
101 DoneItem chan<- struct{}
104 // WorkQueueStatus reflects the queue status.
105 type WorkQueueStatus struct {
110 // NewWorkQueue returns a new empty WorkQueue.
112 func NewWorkQueue() *WorkQueue {
113 nextItem := make(chan interface{})
114 reportDone := make(chan struct{})
115 newList := make(chan *list.List)
117 getStatus: make(chan WorkQueueStatus),
120 DoneItem: reportDone,
123 // Read new work lists from the newlist channel.
124 // Reply to "status" and "get next item" queries by
125 // sending to the getStatus and nextItem channels
126 // respectively. Return when the newlist channel
130 status := WorkQueueStatus{}
132 // When we're done, close the output channel; workers will
133 // shut down next time they ask for new work.
134 defer close(nextItem)
135 defer close(b.getStatus)
137 // nextChan and nextVal are both nil when we have
138 // nothing to send; otherwise they are, respectively,
139 // the nextItem channel and the next work item to send
141 var nextChan chan interface{}
142 var nextVal interface{}
144 for newList != nil || status.InProgress > 0 {
146 case p, ok := <-newList:
148 // Closed, stop receiving
155 status.Queued = todo.Len()
156 if status.Queued == 0 {
162 nextVal = todo.Front().Value
164 case nextChan <- nextVal:
165 todo.Remove(todo.Front())
168 if status.Queued == 0 {
173 nextVal = todo.Front().Value
177 case b.getStatus <- status:
184 // ReplaceQueue abandons any work items left in the existing queue,
185 // and starts giving workers items from the given list. After giving
186 // it to ReplaceQueue, the caller must not read or write the given
189 func (b *WorkQueue) ReplaceQueue(list *list.List) {
193 // Close shuts down the manager and terminates the goroutine, which
194 // abandons any pending requests, but allows any pull request already
195 // in progress to continue.
197 // After Close, Status will return correct values, NextItem will be
198 // closed, and ReplaceQueue will panic.
200 func (b *WorkQueue) Close() {
204 // Status returns an up-to-date WorkQueueStatus reflecting the current
207 func (b *WorkQueue) Status() WorkQueueStatus {
208 // If the channel is closed, we get the nil value of
209 // WorkQueueStatus, which is an accurate description of a