
Golang Context
Keep cancellation and deadlines correct in Go HTTP handlers and retries so client disconnects and timeouts do not leak goroutines or ignore r.Context().
Overview
Golang Context is an agent skill for the Build phase that enforces idiomatic context propagation and timeout/cancel hygiene in Go HTTP and database code.
Install
npx skills add https://github.com/samber/cc-skills-golang --skill golang-contextWhat is this skill?
- Eval focus: propagate r.Context() instead of context.Background() inside handlers
- Requires *Context database APIs and http.NewRequestWithContext for outbound calls
- Covers defer cancel() immediately after context.WithTimeout to avoid leak traps
- Assertion-style scenarios for client disconnect handling via ctx.Err()
- Retry-with-timeout patterns with per-attempt deadline discipline
- Eval scenario context-background-in-handler defines five numbered assertions for handler context propagation
Adoption & trust: 3.9k installs on skills.sh; 2k GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
Your Go handlers and retries break the cancellation chain with context.Background(), non-context DB calls, or missing defer cancel() after WithTimeout.
Who is it for?
Indie builders writing Go microservices or monolith APIs who want agents to follow context best practices under test-like criteria.
Skip if: Frontend-only projects or Go codebases that do not use net/http or context-aware I/O.
When should I use this skill?
Writing or reviewing Go HTTP handlers, DB calls, or retries that must honor client cancellation and timeouts.
What do I get? / Deliverables
Handlers and helpers pass derived request contexts to database and outbound HTTP work so disconnects and deadlines propagate cleanly.
- Handlers and helpers that thread context through DB and HTTP calls
- Retry/timeout code with defer cancel() after WithTimeout
Recommended Skills
Journey fit
Context propagation is implemented while writing Go services and handlers during backend build, not as a launch or growth activity. Backend subphase fits net/http handlers, database/sql Context variants, and outbound HTTP with NewRequestWithContext.
How it compares
Use for procedural context rules instead of generic Go tutorials that skip cancellation edge cases.
Common Questions / FAQ
Who is golang-context for?
Go developers using AI agents on backend services where request cancellation, timeouts, and sql/http Context APIs matter.
When should I use golang-context?
During Build backend work when implementing HTTP handlers, DB access, external API clients, or retry loops with deadlines.
Is golang-context safe to install?
Treat it like any third-party skill: check the Security Audits panel on this page; it guides code patterns and does not by itself access your servers.
SKILL.md
READMESKILL.md - Golang Context
[ { "id": 1, "name": "context-background-in-handler", "description": "Tests whether the model propagates r.Context() instead of creating context.Background() inside an HTTP handler", "prompt": "Write a Go HTTP handler for GET /orders/:id that fetches an order from a database and calls an external payment service to get payment status. Both operations should be cancellable if the client disconnects. Use database/sql and net/http.", "trap": "Model might create context.Background() inside the handler instead of using r.Context(), breaking cancellation chain", "assertions": [ {"id": "1.1", "text": "Uses r.Context() to obtain the request context, NOT context.Background() inside the handler"}, {"id": "1.2", "text": "Passes the same context (or a derived child) to the database query (QueryRowContext or similar *Context variant)"}, {"id": "1.3", "text": "Passes the same context to the external HTTP call via http.NewRequestWithContext"}, {"id": "1.4", "text": "Does NOT use http.NewRequest (without context) for the external service call"}, {"id": "1.5", "text": "Checks ctx.Err() or handles context cancellation when the client disconnects"} ] }, { "id": 2, "name": "cancel-leak-timeout", "description": "Tests whether the model defers cancel() immediately after WithTimeout to prevent resource leaks", "prompt": "Write a Go function that retries an HTTP request up to 3 times with a 5-second timeout per attempt. Each attempt should have its own independent timeout. Return the response body as []byte or the last error.", "trap": "Model might forget to defer cancel() after WithTimeout, or create a single timeout for all retries instead of per-attempt", "assertions": [ {"id": "2.1", "text": "Creates a new context.WithTimeout for each retry attempt (not one timeout for all retries)"}, {"id": "2.2", "text": "Calls defer cancel() (or cancel() before next iteration) for every WithTimeout call"}, {"id": "2.3", "text": "Does NOT discard the cancel function with _ (e.g., ctx, _ = context.WithTimeout(...))"}, {"id": "2.4", "text": "Uses http.NewRequestWithContext to attach the per-attempt timeout context"}, {"id": "2.5", "text": "Accepts a parent context parameter and derives timeouts from it"} ] }, { "id": 3, "name": "context-value-key-type", "description": "Tests whether the model uses unexported key types for context values instead of string keys", "prompt": "Write Go middleware that extracts a tenant ID from the X-Tenant-ID header and makes it available to downstream handlers. Also write a helper function to retrieve the tenant ID from the context. Other packages in the codebase will import and use this helper.", "trap": "Model might use a plain string key like context.WithValue(ctx, \"tenant_id\", ...) which causes namespace collisions across packages", "assertions": [ {"id": "3.1", "text": "Uses an unexported type for the context key (e.g., type contextKey string or type tenantKey struct{})"}, {"id": "3.2", "text": "Does NOT use a plain string as the context key (e.g., context.WithValue(ctx, \"tenant_id\", ...))"}, {"id": "3.3", "text": "Provides a typed getter function (e.g., TenantIDFromContext) that returns the value with proper type assertion"}, {"id": "3.4", "text": "Provides a setter function or the middleware injects the value using the unexported key"}, {"id": "3.5", "text": "The getter handles the case where the value is missing from the context (returns zero value + bool or error)"} ] }, { "id": 4, "name": "context-in-struct-trap", "description": "Tests whether the model avoids storing context.Context in a struct field", "prompt": "Design a Go Worker struct that processes jobs from a channel. The worker should support graceful shutdown — when told to stop, it finishes the current job and exits. Write NewWorker, Start, and Stop methods.",