Skip to content

Commit a6776fb

Browse files
committed
Support failing fast
1 parent ed2dd47 commit a6776fb

File tree

12 files changed

+197
-47
lines changed

12 files changed

+197
-47
lines changed

README.md

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Simplifies creating a pool of workers that execute jobs in parallel
66

77
* Easy to use
88
* Context Support
9+
* Fail fast with errors
910
* Customizable Pool Size
1011
* Default number of workers is 10
1112
* Customizable Job Queue Size
@@ -29,16 +30,18 @@ func main() {
2930
group := parallelizer.NewGroup()
3031
defer group.Close()
3132

32-
group.Add(func() {
33+
group.Add(func() error {
3334
for char := 'a'; char < 'a'+3; char++ {
3435
fmt.Printf("%c ", char)
3536
}
37+
return nil
3638
})
3739

38-
group.Add(func() {
40+
group.Add(func() error {
3941
for number := 1; number < 4; number++ {
4042
fmt.Printf("%d ", number)
4143
}
44+
return nil
4245
})
4346

4447
err := group.Wait()
@@ -76,16 +79,20 @@ func main() {
7679
group := parallelizer.NewGroup()
7780
defer group.Close()
7881

79-
group.Add(func() {
82+
group.Add(func() error {
8083
time.Sleep(2 * time.Second)
8184

8285
fmt.Println("Finished work 1")
86+
87+
return nil
8388
})
8489

85-
group.Add(func() {
90+
group.Add(func() error {
8691
time.Sleep(2 * time.Second)
8792

8893
fmt.Println("Finished work 2")
94+
95+
return nil
8996
})
9097

9198
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
@@ -128,8 +135,9 @@ func main() {
128135

129136
for i := 1; i <= 10; i++ {
130137
i := i
131-
group.Add(func() {
138+
group.Add(func() error {
132139
fmt.Print(i, " ")
140+
return nil
133141
})
134142
}
135143

@@ -167,8 +175,9 @@ func main() {
167175

168176
for i := 1; i <= 10; i++ {
169177
i := i
170-
group.Add(func() {
178+
group.Add(func() error {
171179
fmt.Print(i, " ")
180+
return nil
172181
})
173182
}
174183

@@ -206,8 +215,9 @@ func main() {
206215
defer group.Close()
207216

208217
for i := 1; i <= 10; i++ {
209-
group.Add(func() {
218+
group.Add(func() error {
210219
time.Sleep(time.Second)
220+
return nil
211221
})
212222

213223
fmt.Println("Job added at", time.Now().Format("04:05"))
@@ -257,8 +267,9 @@ func main() {
257267
defer group.Close()
258268

259269
for i := 1; i <= 10; i++ {
260-
group.Add(func() {
270+
group.Add(func() error {
261271
time.Sleep(time.Second)
272+
return nil
262273
})
263274

264275
fmt.Println("Job added at", time.Now().Format("04:05"))
@@ -307,8 +318,9 @@ func main() {
307318
group := parallelizer.NewGroup()
308319
defer group.Close()
309320

310-
group.Add(func() {
321+
group.Add(func() error {
311322
fmt.Println("Finished work")
323+
return nil
312324
})
313325

314326
fmt.Println("We did not wait!")
@@ -340,12 +352,14 @@ func main() {
340352
group := parallelizer.NewGroup()
341353
defer group.Close()
342354

343-
group.Add(func() {
355+
group.Add(func() error {
344356
fmt.Println("Worker 1")
357+
return nil
345358
})
346359

347-
group.Add(func() {
360+
group.Add(func() error {
348361
fmt.Println("Worker 2")
362+
return nil
349363
})
350364

351365
fmt.Println("Waiting for workers 1 and 2 to finish")
@@ -354,8 +368,9 @@ func main() {
354368

355369
fmt.Println("Workers 1 and 2 have finished")
356370

357-
group.Add(func() {
371+
group.Add(func() error {
358372
fmt.Println("Worker 3")
373+
return nil
359374
})
360375

361376
fmt.Println("Waiting for worker 3 to finish")
@@ -377,4 +392,47 @@ Workers 1 and 2 have finished
377392
Waiting for worker 3 to finish
378393
Worker 3
379394
Worker 3 has finished
395+
```
396+
397+
## Example 9
398+
399+
Showing an example with a failed task.
400+
401+
```go
402+
package main
403+
404+
import (
405+
"errors"
406+
"fmt"
407+
"time"
408+
409+
"github.com/shomali11/parallelizer"
410+
)
411+
412+
func main() {
413+
group := parallelizer.NewGroup()
414+
defer group.Close()
415+
416+
group.Add(func() error {
417+
return errors.New("something went wrong")
418+
})
419+
420+
group.Add(func() error {
421+
time.Sleep(10 * time.Second)
422+
return nil
423+
})
424+
425+
err := group.Wait()
426+
427+
fmt.Println()
428+
fmt.Println("Done")
429+
fmt.Printf("Error: %v", err)
430+
}
431+
```
432+
433+
Output:
434+
435+
```text
436+
Done
437+
Error: something went wrong
380438
```

examples/1/example1.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ func main() {
1010
group := parallelizer.NewGroup()
1111
defer group.Close()
1212

13-
group.Add(func() {
13+
group.Add(func() error {
1414
for char := 'a'; char < 'a'+3; char++ {
1515
fmt.Printf("%c ", char)
1616
}
17+
return nil
1718
})
1819

19-
group.Add(func() {
20+
group.Add(func() error {
2021
for number := 1; number < 4; number++ {
2122
fmt.Printf("%d ", number)
2223
}
24+
return nil
2325
})
2426

2527
err := group.Wait()

examples/2/example2.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@ func main() {
1212
group := parallelizer.NewGroup()
1313
defer group.Close()
1414

15-
group.Add(func() {
15+
group.Add(func() error {
1616
time.Sleep(2 * time.Second)
1717

1818
fmt.Println("Finished work 1")
19+
20+
return nil
1921
})
2022

21-
group.Add(func() {
23+
group.Add(func() error {
2224
time.Sleep(2 * time.Second)
2325

2426
fmt.Println("Finished work 2")
27+
28+
return nil
2529
})
2630

2731
ctx, cancel := context.WithTimeout(context.Background(), time.Second)

examples/3/example3.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ func main() {
1212

1313
for i := 1; i <= 10; i++ {
1414
i := i
15-
group.Add(func() {
15+
group.Add(func() error {
1616
fmt.Print(i, " ")
17+
return nil
1718
})
1819
}
1920

examples/4/example4.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ func main() {
1212

1313
for i := 1; i <= 10; i++ {
1414
i := i
15-
group.Add(func() {
15+
group.Add(func() error {
1616
fmt.Print(i, " ")
17+
return nil
1718
})
1819
}
1920

examples/5/example5.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ func main() {
1212
defer group.Close()
1313

1414
for i := 1; i <= 10; i++ {
15-
group.Add(func() {
15+
group.Add(func() error {
1616
time.Sleep(time.Second)
17+
return nil
1718
})
1819

1920
fmt.Println("Job added at", time.Now().Format("04:05"))

examples/6/example6.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ func main() {
1212
defer group.Close()
1313

1414
for i := 1; i <= 10; i++ {
15-
group.Add(func() {
15+
group.Add(func() error {
1616
time.Sleep(time.Second)
17+
return nil
1718
})
1819

1920
fmt.Println("Job added at", time.Now().Format("04:05"))

examples/7/example7.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ func main() {
1111
group := parallelizer.NewGroup()
1212
defer group.Close()
1313

14-
group.Add(func() {
14+
group.Add(func() error {
1515
fmt.Println("Finished work")
16+
return nil
1617
})
1718

1819
fmt.Println("We did not wait!")

examples/8/example8.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ func main() {
1010
group := parallelizer.NewGroup()
1111
defer group.Close()
1212

13-
group.Add(func() {
13+
group.Add(func() error {
1414
fmt.Println("Worker 1")
15+
return nil
1516
})
1617

17-
group.Add(func() {
18+
group.Add(func() error {
1819
fmt.Println("Worker 2")
20+
return nil
1921
})
2022

2123
fmt.Println("Waiting for workers 1 and 2 to finish")
@@ -24,8 +26,9 @@ func main() {
2426

2527
fmt.Println("Workers 1 and 2 have finished")
2628

27-
group.Add(func() {
29+
group.Add(func() error {
2830
fmt.Println("Worker 3")
31+
return nil
2932
})
3033

3134
fmt.Println("Waiting for worker 3 to finish")

examples/9/example9.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"time"
7+
8+
"github.com/shomali11/parallelizer"
9+
)
10+
11+
func main() {
12+
group := parallelizer.NewGroup()
13+
defer group.Close()
14+
15+
group.Add(func() error {
16+
return errors.New("something went wrong")
17+
})
18+
19+
group.Add(func() error {
20+
time.Sleep(10 * time.Second)
21+
return nil
22+
})
23+
24+
err := group.Wait()
25+
26+
fmt.Println()
27+
fmt.Println("Done")
28+
fmt.Printf("Error: %v", err)
29+
}

0 commit comments

Comments
 (0)