forked from lovoo/goka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (74 loc) · 1.39 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"time"
"github.com/lovoo/goka/examples/3-messaging"
)
const (
urlTmpl = "http://localhost:8080/%s/send"
spamProb = 0.3
)
var (
users = []string{
"Alice",
"Bob",
"Charlie",
"Dave",
"Eve",
}
contents = []string{
"Hi how are you doing",
"Hello let's have lunch together",
"Bye",
}
)
func send(from, to, content string) {
m := messaging.Message{
To: to,
Content: content,
}
b, err := json.Marshal(&m)
if err != nil {
log.Printf("error encoding message: %v", err)
return
}
url := fmt.Sprintf(urlTmpl, from)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(b))
if err != nil {
log.Printf("error creating request: %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("error sending request: %v", err)
return
}
defer resp.Body.Close()
//TODO(diogo) check response status code
}
func main() {
t := time.NewTicker(200 * time.Millisecond)
defer t.Stop()
for range t.C {
var (
cnt = "spam!"
from = "Bob"
)
if rand.Float64() < 1-spamProb {
from = users[rand.Intn(len(users))]
cnt = contents[rand.Intn(len(contents))]
}
to := users[rand.Intn(len(users))]
for to == from {
to = users[rand.Intn(len(users))]
}
send(from, to, cnt)
}
}