A queue is a temporary holding area for items waiting to be processed. It handles items in first-in, first-out (FIFO) order, so the earliest inserted task is handled first. section 2.2 section 4.3
A queue exposes a small operational interface:
- Enqueue adds an item to the queue. section 4.3
- Dequeue removes an item for processing. section 4.3
- Len reports the current number of queued items. section 4.3
In a worker, the queue sits between task submission and execution. A manager sends a task, the worker places it in the queue, and the worker later processes it while retaining task state elsewhere. section 2.2 section 4.3
Queueing a task through an API
A Go StartTaskHandler decodes a JSON task event from an HTTP request and adds its task to the worker’s queue. The worker can then process queued tasks in FIFO order. section 5.4 section 4.3
Listing 2.6 The beginnings of the Worker struct
package worker
import (
"fmt"
"github.com/google/uuid"
"github.com/golang-collections/collections/queue"
"cube/task"
)
type Worker struct {
Name string
Queue queue.Queue
Db map[uuid.UUID]*task.Task
TaskCount int
}
The Worker struct shows a queue composed with task storage and a task counter, making the queue’s role explicit in the worker design. section 2.2























