workerpool

Test status Go Report Card GoDev

Lightweight and fast worker pool with generics support.

Go version: >=1.18

Based on: valyala/fasthttp/workerpool.go

Example:

type Foo struct {
    A int
    B int
}

wg := sync.WaitGroup{}

handler := func(args Foo) {
    log.Println(args.A + args.B)
    wg.Done()
}

wp := New(
    Config{
        MaxWorkersCount:       100,
        MaxIdleWorkerDuration: 5 * time.Second,
    },
    handler,
)

for i := 0; i < 10; i++ {
    wg.Add(1)
    wp.Exec(Foo{A: i, B: i + 1})
}

wg.Wait()

GitHub

View Github