Skip to content
Tutorials Center
My learning

Topics

Lesson 67 of 147 ยท Concurrency

Buffered Channels and select

Learn buffered channels, backpressure and the Go select statement - multiple cases, default, timeouts with time.After, tickers, select loops, nil channels and fan-in.

Unbuffered channels force sender and receiver to meet. That is perfect for hand-offs, but sometimes you want a small queue between two goroutines so the sender can run ahead a little. That is what a buffered channel gives you. And once a goroutine has more than one channel to watch, plain <-ch is not enough, because it waits on exactly one. The select statement waits on several channels at once and reacts to whichever is ready first.

This lesson covers buffering and backpressure, len and cap on channels, select with several cases, the default case for non-blocking operations, timeouts, tickers, select inside loops, switching cases off with nil channels, and fan-in.

Buffered channels

Give make a second argument to create a channel with room for that many values:

Go
jobs := make(chan string, 3) // holds up to 3 values

The rules change slightly:

  • A send blocks only when the buffer is full.
  • A receive blocks only when the buffer is empty.
  • Values leave in the order they entered (first in, first out).
Go Sending without a receiver, up to the capacity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

len(ch) is the number of values currently waiting and cap(ch) is the buffer size. The output is len: 3 cap: 3, then a b c, then len after: 0. Nobody receives while the three sends happen, and still nothing blocks, which would be a deadlock with an unbuffered channel.

Use len(ch) for debugging or metrics only. In concurrent code the value can be out of date the moment you read it, so decisions like "if len(ch) < cap(ch) then send" are racy. To test whether a send would block, use select with default, shown below.

Backpressure

The real purpose of a buffer is to decouple the speed of a producer from that of a consumer for a short time. When the buffer fills up, the producer blocks until the consumer catches up. That automatic slowing down is called backpressure, and it protects your program from a fast producer flooding memory.

