Skip to main content
InterviewHubPRO
Section Overview
Q23

Question 23 of 50

Part 2: Concurrency, Channels & The GMP Scheduler (16 - 32)

46%
Golang & Concurrency (50 Qs)•Part 2: Concurrency, Channels & The GMP Scheduler (16 - 32)

Implementing the Worker Pool Pattern in Go with Backpressure.

senior LevelCode ExampleGolang

In Simple Words: A worker pool limits maximum concurrent goroutines, preventing CPU thrashing and database connection exhaustion:

go
func workerPool(numWorkers int, jobs <-chan int, results chan<- int) {
	var wg sync.WaitGroup
	for i := 0; i < numWorkers; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for job := range jobs {
				results <- job * 2
			}
		}()
	}
	wg.Wait()
	close(results)
}