Files
opencloud/vendor/github.com/gookit/goutil/group.go
T
dependabot[bot] 5ebc596352 Bump github.com/gookit/config/v2 from 2.1.8 to 2.2.2
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.1.8 to 2.2.2.
- [Release notes](https://github.com/gookit/config/releases)
- [Commits](https://github.com/gookit/config/compare/v2.1.8...v2.2.2)

---
updated-dependencies:
- dependency-name: github.com/gookit/config/v2
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-06-13 10:54:58 +02:00

83 lines
1.5 KiB
Go

package goutil
import (
"context"
"github.com/gookit/goutil/structs"
"golang.org/x/sync/errgroup"
)
// ErrGroup is a collection of goroutines working on subtasks that
// are part of the same overall task.
//
// Refers:
//
// https://github.com/neilotoole/errgroup
// https://github.com/fatih/semgroup
type ErrGroup struct {
*errgroup.Group
}
// NewCtxErrGroup instance
func NewCtxErrGroup(ctx context.Context, limit ...int) (*ErrGroup, context.Context) {
egg, ctx1 := errgroup.WithContext(ctx)
if len(limit) > 0 && limit[0] > 0 {
egg.SetLimit(limit[0])
}
eg := &ErrGroup{Group: egg}
return eg, ctx1
}
// NewErrGroup instance
func NewErrGroup(limit ...int) *ErrGroup {
eg := &ErrGroup{Group: new(errgroup.Group)}
if len(limit) > 0 && limit[0] > 0 {
eg.SetLimit(limit[0])
}
return eg
}
// Add one or more handler at once
func (g *ErrGroup) Add(handlers ...func() error) {
for _, handler := range handlers {
g.Go(handler)
}
}
// RunFn func
type RunFn func(ctx *structs.Data) error
// QuickRun struct
type QuickRun struct {
ctx *structs.Data
// err error
fns []RunFn
}
// NewQuickRun instance
func NewQuickRun() *QuickRun {
return &QuickRun{
ctx: structs.NewData(),
}
}
// Add func for run
func (p *QuickRun) Add(fns ...RunFn) *QuickRun {
p.fns = append(p.fns, fns...)
return p
}
// Run all func
func (p *QuickRun) Run() error {
for i, fn := range p.fns {
p.ctx.Set("index", i)
if err := fn(p.ctx); err != nil {
return err
}
}
return nil
}