Go A full buffer with no consumer (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The producer fills the two slots, blocks on the third send, and main waits for the producer, so nothing can move. The runtime reports fatal error: all goroutines are asleep - deadlock!, with the producer shown as goroutine 4 [chan send] and main as [sync.WaitGroup.Wait]. Here is the corrected idea: the consumer runs, and the producer can never get more than two items ahead of it.

Go Producer and consumer with a small buffer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The consumer prints consumer sum: 55. Whatever the timing, the producer is never more than the buffer size ahead of the consumer, and close followed by range makes the consumer stop cleanly.

Bigger buffers are not automatically better. A large buffer hides problems and uses memory; it only delays the moment the producer has to wait. Start with unbuffered, or a buffer sized for a clear reason: the exact number of values you will send (so the sender never blocks), or a small queue to smooth out bursts.

The select statement

select looks like a switch, but each case is a channel operation. It waits until at least one case can proceed, runs that case, and then leaves the select. If several are ready at the same time, Go picks one at random, so no case can starve the others.

Go Waiting for the first of two channels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Only fast has a value, so the first case runs and prints received: fast result. If neither channel were ready, select would block until one is. A select with no cases at all, select {}, blocks forever.

A case can also be a send:

Go
select {
case out <- value:
	// the value was sent
case <-quit:
	return
}

This is the standard way to send "unless we are told to stop", so that a goroutine does not get stuck on a send nobody will receive.

Receive with comma-ok in a case

A receive case can capture the ok flag too, which matters when a channel might be closed:

Go Detecting a closed channel in select

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The first pass prints value: 99, the second data channel closed. A closed channel is always ready to receive, which is why select over a closed channel never blocks. Without checking ok, a loop around such a select would spin and receive zero values forever.

Non-blocking operations with default

A select with a default case never blocks: if no other case is ready right now, default runs immediately.

Go Try to send and receive without blocking

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The output is nothing to receive, sent 1, buffer full, dropped 2. This pattern is useful when dropping data is better than waiting, for example a log or metrics channel that must never slow down the main program. Be careful not to use default inside a tight loop without any waiting: the loop would spin and use 100 percent of a core.

Timeouts with time.After

time.After(d) returns a channel that receives the current time once after the duration d. Using it as a select case turns any wait into a wait with a deadline:

Go Giving up after a timeout

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The first wait gives up, printing timed out after 50ms, and the second succeeds with result: finished because 300 ms is shorter than two seconds. Notice that the channel in slowOperation has a buffer of 1. When the caller walks away after a timeout, the abandoned goroutine can still complete its send into the buffer and exit, instead of leaking forever on an unbuffered channel nobody reads.

For timeouts that need to stop work rather than just stop waiting, use a context. time.After is the simple tool for a single wait. Since Go 1.23 an unused timer is garbage collected even if it has not fired yet, so time.After in a loop is no longer the memory problem it used to be; still, for tight loops a reusable time.Timer is cheaper (see Time, Timers and Tickers).

Tickers

A time.Ticker delivers the time on its channel repeatedly at a fixed interval. Combine it with select to do something periodically until you are told to stop. Always call Stop when done:

Go A ticker and a stop signal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The program prints ticked at least 4 times: true; the exact count depends on timing, so we only assert a safe lower bound. The label loop: matters: a bare break inside a select leaves only the select, not the surrounding for (labels, break and goto explain this).

select in a loop

Most real uses put select inside a for loop, so a goroutine keeps serving events until it receives a quit signal. Here a goroutine handles work and reports when it is told to stop:

Go An event loop with a quit channel

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

work is unbuffered, so each send is accepted before the next begins, and by the time close(quit) runs, all four values (1+2+3+4) have been handled. The output is handled total: 10. Closing quit is the broadcast-stop idiom from the channels lesson; it would stop any number of goroutines listening on it.

Turning cases off with nil channels

A send or receive on a nil channel blocks forever, so inside select such a case is never chosen. You can use that to switch a case off dynamically by assigning nil to the channel variable. The classic use is merging two channels until both are closed:

Go Merging two channels with nil to disable closed ones

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Without the nil assignment, a closed channel would be ready on every iteration, returning zero values and spinning the loop. With it, the loop ends exactly when both sources are exhausted. The result, after sorting, is [1 2 3 4 5].

Fan-in

Fan-in means combining several input channels into one output channel. Each input gets its own goroutine that forwards values; a WaitGroup closes the output when all inputs are drained. This is simpler than the nil-channel loop when the number of inputs is not fixed:

Go A fan-in helper for any number of channels

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

The output is [1 2 3 10 11 12 100 101]. The helper uses a generic function so it works for any element type. The same fan-in step appears in the larger designs of concurrency patterns.

Choosing the right buffer size

  • Unbuffered (0): you want synchronization, a guaranteed hand-off, or you are not sure. This is the default choice.
  • Exactly N: you know you will send N values and want the sender never to block, such as one result per worker.
  • Small queue: smoothing bursts between two stages; pick a number from measuring, not guessing.
  • Never use a huge buffer as a way to "fix" a deadlock. It only postpones it.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. When does a send on a buffered channel make(chan int, 2) block?

    A buffered send blocks only when the buffer is full. With capacity 2, the third send waits for a receive.
  2. 2. If two cases of a select are ready at the same time, which one runs?

    Go picks uniformly at random among the ready cases, so no case can starve another.
  3. 3. What does the default case in a select do?

    With default, select becomes non-blocking: if nothing can proceed right now, default runs at once.
  4. 4. Inside for { select { ... case <-quit: break } }, what does the break do?

    A plain break leaves the innermost select (or switch or for). Use a labeled break or return to leave the outer loop.
  5. 5. Why set a channel variable to nil inside a select loop?

    Operations on a nil channel block forever, so select never picks that case. This is how a loop stops listening to a source that has finished.

Summary

  • make(chan T, n) creates a buffer of n: sends block only when it is full, receives only when it is empty. len and cap report the state, but use them for debugging only.
  • A full buffer slows the producer down; this backpressure is a feature. Prefer unbuffered or a deliberate size.
  • select waits for the first ready channel operation (send or receive), choosing randomly when several are ready.
  • default makes a select non-blocking, which suits drop-on-overflow and polling.
  • time.After gives a timeout, time.NewTicker a repeating event (stop it with Stop); use labeled break or return to leave a loop from inside a select.
  • A nil channel in a select is never chosen, which lets you disable sources that finished.
  • Fan-in merges many channels into one: a goroutine per input, and a closer goroutine that waits on a WaitGroup.

Next: The sync Package covers mutexes, Once, Cond and the other tools for sharing memory safely.