-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathmain.go
83 lines (69 loc) · 2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Credit:
// https://gobyexample.com/worker-pools
// Worker pool benefits:
// - Efficiency because it distributes the work across threads.
// - Flow control: Limit work in flight
// Disadvantage of worker:
// Lifetimes complexity: clean up and idle worker
// Principles:
// Start goroutines whenever you have the concurrent work to do.
// The goroutine should exit as soon as posible the work is done. This helps us
// to clean up the resources and manage the lifetimes correctly.
package main
import (
"fmt"
"sync"
"time"
)
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
fmt.Println("worker", id, "started job", j)
time.Sleep(time.Second)
fmt.Println("worker", id, "fnished job", j)
results <- j * 2
}
}
func workerEfficient(id int, jobs <-chan int, results chan<- int) {
// sync.WaitGroup helps us to manage the job
var wg sync.WaitGroup
for j := range jobs {
wg.Add(1)
// we start a goroutine to run the job
go func(job int) {
// start the job
fmt.Println("worker", id, "started job", job)
time.Sleep(time.Second)
fmt.Println("worker", id, "fnished job", job)
results <- job * 2
wg.Done()
}(j)
}
// With a help to manage the lifetimes of goroutines
// we can add more handler when a goroutine finished
wg.Wait()
}
func main() {
const numbJobs = 8
jobs := make(chan int, numbJobs)
results := make(chan int, numbJobs)
// 1. Start the worker
// it is a fixed pool of goroutines receive and perform tasks from a channel
// In this example, we define a fixed 3 workers
// they receive the `jobs` from the channel jobs
// we also naming the worker name with `w` variable.
for w := 1; w <= 3; w++ {
go workerEfficient(w, jobs, results)
}
// 2. send the work
// other goroutine sends the work to the channels
// in this example, the `main` goroutine sends the work to the channel `jobs`
for j := 1; j <= numbJobs; j++ {
jobs <- j
}
close(jobs)
fmt.Println("Closed job")
for a := 1; a <= numbJobs; a++ {
<-results
}
close(results)
}