Luna is a flexible and generic worker pool implementation in Go, designed to manage and orchestrate workers efficiently. It supports generic types for keys and workers, allowing for a wide range of applications. Workers in the pool can be started, stopped, and have operations executed upon them, providing a robust framework for concurrent task execution.
- Generic implementation supporting any key and worker types.
- Thread-safe operations to add, delete, and manipulate workers.
- Error handling for start and stop operations of workers.
To install Luna, you need a working Go environment. Luna can be installed using go get
:
go get github.com/kaatinga/luna
Here's a simple example of how to use Luna:
package main
import (
"fmt"
"github.com/kaatinga/luna"
)
type myWorker struct{}
func (myWorker) Start() error {
fmt.Println("Worker started")
return nil
}
func (myWorker) Stop() error {
fmt.Println("Worker stopped")
return nil
}
func main() {
pool := luna.NewWorkerPool[string, myWorker]()
if err := pool.Add("worker1", myWorker{}); err != nil {
fmt.Printf("Error adding worker: %v\n", err)
}
pool.Do("worker1", func(item *luna.Item[string, myWorker]) {
fmt.Println("Executing operation on worker")
})
if err := pool.Delete("worker1"); err != nil {
fmt.Printf("Error removing worker: %v\n", err)
}
}
To run the tests for Luna, navigate to the package directory and use the Go tool:
go test ./...
Contributions are welcome! Please feel free to submit a pull request or open issues to discuss potential improvements or features.