-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_options.go
49 lines (38 loc) · 1.06 KB
/
map_options.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
package parallel
const (
MapDefaultConcurrency = 10
)
type MapOption interface {
isMapOption()
}
// mapOptionConcurrency sets concurrency for parallel execution.
type mapOptionConcurrency struct{ concurrency int }
// mapOptionStopOnFirstError force executor to stop processing right after the first error occurred.
type mapOptionStopOnFirstError struct{}
func (o mapOptionConcurrency) isMapOption() {}
func (o mapOptionStopOnFirstError) isMapOption() {}
func WithMapConcurrency(concurrency int) MapOption {
return mapOptionConcurrency{concurrency: concurrency}
}
func WithMapStopOnFirstError() MapOption {
return mapOptionStopOnFirstError{}
}
type mapOptions struct {
concurrency int
stopOnFirstError bool
}
func parseMapOptions(opts []MapOption) mapOptions {
ret := mapOptions{
concurrency: MapDefaultConcurrency,
stopOnFirstError: false,
}
for i := range opts {
switch opt := opts[i].(type) {
case mapOptionConcurrency:
ret.concurrency = opt.concurrency
case mapOptionStopOnFirstError:
ret.stopOnFirstError = true
}
}
return ret
}