Request Queue (Backpressure & Traffic Shaping)
Source: Custom
Topics: bounded channels, backpressure, graceful shutdown, generics
Problem
Implement a generic bounded Queue[T] with worker goroutines — the building block for
"request queuing, traffic shaping" at a gateway: accept what you can handle, shed load
(don't block) when overwhelmed, and drain cleanly on shutdown.
Requirements:
New(capacity, workers int, handler func(T)) *Queue[T] — start workers goroutines that call handler for each item.
Submit(item T) error — non-blocking. Returns ErrFull immediately if the queue is at capacity, ErrClosed if Close has been called.
Close() — stop accepting new items, let workers drain everything already queued, wait for all workers to exit, then return.
Submit and Close must be safe to call concurrently without panicking on a closed channel.
Type:
var ErrFull = errors.New("queue is full")
var ErrClosed = errors.New("queue is closed")
type Queue[T any] struct { ... }
func New[T any](capacity, workers int, handler func(T)) *Queue[T]
func (q *Queue[T]) Submit(item T) error
func (q *Queue[T]) Close()
Key concepts
- Buffered channel as bounded queue:
make(chan T, capacity) — select default on send gives non-blocking ErrFull (load shedding, like a 503 from a gateway).
- RWMutex around close:
Submit takes RLock while sending so it can never race with Close's Lock + close(ch) — avoids "send on closed channel" panics.
- Graceful drain: closing the channel lets
for item := range ch workers finish in-flight + queued items before exiting; sync.WaitGroup lets Close wait for that.
Run
go test -v -race -bench=. ./challenges/networking/request-queue/
Sign in to submit your solution.