-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree.go
153 lines (129 loc) · 2.67 KB
/
three.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
package main
import (
"fmt"
"sync"
"time"
)
func main() {
defer func(t time.Time) { fmt.Printf("total took %v\n", time.Since(t)) }(time.Now())
wg := new(sync.WaitGroup)
apns := newAppleSender(wg)
gcm := newGoogleSender(wg)
pc := newPayloadCreator(apns, gcm, wg)
pc.createAndSend("hello %s, your word of the day is %s")
wg.Wait()
}
func getAndroidUsers() []string {
time.Sleep(500 * time.Millisecond) // database overhead
return androidUsers
}
func getIOSUsers() []string {
time.Sleep(500 * time.Millisecond) // database overhead
return iosUsers
}
func getWords() []string {
time.Sleep(500 * time.Millisecond) // database overhead
return words
}
type appleSender struct {
in chan string
wg *sync.WaitGroup
}
func newAppleSender(wg *sync.WaitGroup) chan<- string {
as := &appleSender{
in: make(chan string, 8),
wg: wg,
}
go as.sendApples()
return as.in
}
func (as *appleSender) sendApples() {
for payload := range as.in {
time.Sleep(100 * time.Millisecond) // network overhead
fmt.Printf("apns | %s\n", payload)
as.wg.Done()
}
}
type googleSender struct {
in chan string
wg *sync.WaitGroup
}
func newGoogleSender(wg *sync.WaitGroup) chan<- string {
gs := &googleSender{
in: make(chan string, 8),
wg: wg,
}
go gs.sendGoogles()
return gs.in
}
func (gs *googleSender) sendGoogles() {
for payload := range gs.in {
time.Sleep(100 * time.Millisecond) // network overhead
fmt.Printf("gcm | %s\n", payload)
gs.wg.Done()
}
}
type payloadCreator struct {
apns chan<- string
gcm chan<- string
wg *sync.WaitGroup
}
func newPayloadCreator(apns chan<- string, gcm chan<- string, wg *sync.WaitGroup) *payloadCreator {
return &payloadCreator{
apns: apns,
gcm: gcm,
wg: wg,
}
}
func (pc *payloadCreator) createAndSendIOS(message string, words []string) {
iosUsers := getIOSUsers()
for i, word := range words {
pc.wg.Add(1)
pc.apns <- fmt.Sprintf(message, iosUsers[i], word)
}
pc.wg.Done()
}
func (pc *payloadCreator) createAndSendAndroid(message string, words []string) {
androidUsers := getAndroidUsers()
for i, word := range words {
pc.wg.Add(1)
pc.gcm <- fmt.Sprintf(message, androidUsers[i], word)
}
pc.wg.Done()
}
func (pc *payloadCreator) createAndSend(message string) {
words := getWords()
pc.wg.Add(2)
go pc.createAndSendAndroid(message, words)
go pc.createAndSendIOS(message, words)
}
var words = []string{
"lorem",
"ipsum",
"dolor",
"sit",
"amet",
"consectetur",
"adipiscing",
"elit",
}
var androidUsers = []string{
"Shawn",
"Cari",
"Alara",
"Poppy-Mae",
"Lennie",
"Jack",
"Amrita",
"Mylo",
}
var iosUsers = []string{
"Brooklyn",
"Fahad",
"Chardonnay",
"Callam",
"Rebekah",
"Byron",
"Rikki",
"Keyan",
}