-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
317 lines (256 loc) · 5.3 KB
/
client.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
package e2e
import (
"fmt"
"os"
"strconv"
"time"
"github.com/RackSec/srslog"
"github.com/RoaringBitmap/roaring"
"github.com/g41797/sputnik"
"github.com/g41797/syslogsidecar"
)
const (
syslogClientName = "syslogclient"
syslogClientResponsibility = "syslogclient"
rfc3164 = "RFC3164"
rfc5424 = "RFC5424"
rfcFormatKey = "rfc"
)
func syslogClientDescriptor() sputnik.BlockDescriptor {
return sputnik.BlockDescriptor{Name: syslogClientName, Responsibility: syslogClientResponsibility}
}
func init() {
sputnik.RegisterBlockFactory(syslogClientName, syslogClientBlockFactory)
}
func syslogClientBlockFactory() *sputnik.Block {
client := new(client)
block := sputnik.NewBlock(
sputnik.WithInit(client.init),
sputnik.WithRun(client.run),
sputnik.WithFinish(client.finish),
sputnik.WithOnMsg(client.processBrokerMsg),
)
return block
}
const max_LOG_MESSAGES = 1000000
type client struct {
conf syslogsidecar.SyslogConfiguration
loggers []*srslog.Writer
flip bool
bc sputnik.BlockCommunicator
started bool
startTime time.Time
currIndx int
processed *roaring.Bitmap
successN int
recvN int
startFlow chan struct{}
stopFlow chan struct{}
msgheaders chan map[string]string
nextSend chan struct{}
stop chan struct{}
done chan struct{}
}
// Init
func (cl *client) init(fact sputnik.ConfFactory) error {
if err := fact(syslogsidecar.ReceiverName, &cl.conf); err != nil {
return err
}
cl.loggers = make([]*srslog.Writer, 2)
cl.stop = make(chan struct{}, 1)
cl.done = make(chan struct{}, 1)
cl.startFlow = make(chan struct{}, 1)
cl.stopFlow = make(chan struct{}, 1)
cl.msgheaders = make(chan map[string]string, 1)
cl.nextSend = make(chan struct{}, 1)
return nil
}
// Finish:
func (cl *client) finish(init bool) {
if init {
return
}
close(cl.stop) // Cancel Run
<-cl.done // Wait finish of Run
return
}
// Run
func (cl *client) run(bc sputnik.BlockCommunicator) {
cl.bc = bc
defer close(cl.done)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
loop:
for {
select {
case <-cl.stop:
return
case <-ticker.C:
if err := cl.openLoggers(); err == nil {
break loop
}
}
}
cl.runLoop()
cl.closeLoggers()
cl.report()
return
}
func (cl *client) runLoop() {
ticker := time.NewTicker(20 * time.Second)
defer ticker.Stop()
for {
select {
case headers := <-cl.msgheaders:
cl.update(headers)
case <-cl.nextSend:
cl.sendNext()
case <-cl.stop:
return
case <-cl.startFlow:
cl.startflow()
case <-cl.stopFlow:
cl.stopflow()
case <-ticker.C:
cl.report()
}
}
}
// OnMsg:
func (cl *client) processBrokerMsg(brokermsg sputnik.Msg) {
if brokermsg == nil {
return
}
name, exists := brokermsg["name"]
if !exists {
return
}
switch name {
case "start":
cl.startFlow <- struct{}{}
case "stop":
cl.stopFlow <- struct{}{}
case "consumed":
headers, ok := brokermsg["consumed"].(map[string]string)
if ok && headers != nil {
cl.msgheaders <- headers
}
}
return
}
func (cl *client) startflow() {
if cl.started {
return
}
cl.startTime = time.Now()
cl.started = true
cl.processed = roaring.New()
cl.sendNext()
}
func (cl *client) sendNext() {
if !cl.started {
return
}
if cl.currIndx >= max_LOG_MESSAGES {
return
}
var err error
if cl.flip {
err = cl.loggers[cl.currIndx%2].Warning(strconv.Itoa(cl.currIndx))
} else {
err = cl.loggers[cl.currIndx%2].Emerg(strconv.Itoa(cl.currIndx))
}
cl.flip = !cl.flip
if err != nil {
cl.stopflow()
return
}
cl.currIndx++
cl.nextSend <- struct{}{}
return
}
func (cl *client) stopflow() {
cl.started = false
sendQuit()
}
func (cl *client) update(hdrs map[string]string) {
if hdrs == nil {
return
}
cl.recvN++
rfc, ok := hdrs[rfcFormatKey]
if !ok {
return
}
valName := "message"
if rfc == rfc3164 {
valName = "content"
}
msgText, ok := hdrs[valName]
if !ok {
return
}
msgIndex, err := strconv.Atoi(msgText)
if err != nil {
return
}
if msgIndex >= max_LOG_MESSAGES {
return
}
if msgIndex > cl.currIndx {
return
}
if wasAdded := cl.processed.CheckedAdd(uint32(msgIndex)); !wasAdded {
return
}
cl.successN++
if cl.successN >= max_LOG_MESSAGES {
cl.stopflow()
}
return
}
func (cl *client) report() {
if cl.currIndx > 0 {
elp := time.Since(cl.startTime).String()
fmt.Printf("\n\n\t\t%s Was send %d messages. Successfully consumed %d Received %d\n\n", elp, cl.currIndx, cl.successN, cl.recvN)
}
return
}
func (cl *client) openLoggers() error {
lgr, err := newLogWriter(cl.conf, srslog.RFC3164Formatter, srslog.LOG_LOCAL0+srslog.LOG_EMERG)
if err != nil {
return err
}
cl.loggers[0] = lgr
lgr, err = newLogWriter(cl.conf, srslog.RFC5424Formatter, srslog.LOG_WARNING)
if err != nil {
cl.loggers[0].Close()
cl.loggers[0] = nil
return err
}
cl.loggers[1] = lgr
return nil
}
func (cl *client) closeLoggers() {
for _, lgr := range cl.loggers {
if lgr != nil {
lgr.Close()
}
}
}
func newLogWriter(cnf syslogsidecar.SyslogConfiguration, rfcForm srslog.Formatter, pri srslog.Priority) (*srslog.Writer, error) {
w, err := srslog.Dial("tcp", cnf.ADDRTCP, pri, "re2e")
if err != nil {
return nil, err
}
w.SetFormatter(rfcForm)
return w, nil
}
func sendQuit() error {
cproc, err := os.FindProcess(os.Getpid())
if err != nil {
return err
}
err = cproc.Signal(os.Interrupt)
return err
}