Go concurrent worker pool with error propagation
Views
8.7K
Copies
1.7K
Likes
1.7K
Comments
0
Copy rate
20.0%
Prompt
Act as a senior Go engineer. Write an idiomatic concurrent worker pool in Go.
Requirements:
- Pool size: [worker_count] workers
- Job type: [job_type]
- Use `context.Context` for cancellation — first error cancels all workers
- Use `errgroup.Group` from `golang.org/x/sync/errgroup`
- Bounded channel of jobs, workers read until channel closes
- Results streamed on an output channel preserving job->result association
- Graceful shutdown on SIGINT / SIGTERM
- Fail-fast option controlled by a `FailFast bool` field
- Clean shutdown: all goroutines must exit, no leaks
- Public API: `Pool.Run(ctx, jobs []Job) ([]Result, error)`
- Include a `main()` demo that submits 20 jobs, one of which errors, and prints the outcome
Output format:
- A single `main.go` file, complete and runnable
- Use idiomatic Go: lowercase package `main`, clear names, no global state
- No external deps beyond `golang.org/x/sync/errgroup`
- Add concise doc comments on exported identifiersCustomise this prompt
Fill in 2 variables to personalise this prompt
Preview
Act as a senior Go engineer. Write an idiomatic concurrent worker pool in Go.
Requirements:
- Pool size: [worker_count] workers
- Job type: [job_type]
- Use `context.Context` for cancellation — first error cancels all workers
- Use `errgroup.Group` from `golang.org/x/sync/errgroup`
- Bounded channel of jobs, workers read until channel closes
- Results streamed on an output channel preserving job->result association
- Graceful shutdown on SIGINT / SIGTERM
- Fail-fast option controlled by a `FailFast bool` field
- Clean shutdown: all goroutines must exit, no leaks
- Public API: `Pool.Run(ctx, jobs []Job) ([]Result, error)`
- Include a `main()` demo that submits 20 jobs, one of which errors, and prints the outcome
Output format:
- A single `main.go` file, complete and runnable
- Use idiomatic Go: lowercase package `main`, clear names, no global state
- No external deps beyond `golang.org/x/sync/errgroup`
- Add concise doc comments on exported identifiers