-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
121 lines (106 loc) · 2.05 KB
/
mux.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package concurrent
import (
"os"
"os/signal"
"sync"
"syscall"
)
type mux struct {
workers [] *worker
limit int
m *sync.Mutex
wg *sync.WaitGroup
continueOnError bool
process processFunc
kill chan os.Signal
}
type item struct {
index int
value interface{}
}
func newMux(limit int, continueOnError bool, process processFunc) *mux {
kill := make(chan os.Signal, 1)
signal.Notify(kill, os.Interrupt, syscall.SIGKILL, syscall.SIGTERM)
m := &mux{
workers: []*worker{},
limit: limit,
m: &sync.Mutex{},
wg: &sync.WaitGroup{},
continueOnError: continueOnError,
process: process,
kill: kill,
}
//the mux will always start with 1 worker that will create and hold the first channel
m.addWorker()
return m
}
func (m *mux) addWorker() *worker {
w := newWorker()
m.m.Lock()
defer m.m.Unlock()
m.workers = append(m.workers, w)
w.run(m.wg, m.limit, m.process, m.continueOnError)
return w
}
func (m *mux) getWorker() *worker {
for _, w := range m.workers {
if m.limit > w.count {
return w
}
}
return m.addWorker()
}
func (m *mux) waitAll() {
m.wg.Wait()
}
func (m *mux) errors() error {
for _, w := range m.workers {
for err := range w.errChan {
if err != nil {
return err
}
}
}
return nil
}
func (m *mux) closeAllInputChannels() {
for _, w := range m.workers {
close(w.in)
}
}
func (m *mux) fanOut(input <-chan interface{}) {
defer m.wg.Done()
defer m.closeAllInputChannels()
m.wg.Add(1)
i := 0
for {
select {
case v, canRead := <-input:
if !canRead {
return
}
m.getWorker().add(&item{value: v, index: i})
i++
case <-m.kill:
return
}
}
}
func (m *mux) fanIn() []interface{} {
results := make([]interface{}, m.countItems())
i := 0
for _, w := range m.workers {
for v := range w.out {
results[v.index] = v.value
i++
}
}
return results
}
func (m *mux) countItems() int {
count := 0
for _, w := range m.workers {
count += len(w.out)
}
return count
}