-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.go
More file actions
42 lines (37 loc) · 751 Bytes
/
worker.go
File metadata and controls
42 lines (37 loc) · 751 Bytes
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
package main
import (
"sync"
)
type (
Worker struct {
name string
wg *sync.WaitGroup
input <-chan *DockerBuild
output chan<- *DockerBuild
handler BuildHandler
}
)
// pool is a wrapper that allows to process a chain in a pool. It consumes all
// builds from `input` calls `handler` on them, decremts their wg and puts the
// build in `ouput`
func (w *Worker) pool(size int) {
p := make(chan bool, size)
for i := 0; i < size; i++ {
p <- true
}
defer w.wg.Done()
for b := range w.input {
w.wg.Add(1)
lock := <-p
go func(build *DockerBuild, lock bool) {
defer w.wg.Done()
w.handler(build)
p <- lock
w.output <- build
}(b, lock)
}
}
func (w *Worker) WaitAndClose() {
w.wg.Wait()
close(w.output)
}