Skip to content

Commit ccffb07

Browse files
committed
Close chanel when job is done
1 parent 1616999 commit ccffb07

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ HOW TO USE
3434
goos: darwin
3535
goarch: amd64
3636
pkg: github.com/vardius/gollback
37-
BenchmarkRace-4 5000000 223 ns/op 0 B/op 0 allocs/op
38-
BenchmarkAll-4 5000000 281 ns/op 40 B/op 1 allocs/op
37+
BenchmarkRace-4 500000 2961 ns/op 668 B/op 5 allocs/op
38+
BenchmarkAll-4 5000000 273 ns/op 40 B/op 1 allocs/op
3939
BenchmarkRetry-4 200000000 6.68 ns/op 0 B/op 0 allocs/op
4040
PASS
41-
ok github.com/vardius/gollback 25.130s
41+
ok github.com/vardius/gollback 32.418s
4242
```
4343

4444
## Race

gollback.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ type response struct {
3131
}
3232

3333
func (p *gollback) Race(fns ...AsyncFunc) (interface{}, error) {
34-
ctx, cancel := context.WithCancel(p.ctx)
3534
out := make(chan *response, 1)
35+
ctx, cancel := context.WithCancel(p.ctx)
36+
defer cancel()
3637

3738
for i, fn := range fns {
3839
go func(index int, f AsyncFunc) {
3940
c := make(chan *response, 1)
4041

4142
go func() {
43+
defer close(c)
4244
var r response
4345
r.res, r.err = f(ctx)
4446

@@ -67,7 +69,6 @@ func (p *gollback) Race(fns ...AsyncFunc) (interface{}, error) {
6769
}
6870

6971
r := <-out
70-
cancel()
7172

7273
return r.res, r.err
7374
}

0 commit comments

Comments
 (0)