████████╗ ██████╗ ██╗ ██╗██████╗
╚══██╔══╝██╔═══██╗██║ ██║██╔══██╗
██║ ██║ ██║██║ █╗ ██║██████╔╝
██║ ██║▄▄ ██║██║███╗██║██╔═══╝
██║ ╚██████╔╝╚███╔███╔╝██║
╚═╝ ╚══▀▀═╝ ╚══╝╚══╝ ╚═╝
tqwp
is a Golang package designed to help you manage task processing with a worker pool. It provides an easy-to-use API for enqueuing tasks, processing them concurrently with workers, and retrying failed tasks with configurable retry logic.
- 🔄 Concurrent task processing with configurable worker pools
- 🔁 Built-in retry mechanism for failed tasks
- 📊 Task processing metrics and summary
- 📝 Simple logging of task processing, retries, and failures
- 🎯 Custom task implementation through interface
- 🔧 Configurable queue size and worker count
go get -u github.com/abdullahnettoor/tqwp
type CustomTask struct {
tqwp.TaskModel // Embed TaskModel for retry functionality
Id uint
Data any
}
func (t CustomTask) Process() error {
// Implement your task logic here
return nil
}
wp := tqwp.New(&tqwp.WorkerPoolConfig{
NumOfWorkers: 10, // Number of concurrent workers
MaxRetries: 3, // Maximum retry attempts
QueueSize: 100, // Size of task queue buffer
})
wp.Start()
defer wp.Stop()
// Enqueue tasks
wp.EnqueueTask(&CustomTask{
Id: 1,
Data: "example",
})
// Get processing summary at the end
defer wp.Summary()
Check out our example implementations in the examples directory:
Option | Description | Default |
---|---|---|
NumOfWorkers | Number of concurrent workers | Required |
MaxRetries | Maximum retry attempts for failed tasks | Required |
QueueSize | Buffer size for task queue | Required |
- Go 1.21 or later
The Task
interface defines the behavior for each task:
type Task interface {
Process() error
}
The WorkerPool
manages task processing across multiple workers:
type WorkerPoolConfig struct {
NumOfWorkers int
MaxRetries int
QueueSize int
}
New(cfg *WorkerPoolConfig)
: Creates a new worker pool with provided configuration.Start()
: Starts the worker pool, distributing tasks to workers.EnqueueTask(task Task)
: Adds a task to the queue.Stop()
: Stops the worker pool and waits for all tasks to be processed.Summary()
: Prints a summary of the processing.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
This project is actively maintained. For feature requests and bug reports, please open an issue.