* perf(baidu_photo):multi-thread upload * perf(baidu_netdisk):multi-thread upload and cache optimization * fix:LimitWriter * fix(weiyun):only one login is allowed * feat(189pc):multi threaded upload * feat(baidu_netdisk):multi threaded upload * feat(baidu_photo):multi threaded upload * feat(weiyun):multi threaded upload * perf(aliyundriver_open):optimize upload code and optimize cache * fix(weiyun):invalid directory ID * fix(baidu_netdisk):modified time * fix(baidu_netdisk,baidu_photo):upload slice error * perf(baidu_netdisk):cancel unnecessary retries * fix(limitWriter):must return a non-nil error if it returns n < len(p) * fix(aliyundrive_open):Name and Filename only use one * perf(mopan):multi-thread upload
94 lines
1.6 KiB
Go
94 lines
1.6 KiB
Go
package errgroup
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
"sync/atomic"
|
|
|
|
"github.com/avast/retry-go"
|
|
)
|
|
|
|
type token struct{}
|
|
type Group struct {
|
|
cancel func(error)
|
|
ctx context.Context
|
|
opts []retry.Option
|
|
|
|
success uint64
|
|
|
|
wg sync.WaitGroup
|
|
sem chan token
|
|
}
|
|
|
|
func NewGroupWithContext(ctx context.Context, limit int, retryOpts ...retry.Option) (*Group, context.Context) {
|
|
ctx, cancel := context.WithCancelCause(ctx)
|
|
return (&Group{cancel: cancel, ctx: ctx, opts: retryOpts}).SetLimit(limit), ctx
|
|
}
|
|
|
|
func (g *Group) done() {
|
|
if g.sem != nil {
|
|
<-g.sem
|
|
}
|
|
g.wg.Done()
|
|
atomic.AddUint64(&g.success, 1)
|
|
}
|
|
|
|
func (g *Group) Wait() error {
|
|
g.wg.Wait()
|
|
return context.Cause(g.ctx)
|
|
}
|
|
|
|
func (g *Group) Go(f func(ctx context.Context) error) {
|
|
if g.sem != nil {
|
|
g.sem <- token{}
|
|
}
|
|
|
|
g.wg.Add(1)
|
|
go func() {
|
|
defer g.done()
|
|
if err := retry.Do(func() error { return f(g.ctx) }, g.opts...); err != nil {
|
|
g.cancel(err)
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (g *Group) TryGo(f func(ctx context.Context) error) bool {
|
|
if g.sem != nil {
|
|
select {
|
|
case g.sem <- token{}:
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
g.wg.Add(1)
|
|
go func() {
|
|
defer g.done()
|
|
if err := retry.Do(func() error { return f(g.ctx) }, g.opts...); err != nil {
|
|
g.cancel(err)
|
|
}
|
|
}()
|
|
return true
|
|
}
|
|
|
|
func (g *Group) SetLimit(n int) *Group {
|
|
if len(g.sem) != 0 {
|
|
panic(fmt.Errorf("errgroup: modify limit while %v goroutines in the group are still active", len(g.sem)))
|
|
}
|
|
if n > 0 {
|
|
g.sem = make(chan token, n)
|
|
} else {
|
|
g.sem = nil
|
|
}
|
|
return g
|
|
}
|
|
|
|
func (g *Group) Success() uint64 {
|
|
return atomic.LoadUint64(&g.success)
|
|
}
|
|
|
|
func (g *Group) Err() error {
|
|
return context.Cause(g.ctx)
|
|
}
|