-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
166 lines (151 loc) · 3.43 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"errors"
"fmt"
"log"
"math/rand"
"runtime"
"strconv"
"sync"
"time"
)
const factor = 1
type Product struct {
ID string
Name string
}
func main() {
channels := runtime.NumCPU() * factor
products := make([]*Product, 0)
ids := make([]string, 0)
for i := 0; i < 10; i++ {
product := Product{
ID: strconv.Itoa(i + 1),
Name: fmt.Sprintf("Product: %s", strconv.Itoa(i+1)),
}
products = append(products, &product)
ids = append(ids, product.ID)
}
productsFoundP := FindProductsByIds(products, ids, channels)
productsFoundS := FindProductsByIdsSeq(products, ids)
fmt.Printf("Parallel: %#v\n", productsFoundP)
fmt.Printf("Sequential: %#v\n", productsFoundS)
}
func FindProductsByIdsSeq(products []*Product, ids []string) []*Product {
now := time.Now()
productsFound := make([]*Product, 0)
for _, id := range ids {
productFound, err := FindProductById(id, products)
if err == nil {
productsFound = append(productsFound, productFound)
}
}
fmt.Printf("sequential version took: %v\n", time.Since(now))
return productsFound
}
func FindProductsByIds(
products []*Product,
ids []string,
channels int,
) []*Product {
now := time.Now()
productsFound := make([]*Product, 0)
done := make(chan interface{})
defer close(done)
idsStream := getStringsStream(ids)
finders := fanOutProducts(done, products, idsStream, channels)
for value := range fanInProducts(done, finders...) {
productsFound = append(productsFound, value)
}
fmt.Printf("parallel version took: %v\n", time.Since(now))
return productsFound
}
func FindProductById(id string, products []*Product) (*Product, error) {
for _, product := range products {
sleepRandomTime()
if product.ID == id {
return product, nil
}
}
return nil, errors.New("Product not found.")
}
func getStringsStream(ids []string) <-chan string {
idsStream := make(chan string, len(ids))
go func(idsStream chan string) {
defer close(idsStream)
for i := 0; i < len(ids); i++ {
idsStream <- ids[i]
}
}(idsStream)
return idsStream
}
func findProductsByIds(
products []*Product,
done <-chan interface{},
idsStream <-chan string,
) <-chan *Product {
productsStream := make(chan *Product)
go func() {
defer close(productsStream)
for id := range idsStream {
product, err := FindProductById(id, products)
if err != nil {
log.Fatal(err)
}
select {
case <-done:
return
case productsStream <- product:
}
}
}()
return productsStream
}
func fanOutProducts(
done <-chan interface{},
products []*Product,
idsStream <-chan string,
channels int,
) []<-chan *Product {
finders := make([]<-chan *Product, channels)
for i := 0; i < channels; i++ {
finders[i] = findProductsByIds(products, done, idsStream)
}
return finders
}
func fanInProducts(
done <-chan interface{},
channels ...<-chan *Product,
) <-chan *Product {
var wg sync.WaitGroup
multiplexedStream := make(chan *Product)
multiplex := func(c <-chan *Product) {
defer wg.Done()
for i := range c {
select {
case <-done:
return
case multiplexedStream <- i:
}
}
}
wg.Add(len(channels))
for _, c := range channels {
go multiplex(c)
}
go func() {
wg.Wait()
close(multiplexedStream)
}()
return multiplexedStream
}
func computeBinaryInt() int {
t := time.Now().UnixNano()
rand.Seed(t)
target := rand.Intn(2) % 2
return target
}
func sleepRandomTime() {
duration := time.Duration(computeBinaryInt() + 1)
time.Sleep(duration * time.Nanosecond)
}