Skip to content

Commit

Permalink
Remove unnecessary checks
Browse files Browse the repository at this point in the history
  • Loading branch information
vardius committed May 11, 2019
1 parent 12d6fc4 commit 0f887d2
Showing 1 changed file with 12 additions and 44 deletions.
56 changes: 12 additions & 44 deletions gollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,23 @@ type Gollback interface {
type gollback struct {
gollbacks []AsyncFunc
ctx context.Context
cancel context.CancelFunc
}

type response struct {
res interface{}
err error
index int
res interface{}
err error
}

func (p *gollback) Race(fns ...AsyncFunc) (interface{}, error) {
out := make(chan *response, 1)

for i, fn := range fns {
go func(index int, f AsyncFunc) {
for {
select {
case <-p.ctx.Done():
return
default:
var r response
r.res, r.err = f(p.ctx)

if p.ctx.Err() != nil {
return
}

if r.err == nil || index == len(fns)-1 {
p.cancel()
out <- &r
}
return
}
var r response
r.res, r.err = f(p.ctx)

if r.err == nil || index == len(fns)-1 {
out <- &r
}
}(i, fn)
}
Expand All @@ -73,29 +58,15 @@ func (p *gollback) All(fns ...AsyncFunc) ([]interface{}, []error) {
go func(index int, f AsyncFunc) {
defer wg.Done()

for {
select {
case <-p.ctx.Done():
return
default:
var r response
r.res, r.err = f(p.ctx)

if p.ctx.Err() != nil {
return
}

rs[index] = r.res
errs[index] = r.err
var r response
r.res, r.err = f(p.ctx)

return
}
}
rs[index] = r.res
errs[index] = r.err
}(i, fn)
}

wg.Wait()
p.cancel()

return rs, errs
}
Expand All @@ -106,10 +77,7 @@ func New(ctx context.Context) Gollback {
ctx = context.Background()
}

ctxWithCancel, cancel := context.WithCancel(ctx)

return &gollback{
ctx: ctxWithCancel,
cancel: cancel,
ctx: ctx,
}
}

0 comments on commit 0f887d2

Please sign in to comment.