executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
concurrency-patternsImplements Go concurrency patterns including goroutines, channels, worker pools, context cancellation, and synchronization for high-performance applications.
Independent GitHub snapshot · Automated classification · Original documentation governs
name: concurrency-patterns description: Implements Go concurrency patterns including goroutines, channels, worker pools, context cancellation, and synchronization for high-performance applications. license: MIT compatibility: opencode metadata: archetypes: implementation anti_triggers: debugging, inefficient, blocking response_profile: verbosity: medium directive_strength: high abstraction_level: operational version: "1.0.0" domain: go role: implementation scope: implementation output-format: code triggers: go concurrency, go goroutines, go channels, worker pool, synchronization, context cancellation
This skill provides a comprehensive overview of Go concurrency patterns, focusing on the effective use of goroutines and channels. It covers the standard library's tools for synchronization and how to manage concurrent tasks safely in high-performance applications.
sync.WaitGroup to manage task completion in worker pools.sync.Mutex or sync.RWMutex — avoid concurrent writes without locks.<-chan T for receiving only, chan<- T for sending only.context.Context to manage goroutine lifecycles and allow for graceful shutdowns.sync.WaitGroup safely.go test -race to catch concurrency issues during testing.Pattern: A bounded worker pool implementation that manages concurrency for processing tasks.
package main
import (
"fmt"
"sync"
"time"
)
func ProcessAll(items []string) []string {
var results []string
for _, item := range items {
go func(i string) {
result := process(i) // Potential race condition on results
results = append(results, result)
}(item)
}
time.Sleep(5 * time.Second) // Inadequate synchronization
return results
}
func process(item string) string {
// Simulated processing
time.Sleep(1 * time.Second)
return fmt.Sprintf("processed: %s", item)
}
Critique:
time.Sleep for synchronization, which is unreliable.package main
import (
"context"
"fmt"
"sync"
)
type Task struct {
Name string
}
type WorkerPool struct {
workers int
tasks chan Task
wg sync.WaitGroup
}
func NewWorkerPool(workers int) *WorkerPool {
return &WorkerPool{
workers: workers,
tasks: make(chan Task),
}
}
func (wp *WorkerPool) Start(ctx context.Context) {
for i := 0; i < wp.workers; i++ {
wp.wg.Add(1)
go wp.worker(ctx)
}
}
func (wp *WorkerPool) worker(ctx context.Context) {
defer wp.wg.Done()
for {
select {
case task := <-wp.tasks:
fmt.Println("Processing task:", task.Name)
process(task)
case <-ctx.Done():
return
}
}
}
func process(task Task) {
fmt.Printf("Task: %s processed\n", task.Name)
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
wp := NewWorkerPool(3)
wp.Start(ctx)
// Adding tasks
go func() {
for i := 0; i < 10; i++ {
wp.tasks <- Task{Name: fmt.Sprintf("Task-%d", i)}
}
close(wp.tasks)
}()
wp.wg.Wait() // Wait for all workers to finish processing
cancel() // Cancel context to stop any remaining goroutines
}
Advantages:
sync.WaitGroup ensures all tasks are completed.sync.Mutex or sync.RWMutex before access.time.Sleep for synchronization; opt for structured synchronization techniques instead.Authoritative documentation links for this skill's domain. The model follows markdown links at load time to resolve external references and inline content.