liveBook · Manning

Queue

As explained in Build and Orchestrator in Go (From Scratch)

A queue is a data structure that stores items for processing in an order determined by their insertion sequence.

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:

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

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Defines FIFO behavior and Enqueue, Dequeue, and Len operations, while reusing rather than implementing the queue itself.
Examples
Uses a Go Worker struct and an HTTP start-task handler that decodes JSON before queueing a task.
What it assumes
Assumes basic familiarity with workers, tasks, and Go structs while defining queue operations through a reused package.
What you can do after
Apply it via a queue interface to hold and process worker tasks in insertion order.
How it connects
Connects queues with workers, tasks, FIFO processing, composition, and HTTP request handlers.

Also explained in

Compare how 24 books explain queue →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.