Build the safeguards
Eight explained steps, tradeoffs, pitfalls and a checkpoint before moving on.
Read the tutorialComparison / Choose a boundary
A goroutine needs an owner and a way to finish. A bounded worker pool limits active work; cancellation must reach both producers and workers; joining them establishes that shutdown has actually completed.
This comparison separates the implementation pattern from the environment used to explore it. A general playground is useful for editing freely; this lab adds a fixed failure, a readable execution trace and a direct path from the result to its source.
| Approach | Useful when | Tradeoff | Connection to this lab |
|---|---|---|---|
| Goroutine per message | Small bounded fan-out where lifetime and resource limits are already controlled. | Unbounded input becomes unbounded concurrency. | The deliberately broken case makes that pressure visible. |
| Bounded worker pool | Control active work and apply queue backpressure. | Queue size, waiting and rejection need an explicit policy. | The safeguarded engine uses this model. |
| Buffered result channel | Decouple a known number of sends from immediate receives. | Capacity is not a cancellation or ownership policy. | Compare it locally with a receiver that returns early. |
| Go browser WebAssembly | Click and run the actual engine without installing Go. | Browser scheduling is not native multicore execution. | The public environment uses this isolated target. |
| Native Go tests and loopback server | Exercise native scheduling, request contexts and race instrumentation. | Requires Go; race instrumentation also needs a supported C toolchain. | The matching source package supplies both. |
| A general Go playground | Explore a compact language example quickly. | A custom trace and failure controls still need to be designed. | Use focused snippets for language questions and this lab for the complete worker lifetime. |
The browser runs the same Go engine compiled to WebAssembly inside a dedicated worker. Goroutines run concurrently, but this browser target is not a multicore throughput benchmark. A harness cleans up the deliberately blocked example after recording it. The native download includes an HTTP adapter and Go tests.
Run the comparisonReferences support the language and runtime concepts. The lesson, scenarios and explanations are original Zyel Lab material.
Keep exploring
Eight explained steps, tradeoffs, pitfalls and a checkpoint before moving on.
Read the tutorialChoose a pattern and an environment for the problem you actually have.
Compare approachesA beginner route, a local setup and an extension challenge.
Open the how-to guideLab discussion / Go Worker Lab
Ask about the code, compare an approach, or describe an edge case. Reader comments are reviewed before publication.
Code walkthrough
These short excerpts connect the lab’s behavior to its implementation. Each example links to the full tutorial step for its context, tradeoffs and checkpoint. Names and dependencies belong to that source; the excerpts are not standalone programs.
The safeguarded path creates a fixed number of workers and a queue whose capacity equals that count. A producer sends messages through the queue. When the queue fills, the producer waits, limiting how far it gets ahead. This is backpressure: the upstream workload must respect downstream capacity.
jobs := make(chan int, config.Workers)
for range config.Workers { /* start one worker */ }
Both the producer send and the worker receive are select operations that also observe ctx.Done. A worker waiting for its simulated downstream result has its own cancellation branch. It stops its timer and records an incomplete outcome before returning. The final join runs after those paths settle.
select {
case <-ctx.Done(): return
case jobs <- id:
}
Workers update the same counters. A mutex protects both updates and snapshots so measurements themselves do not introduce a data race. The live panel displays per-run started, completed, failed and peak values from the engine result. It does not treat process-wide goroutine counts as an exact measure of application leaks.
c.mu.Lock()
defer c.mu.Unlock()
c.active++
c.result.Started++
Every fourth message can be made to fail. That is a handled result from the simulated downstream operation, not a panic. Cancellation stops unfinished work; it does not make it successful. Run with cancellation disabled to compare completed plus failed outcomes with the message count.
if config.FailEvery > 0 && id % config.FailEvery == 0 {
c.end("failed")
} else { c.end("completed") }
The download includes a loopback HTTP adapter on 127.0.0.1:4188. It accepts a bounded JSON configuration, limits concurrent experiments, derives a deadline from the request context and returns the engine result. It is an independent local test environment, not a new public notification service.
go test ./...
go run ./cmd/server
# POST a sample configuration to /api/run