https://github.com/rvflash/workr
Another Golang worker pool
https://github.com/rvflash/workr
Last synced: 8 months ago
JSON representation
Another Golang worker pool
- Host: GitHub
- URL: https://github.com/rvflash/workr
- Owner: rvflash
- License: mit
- Created: 2021-07-18T17:24:44.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-04-16T18:44:34.000Z (over 2 years ago)
- Last Synced: 2025-01-28T16:16:29.685Z (10 months ago)
- Language: Go
- Size: 31.3 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Workr, another Golang worker pool
[](https://godoc.org/github.com/rvflash/workr)
[](https://github.com/rvflash/workr/actions?workflow=build)
[](https://codecov.io/gh/rvflash/workr)
[](https://goreportcard.com/report/github.com/rvflash/workr)
`workr` provides synchronization, error propagation, context cancellation and execution details
for groups of tasks running in parallel with a limited number of goroutines.
This number is by default fixed by the number of CPU.
It provides an interface similar to `sync/errgroup` to manage a group of subtasks.
## Install
go 1.20 is the minimum required version due to the usage of `errors.Join`.
```bash
$ go get -u github.com/rvflash/workr
```
## Example
Simple use case that returns the first error occurred.
```go
g := new(workr.Group)
g.Go(func() error {
return nil
})
err := g.Wait()
````
A more advanced one that returns all errors that occurred, not only the first one.
```go
var (
g = workr.New(workr.ReturnAllErrors())
ctx = context.Background()
)
g.Go(func() error {
return doSomething(ctx)
})
g.Go(func() error {
return doSomethingElse(ctx)
})
err := g.Wait()
````
It also provides a method `WaitAndReturn` to get details on each task done and
functions to list those that were successful or `SuccessfulResult` not `FailedResult`.
By creating the worker with a context, the first task on error will cancel it and so,
all tasks using it are also cancelled.
```go
oops := errors.New("oops")
g, ctx := workr.WithContext(context.Background(), workr.SetPoolSize(2))
g.Go(
func() error {
return oops
},
workr.ID(1),
workr.SkipError(oops),
)
g.Go(
func() error {
return doSomething(ctx)
},
workr.SetID(2),
)
res, err := g.WaitAndReturn()
if err != nil {
// No error expected
}
log.Println(workr.SuccessfulResult(res).IDList())
```