Generic Goroutine and Worker Manager
You can add Koi into your project as follows:
go get github.com/1995parham/koi
In Koi you first register a worker on a Pond then push your inputs. Your worker has concurrency configuration for handling inputs.
Worker has generic interface. The first generic parameter is an input rype and the second parameter is an output parameter.
package main
import (
"log"
"sync"
"time"
"github.com/1995parham/koi"
)
func main() {
pond := koi.NewPond[int, koi.NoReturn]()
var wg sync.WaitGroup
printer := func(a int) koi.NoReturn {
time.Sleep(1 * time.Second)
log.Println(a)
wg.Done()
return koi.None
}
// nolint: gomnd
printWorker := koi.MustNewWoker(printer, 2, 10)
pond.MustRegisterWorker("printer", printWorker)
for i := 0; i < 10; i++ {
wg.Add(1)
if _, err := pond.AddWork("printer", i); err != nil {
log.Printf("error while adding job: %s\n", err)
}
}
wg.Wait()
log.Println("all job added")
}
Note: pond.AddWork
is non-blocking unless worker queue is full.
- Koi: Koi is an informal name for the colored variants of C. rubrofuscus kept for ornamental purposes.
- Pond: an area of water smaller than a lake, often artificially made.