Market Flash
Mega-cap AI budgets are moving from pilot projects to core planning cycles
Cyber resilience spending is climbing as boards rethink operational risk
CEO succession is turning into a valuation issue for large public companies
Payments and software deal talk is heating up again across the market
Margin discipline is still winning earnings season when demand stays intact
GoSarah Chen

Go Concurrency Patterns for High-Performance APIs

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()
}

More Stories