Flamepool is a worker pool implementation for Go inspired on the thread pool pattern.
go get github.com/mercadolibre/flamepool
You can use either an explicit task or annonymous function, both alternatives allows you to accomplish the same thing. The main difference is that the annonymous function is better for simpler scenarios because probably you wont need to pass a lot of parameters to the function.
In the other hand: if you need (e.g) an http rest client, a db client and a cache access to accomplish the task, probably using the explicit task is better because you can see more clearly which things the task requires to work.
Flamepool allows running tasks distributed in many workers as you need.
An explicit task must implement the following method, which is the method that the workers will execute for every element in the pool
Do(element interface{}) (interface{}, error)
package main
import (
"fmt"
fp "github.com/mercadolibre/flamepool"
)
func main() {
// The things you want to process through the workers concurrently
elements := []string{"Bart", "Lisa", "Maggie", "Abraham", "Homer", "Marge", "Mona"}
// The workers quantity. Choose as many as you want!
workers := 2
pool := fp.New(workers, elements)
results, runErr := pool.Run(AddLastnameTask{})
if runErr != nil {
fmt.Println("Error running task")
return
}
fmt.Println("Successful: ")
for _, succ := range results.Successful {
fmt.Println(succ)
}
fmt.Println("\nErrors: ")
for _, er := range results.Errors {
fmt.Println(er)
}
}
type AddLastnameTask struct {
}
// A valid task implements the "Do" method
func (task AddLastnameTask) Do(name interface{}) (interface{}, error) {
return name.(string) + " " + "Simpson", nil
}
The output of this little program will be:
Successful:
Bart Simpson
Lisa Simpson
Maggie Simpson
Abraham Simpson
Homer Simpson
Marge Simpson
Mona Simpson
Errors:
package main
import (
"fmt"
fp "github.com/mercadolibre/flamepool"
)
func main() {
// The things you want to process through the workers concurrently
elements := []string{"Bart", "Lisa", "Maggie", "Abraham", "Homer", "Marge", "Mona"}
// The workers quantity. Choose as many as you want!
workers := 2
pool := fp.New(workers, elements)
// Using the "anonymous" function, which receives each item from elements slice as "name" parameter
task := func(name interface{}) (interface{}, error) {
return name.(string) + " " + "Simspon", nil
}
results, runErr := pool.Run(task)
if runErr != nil {
fmt.Println("Error running task")
return
}
fmt.Println("Successful: ")
for _, succ := range results.Successful {
fmt.Println(succ)
}
fmt.Println("\nErrors: ")
for _, er := range results.Errors {
fmt.Println(er)
}
}
The output of this little program will be the same of the task explicit approach :
Successful:
Bart Simpson
Lisa Simpson
Maggie Simpson
Abraham Simpson
Homer Simpson
Marge Simpson
Mona Simpson
Errors:
Please refer to contribution guidelines for submitting patches and additions. In general, we follow the "fork-and-pull" Git workflow.
- Fork the repo on GitHub
- Clone the project to your own machine
- Commit changes to your own branch
- Push your work back up to your fork
- Submit a Pull request so that we can review your changes