Context carries cancellation, deadlines, and request-scoped metadata across call boundaries. In a concurrent HTTP service, it gives downstream goroutines an escape switch when a client disconnects or an operation exceeds its deadline, preventing work from continuing without a reader. section 1.4.3 section 9.3
Use context to:
- Set an overall deadline, such as 60 seconds for all downstream operations. section 9.3
- Pass cancellation through database, HTTP, and other request-scoped operations. section 9.3
- Derive a context that cancels related work when one concurrent operation fails. section 9.3
- Keep contexts temporary and pass them as values rather than embedding them in structs. section 9.3
Bounded, coordinated feed fetching
Create a timeout at the request boundary, pass the derived context into each fetch, and defer cancellation for cleanup: `ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second); defer cancel(); fetchFeeds(ctx, urls)`. Inside `fetchFeeds`, derive `ectx` with `errgroup.WithContext(ctx)` and call `fetchFeed(ectx, url)`. A failed fetch can then cancel the remaining feed requests, while `ParseURLWithContext` observes the deadline and cancellation. section 9.3
Figure 1.4 The life cycle of a single HTTP request
Trace one HTTP request from its handler through child goroutines, database work, and downstream APIs as cancellation propagates. section 1.4.3






