Go Semaphore Pattern: How to Cap Goroutines for Safe Parallel Fetching

When fetching tens of thousands of URLs concurrently in Go, spawning an unbounded number of goroutines can exhaust file descriptors, trigger rate-limit errors, and crash downstream APIs. A buffered channel of capacity N acts as a counting semaphore, blocking the loop from launching new goroutines until a slot is free, effectively capping concurrent fetch calls at N. The golang.org/x/sync/semaphore package extends this with context-aware cancellation and weighted acquisition for tasks that consume unequal resources. For workflows that also need error propagation, the errgroup package combines concurrency limiting via SetLimit with automatic context cancellation when any goroutine fails. Together, these three patterns — buffered channel, weighted semaphore, and errgroup — cover the most common bounded-parallelism scenarios in Go production code.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in