-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.go
114 lines (100 loc) · 2.39 KB
/
async.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
/*!
* go-rs/async
* Copyright(c) 2019 Roshan Gade
* MIT Licensed
*/
package async
type Tasks []func() interface{}
type ETasks map[string]func() (interface{}, error)
type channel chan interface{}
// Reference: https://blog.golang.org/pipelines
// Reference: https://en.wikipedia.org/wiki/Moore%27s_law
// Reference: https://en.wikipedia.org/wiki/Amdahl%27s_law
// Rob Pike: Concurrency enables Parallelism
type Promise struct {
//TODO: Context - Cancel, Timeout
}
//
// parallel execution of all tasks
//
func (p *Promise) Parallel(tasks Tasks) []interface{} {
// can not use buffer channels, because not able to maintain output sequence
// using slice channels
workers := make([]channel, 0, len(tasks))
for _, task := range tasks {
workers = append(workers, func() channel {
out := make(channel)
go func(task func() interface{}) {
defer close(out)
out <- task()
}(task)
return out
}())
}
// gather data from all channels
out := make([]interface{}, 0, len(tasks))
for _, result := range workers {
out = append(out, <-result)
}
return out
}
//
// Parallel execution of all tasks, but returns result as well as error
//
func (p *Promise) ParallelWithMap(tasks ETasks) (map[string]interface{}, map[string]error) {
// can not use buffer channels, because not able to maintain output sequence
// using map channels
workers := make(map[string]channel)
for key, task := range tasks {
workers[key] = func() channel {
out := make(channel)
go func(task func() (interface{}, error)) {
defer close(out)
val, err := task()
if err != nil {
out <- err
} else {
out <- val
}
}(task)
return out
}()
}
// gather data from all channels
out := make(map[string]interface{})
err := make(map[string]error)
for key, result := range workers {
var _err error
_val := <-result
if _err = isError(_val); _err != nil {
err[key] = _err
_val = nil
}
out[key] = _val
}
// set error as nil if nothing caught
err = hasError(err)
return out, err
}
// check val type, if error then return cast to error
func isError(val interface{}) (err error) {
switch val.(type) {
case error:
err = val.(error)
}
return
}
// check map and look for an error, otherwise set as nil
func hasError(err map[string]error) map[string]error {
flag := true
for _, e := range err {
if e != nil {
flag = false
break
}
}
if flag {
err = nil
}
return err
}