/go-worker-pool

A re-useable golang worker pool

Primary LanguageGo

go-worker-pool

Build Status codecov Go Report Card

Thank you for all the good libraries and Articles:

  1. Tunny
  2. Wpool
  3. Handling 1 millon requests per minute with golang
  4. Visually understanding worker pool

How to use this?

Install package

$ go get github.com/jayhuang75/go-worker-pool

In your application main.go, import the package

import (
    "github.com/jayhuang75/go-worker-pool"
)

Example how to use the worker pool

// Person Struct
type Person struct {
    Name string
    Age  int
}

// Processor func
// This is the custom process function
func Processor(resource interface{}) error {
    // fmt.Printf("worker: started, working for %s\n", resource)
    fmt.Printf(">>>>>>>>>>>>>> %s \n", resource.(Person).Name+" ok")
    return nil
}

// ResultProcessor func 
// We can catch all the failed and retry in here
func Result(result worker.Result) error {
    fmt.Printf("Result processor got error: %s\n", result.Err)
    fmt.Printf("Result processor got result: %d\n", result.Job)
    return nil
}

func main() {

    p1 := Person{"apple ", 3}
    p2 := Person{"orange", 8}
    p3 := Person{"pear", 35}
    p4 := Person{"pizza", 3}
    p5 := Person{"cafe", 8}

    persons := []Person{p1, p2, p3, p4, p5}

    numCPUs := runtime.NumCPU()

    // convert the Struct to the interface
    resources := make([]interface{}, len(persons))
    for i, s := range persons {
        resources[i] = s
    }

    pool := worker.NewWorkerPool(numCPUs)
    pool.Log(false) // remove heavry logging
    pool.Start(resources, Processor, Result)

}