Worker Pool Bounded Goroutines

Process a job channel with a fixed pool of workers, draining remaining jobs on shutdown and collecting per-worker stats safely.

GoResilience

Code

gosnippet
66 lines
package main

import (
	"context"
	"fmt"
	"sync"
)

type Job struct {
	ID  int
	URL string
}

func runPool(ctx context.Context, jobs []Job, workers int) (map[int]int, error) {
	jobCh := make(chan Job)
	stats := make([]int, workers)
	var wg sync.WaitGroup

	for w := 0; w < workers; w++ {
		w := w
		wg.Add(1)
		go func() {
			defer wg.Done()
			for job := range jobCh {
				select {
				case <-ctx.Done():
					return // 优雅退出:剩余任务由调用方决定去留
				default:
					fmt.Println("worker", w, "handles", job.ID)
					stats[w]++
				}
			}
		}()
	}

	dispatchDone := make(chan struct{})
	go func() {
		defer close(dispatchDone)
		for _, job := range jobs {
			select {
			case jobCh <- job:
			case <-ctx.Done():
				return
			}
		}
	}()

	<-dispatchDone
	close(jobCh) // 关闭通道即全体工人的退出信号
	wg.Wait()

	perWorker := make(map[int]int, workers)
	for w, count := range stats {
		perWorker[w] = count
	}
	return perWorker, ctx.Err()
}

func main() {
	jobs := make([]Job, 12)
	for i := range jobs {
		jobs[i] = Job{ID: i, URL: fmt.Sprintf("https://example.com/%d", i)}
	}
	stats, err := runPool(context.Background(), jobs, 4)
	fmt.Println(stats, err)
}

How it works

Worker Pool Bounded Goroutines is built around the functions runPool and main.

Iteration happens inside for w := 0; w < workers; w++ — everything outside it runs once. Work runs on a goroutine and results come back through a channel, giving a safe handoff between the two execution contexts. defer schedules the cleanup, and the scheduled call fires no matter how the function exits.

Putting it to use

The primary signature is func runPool(ctx context.Context, jobs []Job, workers int) (map[int]int, error)

The demo output is labeled (worker); each label names the call that printed it.

The sequence runPool lives in main; running the file triggers it.

Core lines

return perWorker, ctx.Err()
jobs[i] = Job{ID: i, URL: fmt.Sprintf("https://example.com/%d", i)}
dispatchDone := make(chan struct{})

Interactive Tools for This Topic

FAQ

What does Worker Pool Bounded Goroutines print when run?
Running the file directly is enough — expect labeled output lines such as `worker`.