go
35 lines · 1 tab
Leah Thompson
Jan 2026
1 tab
package pool
import "context"
type Job func(ctx context.Context) error
type Pool struct {
jobs chan Job
}
func New(workers, buffer int, ctx context.Context) *Pool {
p := &Pool{jobs: make(chan Job, buffer)}
for i := 0; i < workers; i++ {
go func() {
for {
select {
case <-ctx.Done():
return
case job := <-p.jobs:
_ = job(ctx)
}
}
}()
}
return p
}
func (p *Pool) Submit(ctx context.Context, job Job) bool {
select {
case <-ctx.Done():
return false
case p.jobs <- job:
return true
}
}
1 file · go
Explain with highlit
I avoid unbounded goroutines when processing queues; they look fine in staging and then blow up under a burst. This worker pool keeps a fixed number of workers and a bounded channel for jobs, which creates backpressure by design. The Submit call respects ctx.Done() so callers can stop enqueueing quickly during shutdown. On the worker side, each job receives the same ctx, so cancellations propagate. The most important operational detail is sizing: the job buffer should match the acceptable in-memory backlog, and the worker count should reflect downstream capacity (DB connections, API rate limits). Once you add metrics for queue length and processing time, this becomes a predictable subsystem.
Related snips
go
package api
import (
"net/http"
"runtime/debug"
)
Expose build metadata for debugging deploys
go
observability
build
by Leah Thompson
1 tab
go
package dbutil
import (
"context"
"github.com/jackc/pgconn"
Retry Postgres serialization failures with bounded attempts
go
postgres
transactions
by Leah Thompson
1 tab
rust
use crossbeam::channel::unbounded;
use std::thread;
fn main() {
let (tx, rx) = unbounded();
Crossbeam for advanced concurrent data structures
rust
concurrency
lock-free
by Marcus Chen
1 tab
javascript
// Creating a Promise
const myPromise = new Promise((resolve, reject) => {
const success = true;
setTimeout(() => {
if (success) {
Promises and async/await patterns for asynchronous JavaScript
javascript
promises
async-await
by Alex Chang
1 tab
rust
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
Channels (mpsc) for message passing between threads
rust
concurrency
channels
by Marcus Chen
1 tab
typescript
export type Settled<R> =
| { status: 'fulfilled'; value: R }
| { status: 'rejected'; reason: unknown };
export interface ConcurrencyOptions {
limit: number;
Simple concurrency limiter for batch operations
node
concurrency
async
by codesnips
2 tabs
Share this code
Here's the card — post it anywhere.