A high-performance, thread-safe queue and dispatcher library for Go. Designed for concurrent task processing, resource management, and flexible dispatch patterns.
- Generic, thread-safe FIFO queue (
AtomicQueue) - Pluggable dispatcher (
AtomicDispatcher) for sequential or concurrent processing - Resource context injection for tasks
- Graceful shutdown and cancellation support
- Benchmark and test suite for reliability
Add to your Go project:
go get github.com/justdaile/atomic_queuepackage main
import (
"github.com/justdaile/atomic_queue"
"fmt"
"time"
)
type MyTask struct {
ID int
}
func (t MyTask) Dispatch(ctx atomicq.ResourceContext[any]) chan any {
out := make(chan any, 1)
go func() {
time.Sleep(100 * time.Millisecond)
out <- fmt.Sprintf("Task %d done", t.ID)
close(out)
}()
return out
}
func main() {
dispatcher := NewAtomicDispatcher[any, any](nil, 4) // 4 concurrent
for i := 0; i < 10; i++ {
dispatcher.Enqueue(MyTask{ID: i})
}
results := make(chan any, 10)
dispatcher.Listen(results)
for result := range results {
fmt.Println(result)
}
dispatcher.ForceStop()
}Enqueue(item T)Dequeue() (T, bool)Peek() (T, bool)Len() intIsEmpty() boolClear()ToSlice() []T
NewAtomicDispatcher(queue, resource, maxConcurrent)Listen(outChan)Stop()(graceful)ForceStop()(immediate)GetInflight()
- Inject custom resources via
ResourceContext - Use
maxConcurrent=0for sequential, predictable dispatch - Use
maxConcurrent>0for parallel, high-throughput dispatch - Implement custom
Dispatchabletypes for flexible task logic
Run all tests and benchmarks:
go test -v -bench=. .atomic_queue/
├── go.mod
├── queue.go
├── dispatcher.go
├── queue_test.go
└── dispatcher_test.go
└── examples/
└── animals/
├── main.go
└── random_animal.go
MIT