| name | go-context-cancellation |
| description | Context cancellation patterns for graceful shutdown |
Context Cancellation
Pattern
Pass context.Context as first parameter. Check ctx.Done() in goroutines and long operations.
CORRECT
func ProcessItems(ctx context.Context, items []string) error {
for _, item := range items {
select {
case <-ctx.Done():
return ctx.Err()
default:
if err := processItem(item); err != nil {
return err
}
}
}
return nil
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := ProcessItems(ctx, items); err != nil {
log.Printf("processing failed: %v", err)
}
}
WRONG - No context checking
func ProcessItems(items []string) error {
for _, item := range items {
if err := processItem(item); err != nil {
return err
}
}
return nil
}
Rules
- Context is first parameter:
func Do(ctx context.Context, ...)
- Always call
cancel() via defer to prevent leaks
- Check
ctx.Done() in loops and before expensive operations
- Propagate context to downstream calls
- Use
context.WithTimeout or WithDeadline for time limits
Common Patterns
func handler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
result, err := fetchData(ctx)
}
func worker(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case work := <-workCh:
process(work)
}
}
}