Categorygithub.com/youjinp/workerpool
repositorypackage
0.0.7
Repository: https://github.com/youjinp/workerpool.git
Documentation: pkg.go.dev

# README

workerpool

Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks submitting tasks, no matter how many tasks are queued.

This implementation builds on the repo:

Changes made:

  • Add context support
  • Update task to have an error return value

Example

package main

import (
    "fmt"
    "github.com/youjinp/workerpool"
)

func main() {
    wp := workerpool.New(context.TODO(), 2)
    requests := []string{"alpha", "beta", "gamma", "delta", "epsilon"}

    for _, r := range requests {
        r := r
        wp.Submit(func() error {
            fmt.Println("Handling request:", r)
            return nil
        })
    }

    if err := wp.Wait(); err != nil {
        log.Fatal(err)
    }
}