liveBook · Manning

Channel capacity

As explained in Learn Concurrent Programming with Go

Channel capacity is the maximum number of messages a channel can hold before a send must wait for receiving capacity.

Channel capacity is the amount of message-holding space available before a send must wait for receiving capacity. Treating that capacity as a mutually exclusive resource makes channel deadlocks easier to reason about. section 11.3

A deadlock appears when goroutines hold channel resources while requesting others, creating a circular wait. With unbuffered channels, sending and receiving must coordinate directly, so two channels can block each handler while it waits on the other. section 11.3 section 11.5

Recursive directory traversal

One goroutine reads directories and sends paths through a files channel; another processes files and sends discovered directories through a dirs channel. The program prints several files, then can stop when both handlers wait on the channels needed by the other. The example uses a 60-second sleep rather than structured completion, making the blocked state observable.

Avoid circular waits by changing how channel capacity is consumed:

  • Redesign message flows when separate goroutines or select only disguise a circular dependency. section 11.5

How Learn Concurrent Programming with Go covers it

How deep it goes
Develops a two-channel deadlock through runnable Go listings and alternatives, but stops short of detailed buffered-channel mechanics.
Examples
Uses recursive directory traversal with file and directory channels, observed output, and select- or goroutine-based repairs.
What it assumes
Assumes basic Go channels and goroutines while teaching channel deadlock reasoning through a concrete two-handler pipeline.
What you can do after
Diagnose channel deadlocks and apply separate send goroutines, select, or redesigned message flows to avoid circular waits.
How it connects
Connects channel capacity with goroutines, deadlocks, circular waits, select, and resource allocation graphs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Concurrent Programming with Go. The section links point to the parts of the book it draws on.