Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

The Go Blog Unknown date 15 min read How-to mediumAdept

Go Concurrency Patterns: Pipelines and cancellation - The Go Programming Language

Go pipelines are built as stages of goroutines connected by channels, with fan-out to spread work across workers and fan-in (via a sync.WaitGroup-based merge) to combine results. When downstream stages stop reading early, upstream senders block forever and leak goroutines. Sizing buffers to avoid this is fragile. The robust fix is a shared done channel that is closed (via defer) to broadcast cancellation, with every send wrapped in a select. The patterns are applied to a parallel MD5 digest of a directory tree.

Assumes: Go syntaxgoroutines and channels

Related posts

  • Leveraging Go Worker Pools to Scale Server-side Data Sharing

    Shopify Engineering mediumAdept

    Shopify's Server Pixels service, a Go pipeline that consumes over a billion customer events per day from Kafka, fell behind on consumer lag because it spawned an unbounded number of goroutines to process and produce event batches. Replacing that with a fixed-size worker pool fed by a jobs channel, built on a reusable Worker interface and shut down gracefully with context cancellation and WaitGroups, bounded resource use. Throughput per pod rose from 7.75k to 12.9k events/sec with 15 workers, 19.3k with 50, and 21k after further tuning, a 170% gain. That headroom carried the service through a 46k events/sec peak during BFCM 2021.

  • Keep the monolith, but split the workloads

    incident.io mediumAdept

    A Go monolith can get much of the reliability isolation of microservices by splitting workloads into separate deployments of the same binary, instead of breaking it into services. After a bad Pub/Sub message caused an unhandled panic that crashed the web, worker and cron threads together for 32 minutes, incident.io split them into independent Heroku dyno tiers selected by command-line flags. They also gave each class of work its own capped Postgres connection pool and statement timeout, so low-priority jobs such as BigQuery event export or best-effort nudges fall behind rather than exhausting the shared database. The main migration hazard is code that assumes a shared process, such as process-local caches, and the fix is to move that coordination into Postgres or Redis.

  • Goroutine Leak Profiles

    The Go Blog mediumAdept

    Go 1.27 adds a goroutineleak profile that finds goroutines permanently blocked on channels or sync primitives (Mutex, RWMutex, WaitGroup, Cond) in running programs, including production, with few or no false positives. It works by modifying the garbage collector's mark phase. Only unblocked goroutines start as roots, and blocked goroutines are added iteratively when a primitive blocking them is reachable from a live goroutine. Any goroutine never reached is reported as leaked. The profile is exposed via runtime/pprof and /debug/pprof/goroutineleak and is demonstrated on an unbuffered-channel early-return leak. Limitations: it cannot see IO or syscall blocking, it misses leaks where a primitive stays reachable through globals, and pathological "daisy-chain" cases serialize marking with O(n²) inspection. Periodic, infrequent profiling is the recommended way to keep overhead low.

  • Simple trick to save environment and money when using GitHub Actions

    Turso mediumAdept

    GitHub Actions keeps running workflows for outdated pull request commits until they finish or fail. That wastes runner time, money on private repos, and quota on open source ones. Adding a `concurrency` block keyed on workflow name and PR number (falling back to the ref) with `cancel-in-progress: true` makes GitHub cancel stale runs when a PR is updated. In the first week after this change, 56 runs were cancelled in the LibSQL repository.

  • Intermittent downtime from repeated crashes

    incident.io mediumAdept

    A poison-pill Pub/Sub message crashed incident.io's Go monolith on Heroku repeatedly, causing 13 minutes of downtime over 32 minutes. The root cause was an unhandled panic inside goroutines spawned by the Google Cloud Pub/Sub client's Receive method, which a parent goroutine's recover() cannot catch. Debugging was slowed because Go's full goroutine stack dumps overwhelmed Heroku's log rate limits, and the abrupt exit also killed Sentry reporting. The team recovered by purging suspect subscriptions, then added a recover inside the message handler and split web, worker and cron work into separate Heroku dyno tiers so worker crashes can't take down request serving.

The quest for software engineering knowledge.

Look:
Layout: