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 pageshowhide
explore
- Rate Limiting and Coalescing5 questions
- Worker Pools and Bounded Concurrency4 questions
- First Result Wins4 questions
- Pipelines and Stages4 questions
- Merging Channels4 questions
- errgroup and Structured Groups5 questions
- Semaphores and Permits4 questions
- Backpressure and Load Shedding5 questions
questions
page 2 of 2What does `semaphore.Weighted` from golang.org/x/sync give you that a buffered `chan struct{}` cannot?
basics
~20 sTwo 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.
Your racing download returns the winner's bytes while two mirror fetches are still running. What damage can those losers do?
basics
~20 sThey 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.
In a Go pipeline handing `[]byte` pixel buffers between stages, what breaks if a stage reuses its scratch buffer?
basics
~20 sA 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.
As module owner, when do you take golang.org/x/sync/errgroup into go.mod instead of hand-rolling the join?
basics
~20 sJudge 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.
A paid vendor caps your account at 100 requests per second. How do you set your client's rate, burst and coalescing policy?
basics
~20 sTreat 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.
showing 31–35 of 35