forked from lovoo/goka
/
main.go
202 lines (176 loc) · 4.42 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/lovoo/goka"
"github.com/lovoo/goka/codec"
"github.com/lovoo/goka/web/index"
"github.com/lovoo/goka/web/monitor"
"github.com/lovoo/goka/web/query"
)
var (
brokers = []string{"localhost:9092"}
topic goka.Stream = "user-click"
group goka.Group = "mini-group"
)
// A user is the object that is stored in the processor's group table
type user struct {
// number of clicks the user has performed.
Clicks int
}
// This codec allows marshalling (encode) and unmarshalling (decode) the user to and from the
// group table.
type userCodec struct{}
// Encodes a user into []byte
func (jc *userCodec) Encode(value interface{}) ([]byte, error) {
if _, isUser := value.(*user); !isUser {
return nil, fmt.Errorf("Codec requires value *user, got %T", value)
}
return json.Marshal(value)
}
// Decodes a user from []byte to it's go representation.
func (jc *userCodec) Decode(data []byte) (interface{}, error) {
var (
c user
err error
)
err = json.Unmarshal(data, &c)
if err != nil {
return nil, fmt.Errorf("Error unmarshaling user: %v", err)
}
return &c, nil
}
func runEmitter() {
emitter, err := goka.NewEmitter(brokers, topic,
new(codec.String))
if err != nil {
panic(err)
}
defer emitter.Finish()
t := time.NewTicker(100 * time.Millisecond)
defer t.Stop()
var i int
for range t.C {
key := fmt.Sprintf("user-%d", i%50)
value := fmt.Sprintf("%s", time.Now())
emitter.EmitSync(key, value)
i++
}
}
func process(ctx goka.Context, msg interface{}) {
var u *user
if val := ctx.Value(); val != nil {
u = val.(*user)
} else {
u = new(user)
}
u.Clicks++
ctx.SetValue(u)
fmt.Printf("[proc] key: %s clicks: %d, msg: %v\n", ctx.Key(), u.Clicks, msg)
}
func runStatelessProcessor(monitor *monitor.Server) {
g := goka.DefineGroup(group+"-stateless",
goka.Input(topic,
new(codec.String),
func(ctx goka.Context, msg interface{}) {
//ignored
}),
)
p, err := goka.NewProcessor(brokers, g)
if err != nil {
panic(err)
}
// attach the processor to the monitor
monitor.AttachProcessor(p)
err = p.Run(context.Background())
if err != nil {
panic(err)
} else {
fmt.Println("Processor stopped without errors")
}
}
func runJoinProcessor(monitor *monitor.Server) {
g := goka.DefineGroup(group+"-join",
goka.Input(topic,
new(codec.String),
func(ctx goka.Context, msg interface{}) {
var u *user
if val := ctx.Value(); val != nil {
u = val.(*user)
} else {
u = new(user)
}
u.Clicks++
ctx.SetValue(u)
}),
goka.Persist(new(userCodec)),
)
p, err := goka.NewProcessor(brokers, g)
if err != nil {
panic(err)
}
// attach the processor to the monitor
monitor.AttachProcessor(p)
err = p.Run(context.Background())
if err != nil {
panic(err)
} else {
fmt.Println("Processor stopped without errors")
}
}
func runProcessor(monitor *monitor.Server, query *query.Server) {
g := goka.DefineGroup(group,
goka.Input(topic, new(codec.String), process),
goka.Join(goka.GroupTable(goka.Group(string(group)+"-join")), new(codec.String)),
goka.Persist(new(userCodec)),
)
p, err := goka.NewProcessor(brokers, g)
if err != nil {
panic(err)
}
// attach the processor to the monitor
monitor.AttachProcessor(p)
query.AttachSource("user-clicks", p.Get)
err = p.Run(context.Background())
if err != nil {
panic(err)
} else {
fmt.Println("Processor stopped without errors")
}
}
func runView(root *mux.Router, monitor *monitor.Server) {
view, err := goka.NewView(brokers,
goka.GroupTable(group),
new(userCodec),
)
if err != nil {
panic(err)
}
// attach the processor to the monitor
monitor.AttachView(view)
go view.Run(context.Background())
root.HandleFunc("/{key}", func(w http.ResponseWriter, r *http.Request) {
value, _ := view.Get(mux.Vars(r)["key"])
data, _ := json.Marshal(value)
w.Write(data)
})
fmt.Println("View opened at http://localhost:9095/")
http.ListenAndServe(":9095", root)
}
func main() {
root := mux.NewRouter()
monitorServer := monitor.NewServer("/monitor", root)
queryServer := query.NewServer("/query", root)
idxServer := index.NewServer("/", root)
idxServer.AddComponent(monitorServer, "Monitor")
idxServer.AddComponent(queryServer, "Query")
go runEmitter()
go runProcessor(monitorServer, queryServer)
go runStatelessProcessor(monitorServer)
go runJoinProcessor(monitorServer)
runView(root, monitorServer)
}