go-flexpool
is a flexible and efficient worker pool implementation in Go, designed to handle tasks with different priorities seamlessly.
- Priority-Based Task Scheduling: Submit tasks with different priorities (High, Medium, Low).
- Dynamic Worker Pool Resizing: Increase or decrease the number of workers on-the-fly.
- Graceful Shutdown: Ensure all tasks are completed before shutting down the pool.
- Error Handling: Access errors encountered during task execution through an error channel.
- Concurrency: Efficiently manage concurrent task execution with Go's goroutines and channels.
Install go-flexpool
using go get
:
go get github.com/pablolagos/go-flexpool
Create a new pool with a specified number of workers and maximum tasks:
import "github.com/pablolagos/go-flexpool"
pool := flexpool.New(100, 1000) // 100 workers, max 1000 queued tasks
Submit tasks to the pool with different priorities:
err := pool.Submit(func() error {
// Your task code here
return nil
}, flexpool.MediumPriority)
if err != nil {
log.Printf("Failed to submit task: %v", err)
}
Resize the pool to adjust the number of workers dynamically:
err := pool.Resize(200) // Resize to 200 workers
if err != nil {
log.Printf("Failed to resize pool: %v", err)
}
Gracefully shutdown the pool, ensuring all tasks are completed:
err := pool.Shutdown()
if err != nil {
log.Printf("Failed to shutdown pool: %v", err)
}
Wait until all submitted tasks are completed:
pool.WaitUntilDone()
Access errors encountered during task execution through the error channel:
for err := range pool.Errors() {
log.Printf("Task error: %v", err)
}
Contributions are welcome! Please read the CONTRIBUTING.md for guidelines on how to contribute to the project.
This project is licensed under the MIT License - see the LICENSE file for details.
For questions or suggestions, feel free to open an issue or contact me at pablolagos.