Minimal "hello world" example demonstrating the simplest pool usage.
- Creating a pool with
pool.New[T] - Using
pool.WorkerFuncadapter for simple functions - Submitting work items with
Submit() - Closing pool and waiting with
Close() - Basic metrics via
Metrics().GetStats()
go run main.goprocessing: date
processing: apple
processing: banana
processing: cherry
processing: elderberry
done: processed 5 items in 102ms
p := pool.New[string](3, pool.WorkerFunc[string](func(_ context.Context, item string) error {
fmt.Printf("processing: %s\n", item)
time.Sleep(50 * time.Millisecond)
return nil
}))pool.New[string](3, ...)creates a pool with 3 workers processing stringspool.WorkerFuncadapts a function to theWorkerinterface
p.Go(ctx) // start workers
p.Submit("apple") // submit work (not thread-safe)
p.Close(ctx) // close input and wait for completionstats := p.Metrics().GetStats()
fmt.Printf("processed %d items in %v\n", stats.Processed, stats.TotalTime)After understanding this basic example, explore:
- chunking - consistent work distribution by key
- tokenizer_stateful - workers with state
- middleware - adding retry, timeout, validation