skip to content

Coordination Patterns

The compositions you build per unit of work: a pool over a jobs channel, a cancellable pipeline, a merge, a group that stops on the first failure. Each is a few lines that hide a deadlock or a leak.

part ofGo (Golang)overview, primer and where to startread it →
on this pageshow

explore

questions

page 2 of 2

What does `semaphore.Weighted` from golang.org/x/sync give you that a buffered `chan struct{}` cannot?

level: middleimportance: nice to knowfreq 28%

basics

~20 s

Two things: weights, so one unit can consume several units of capacity rather than exactly one, and a context-aware Acquire that returns an error when the caller's context is cancelled. A channel semaphore counts identical, uncancellable permits.

open as a page

Your racing download returns the winner's bytes while two mirror fetches are still running. What damage can those losers do?

level: seniorimportance: nice to knowfreq 33%

basics

~20 s

They run on after the caller has returned, so anything they touch is touched too late: a shared destination file gets a second writer, caller-owned memory gets written after the function returned, and their sockets and buffers stay held. Give each attempt private state and cancel the shared context on return.

open as a page

In a Go pipeline handing `[]byte` pixel buffers between stages, what breaks if a stage reuses its scratch buffer?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

A channel send copies only the slice header, so both stages point at the same backing array. Overwriting it for the next frame corrupts the frame already in flight, so treat a send as handing over ownership of those bytes.

open as a page

As module owner, when do you take golang.org/x/sync/errgroup into go.mod instead of hand-rolling the join?

level: principalimportance: nice to knowfreq 26%

basics

~20 s

Judge it by who bears the cost. In a binary the dependency stops with you, and errgroup beats twenty lines of join logic you must own. In a library it joins every importer's module graph, so the bar is higher.

open as a page

A paid vendor caps your account at 100 requests per second. How do you set your client's rate, burst and coalescing policy?

level: principalimportance: nice to knowfreq 32%

basics

~20 s

Treat the ceiling as a budget to allocate, not a constant to hard-code. Target below it, divide it explicitly between callers and replicas, decide whether overflow waits or is shed, and coalesce duplicate in-flight calls because each one is billable.

open as a page

showing 31–35 of 35