-
Notifications
You must be signed in to change notification settings - Fork 2
/
merger.go
136 lines (114 loc) · 2.9 KB
/
merger.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package par
import (
"code.google.com/p/go.net/context"
"errors"
"fmt"
. "github.com/visionmedia/go-debug"
)
var debug = Debug("par")
type RequestFunc func(ctx context.Context) error
type Par interface {
WithRedundancy(redundancy int) Par
WithConcurrency(concurrency int) Par
Do() error
DoWithContext(ctx context.Context) error
}
type parallel struct {
requests chan RequestFunc
redundancy int
concurrency int
}
func Requests(requests chan RequestFunc) Par {
return ¶llel{
requests: requests,
redundancy: 1,
concurrency: 0,
}
}
func (m *parallel) WithRedundancy(redundancy int) Par {
return ¶llel{
requests: m.requests,
redundancy: redundancy,
concurrency: m.concurrency,
}
}
func (m *parallel) WithConcurrency(concurrency int) Par {
return ¶llel{
requests: m.requests,
redundancy: m.redundancy,
concurrency: concurrency,
}
}
type response struct {
id int
err error
}
func (m parallel) enqueue(ctx context.Context, responses chan *response, done chan interface{}) int {
// helper method to execute request and toss response into responses channel
handle := func(id int, request RequestFunc, pool <-chan interface{}) {
defer func() { <-pool }()
i := id
debug(fmt.Sprintf("request: %d", i))
err := request(ctx)
responses <- &response{
id: i,
err: err,
}
}
// materialize the requests channel into an array of requests
var requests []RequestFunc
for request := range m.requests {
requests = append(requests, request)
}
// use a go routine execute calls to our remote service as per the specified
// level of concurrency
go func() {
// create a channel to simulate both a bounded and unbounded pool
pool := makePoolChan(m.concurrency)
defer close(pool)
for attempt := 1; attempt <= m.redundancy; attempt++ {
for id, request := range requests {
select {
case pool <- true:
go handle(id, request, (<-chan interface{})(pool))
case <-ctx.Done():
return
case <-done:
return
}
}
}
}()
return len(requests)
}
func (m parallel) Do() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
return m.DoWithContext(ctx)
}
func (m parallel) DoWithContext(ctx context.Context) error {
// internal communication channel
responses := make(chan *response)
// signal to indicate we're done
done := make(chan interface{})
defer close(done)
// create a marker channel to let us know once we have pushed all the requests
// onto the queue
expected := m.enqueue(ctx, responses, done)
// collect the results and return when finished
results := map[int]int{}
for len(results) != expected {
select {
case response := <-responses:
if response.err == nil {
results[response.id] = response.id
debug(fmt.Sprintf("received - %d", response.id))
}
case <-ctx.Done():
debug("timeout")
return errors.New("must have timed out")
}
}
debug("finished")
return nil
}