Go excels at building concurrent systems. This guide explores advanced concurrency patterns that will help you build APIs capable of handling millions of requests.
package main
import (
"context"
"fmt"
"sync"
"time"
)
// Worker pool pattern
type WorkerPool struct {
workers int
tasks chan func()
wg sync.WaitGroup
}
func NewWorkerPool(workers int) *WorkerPool {
pool := &WorkerPool{
workers: workers,
tasks: make(chan func(), workers*10),
}
pool.start()
return pool
}
func (p *WorkerPool) start() {
for i := 0; i < p.workers; i++ {
p.wg.Add(1)
go func() {
defer p.wg.Done()
for task := range p.tasks {
task()
}
}()
}
}
func (p *WorkerPool) Submit(task func()) {
p.tasks <- task
}
func (p *WorkerPool) Shutdown() {
close(p.tasks)
p.wg.Wait()
}



