-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhub.go
350 lines (313 loc) · 8.31 KB
/
hub.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package webtail
import (
"encoding/json"
"sync"
"time"
"github.com/go-logr/logr"
)
// Returned Messages
const (
MsgSubscribed = "success"
MsgUnSubscribed = "success"
MsgUnknownChannel = "unknown channel"
MsgNotSubscribed = "not subscribed"
MsgWorkerError = "worker create error"
MsgSubscribedAlready = "attached already"
MsgNone = ""
)
// InMessage holds incoming client request
type InMessage struct {
Type string `json:"type"`
Channel string `json:"channel,omitempty"`
}
// TailMessage holds outgoing file tail row
type TailMessage struct {
Type string `json:"type"`
Channel string `json:"channel,omitempty"`
Data string `json:"data,omitempty"`
}
// TraceMessage holds outgoing trace state
type TraceMessage struct {
Type string `json:"type"`
Enabled bool `json:"enabled"`
}
// StatsMessage holds outgoing app stats
type StatsMessage struct {
Type string `json:"type"`
Data map[string]uint64 `json:"data,omitempty"`
}
// IndexItemEvent holds messages from indexer
type IndexItemEvent struct {
ModTime time.Time `json:"mtime"`
Size int64 `json:"size"`
Name string `json:"name"`
Deleted bool `json:"deleted,omitempty"`
}
// IndexMessage holds outgoing message item for file index
type IndexMessage struct {
Type string `json:"type"`
Data IndexItemEvent `json:"data"`
Error string `json:"error,omitempty"`
}
// Message holds received message and sender
type Message struct {
Client *Client
Message []byte
}
// subscribers holds clients subscribed on channel
type subscribers map[*Client]bool
// codebeat:disable[TOO_MANY_IVARS]
// Hub maintains the set of active clients and broadcasts messages to them
type Hub struct {
// Logger
log logr.Logger
// Tail Service workers
workers *TailService
// wg used by Close for wh.WorkerStop ending
wg *sync.WaitGroup
// Registered clients.
clients map[*Client]bool
// Channel subscribers
subscribers map[string]subscribers
// Channel subscriber counts
stats map[string]uint64
// Inbound messages from the clients.
broadcast chan *Message
// Register requests from the clients.
register chan *Client
// Unregister requests from clients.
unregister chan *Client
// Inbound messages from the tailers.
receive chan *TailMessage
// Inbound messages from the channel indexer.
index chan *IndexItemEvent
// Quit channel
quit chan struct{}
}
// codebeat:enable[TOO_MANY_IVARS]
// NewHub creates hub for client services
func NewHub(logger logr.Logger, ts *TailService, wg *sync.WaitGroup) *Hub {
return &Hub{
log: logger,
workers: ts,
wg: wg,
clients: make(map[*Client]bool),
subscribers: make(map[string]subscribers),
stats: make(map[string]uint64),
broadcast: make(chan *Message),
register: make(chan *Client),
unregister: make(chan *Client),
receive: make(chan *TailMessage),
index: make(chan *IndexItemEvent),
quit: make(chan struct{}),
}
}
// Run processes hub messages
func (h *Hub) Run() {
h.subscribers[""] = make(subscribers)
h.workers.IndexerRun(h.index, h.wg)
defer h.workers.WorkerStop("")
onAir := true
for {
select {
case client := <-h.register:
if onAir {
h.clients[client] = true
}
case client := <-h.unregister:
if _, ok := h.clients[client]; ok {
h.unsubscribeClient(client, onAir)
}
if !onAir && len(h.clients) == 0 {
return
}
case cmessage := <-h.broadcast:
// client sends attach/detach/?list
h.fromClient(cmessage)
case wmessage := <-h.receive:
// tailer sends file line
h.fromTailer(wmessage)
case imessage := <-h.index:
// worker sends index update
h.fromIndexer(imessage)
case <-h.quit:
onAir = false
if len(h.clients) == 0 {
return
}
for client := range h.clients {
close(client.send)
}
}
}
}
// Close closes message processing
func (h *Hub) Close() {
h.quit <- struct{}{}
}
func (h *Hub) fromClient(msg *Message) {
var data []byte
in := InMessage{}
err := json.Unmarshal(msg.Message, &in)
if err != nil {
data, _ = json.Marshal(TailMessage{Type: "error", Data: "parse error"})
h.send(msg.Client, data)
return
}
h.log.Info("Received from Client", "message", in)
switch in.Type {
case "attach":
msgData, ok := h.subscribe(in.Channel, msg.Client)
data = formatTailMessage(in.Channel, "attach", msgData, ok)
case "detach":
msgData, ok := h.unsubscribe(in.Channel, msg.Client)
data = formatTailMessage(in.Channel, "detach", msgData, ok)
case "stats":
// send index counters
data, _ = json.Marshal(StatsMessage{Type: "stats", Data: h.stats})
case "trace":
// on/off tracing
h.workers.SetTrace(in.Channel)
data, _ = json.Marshal(TraceMessage{Type: "trace", Enabled: h.workers.TraceEnabled()})
}
if len(data) > 0 {
h.send(msg.Client, data)
}
}
// fromTailer processes message from worker
func (h *Hub) fromTailer(msg *TailMessage) {
if h.workers.TraceEnabled() {
h.log.Info("Trace from tailer", "channel", msg.Channel, "data", msg.Data, "type", msg.Type)
}
data, _ := json.Marshal(msg)
if msg.Type == "log" && !h.workers.TailerAppend(msg.Channel, data) {
h.log.Info("Incomplete line skipped")
return
}
clients := h.subscribers[msg.Channel]
for client := range clients {
h.send(client, data)
}
}
// process message from indexer
func (h *Hub) fromIndexer(msg *IndexItemEvent) {
if h.workers.TraceEnabled() {
h.log.Info("Trace from indexer", "message", msg)
}
data, _ := json.Marshal(IndexMessage{Type: "index", Data: *msg})
h.workers.IndexUpdate(msg)
clients := h.subscribers[""]
for client := range clients {
h.send(client, data)
}
}
func (h *Hub) subscribe(channel string, client *Client) (string, bool) {
var err error
if !h.workers.ChannelExists(channel) {
return MsgUnknownChannel, false
}
if !h.workers.WorkerExists(channel) {
readyChan := make(chan struct{})
// no producer => create
err = h.workers.TailerRun(channel, h.receive, readyChan, h.wg)
if err != nil {
h.log.Error(err, "Worker create error")
return MsgWorkerError, false
}
h.subscribers[channel] = make(subscribers)
<-readyChan
} else if _, ok := h.subscribers[channel][client]; ok {
return MsgSubscribedAlready, false
}
// Confirm attach
// not via data because have to be first in response
if h.send(client, formatTailMessage(channel, "attach", MsgSubscribed, true)) {
if h.sendReply(channel, client) {
// subscribe client
h.subscribers[channel][client] = true
h.stats[channel]++
}
}
return MsgNone, true
}
func (h *Hub) sendReply(ch string, cl *Client) bool {
if ch != "" {
// send actual buffer
for _, item := range h.workers.TailerBuffer(ch) {
if !h.send(cl, item) {
return false
}
}
return true
}
// send channel index
for _, v := range h.workers.IndexKeys() {
file := h.workers.IndexItem(v)
idx := &IndexMessage{
Type: "index",
Data: IndexItemEvent{
Name: v,
ModTime: file.ModTime,
Size: file.Size,
},
}
data, _ := json.Marshal(idx)
if !h.send(cl, data) {
return false
}
}
return true
}
func (h *Hub) send(client *Client, data []byte) bool {
h.log.Info("Send reply", "message", string(data))
select {
case client.send <- data:
default:
h.unsubscribeClient(client, true)
return false
}
return true
}
// unsubscribeClient removes all client subscriptions
func (h *Hub) unsubscribeClient(client *Client, needsClose bool) {
for k := range h.subscribers {
if _, ok := h.subscribers[k][client]; ok {
h.log.Info("Remove subscriber from channel", "channel", k)
h.unsubscribe(k, client)
}
}
if needsClose {
close(client.send)
}
delete(h.clients, client)
}
func (h *Hub) unsubscribe(channel string, client *Client) (string, bool) {
subscribers, ok := h.subscribers[channel]
if !ok {
return MsgUnknownChannel, false
}
if _, ok = subscribers[client]; !ok {
return MsgNotSubscribed, false
}
delete(h.subscribers[channel], client)
h.stats[channel]--
if channel != "" && h.stats[channel] == 0 {
// tailer has no subscribers => stop it
h.workers.WorkerStop(channel)
}
return MsgUnSubscribed, true
}
// formatTailMessage packs data to json
func formatTailMessage(channel, cmd, msgData string, ok bool) []byte {
if msgData == MsgNone {
return []byte{}
}
msg := TailMessage{Data: msgData, Channel: channel}
if ok {
msg.Type = cmd
} else {
msg.Type = "error"
}
data, _ := json.Marshal(msg)
return data
}