-
Notifications
You must be signed in to change notification settings - Fork 0
/
two.go
143 lines (123 loc) · 2.67 KB
/
two.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
package main
import (
"fmt"
"sync"
"time"
)
func main() {
defer func(t time.Time) { fmt.Printf("total took %v\n", time.Since(t)) }(time.Now())
t := time.Now()
payloads := createPayloads("hello %s, your word of the day is %s")
fmt.Printf("payload creation took %v\n", time.Since(t))
wg := new(sync.WaitGroup)
wg.Add(len(payloads["ios"]) + len(payloads["android"]))
apns := newAppleSender(wg)
gcm := newGoogleSender(wg)
t = time.Now()
sendPayloads(apns, gcm, payloads)
wg.Wait()
fmt.Printf("payload sending took %v\n", time.Since(t))
}
func createPayloads(message string) map[string][]string {
payloads := make(map[string][]string)
iosUsers := getIOSUsers()
androidUsers := getAndroidUsers()
words := getWords()
for i, word := range words {
payloads["ios"] = append(payloads["ios"], fmt.Sprintf(message, iosUsers[i], word))
payloads["android"] = append(payloads["android"], fmt.Sprintf(message, androidUsers[i], word))
}
return payloads
}
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
}
func sendPayloads(apns chan<- string, gcm chan<- string, payloads map[string][]string) {
for platform, payloads := range payloads {
for _, payload := range payloads {
switch platform {
case "ios":
apns <- payload
case "android":
gcm <- payload
}
}
}
}
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()
}
}
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",
}