-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (124 loc) · 3.08 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"math/rand"
"net/http"
"os"
"sync"
"time"
"github.com/alecthomas/kong"
"github.com/marcelbeumer/go-playground/streamproc/services/event-producer/internal/log"
)
type CLI struct {
APIHost string `help:"API host." short:"h" default:"127.0.0.1" env:"API_HOST"`
APIPort int `help:"API port." short:"p" default:"9998" env:"API_PORT"`
PlainText bool `help:"Send events to the API plain text instead of JSON" env:"PLAIN_TEXT"`
Disable bool `help:"Disable all processing." short:"x" env:"DISABLE"`
}
type EventBuffer struct {
events []Event
l sync.RWMutex
}
func (b *EventBuffer) Append(e Event) {
b.l.Lock()
defer b.l.Unlock()
b.events = append(b.events, e)
}
func (b *EventBuffer) Recover(e []Event) {
b.l.Lock()
defer b.l.Unlock()
b.events = append(e, b.events...)
}
func (b *EventBuffer) Flush() []Event {
b.l.Lock()
defer b.l.Unlock()
slice := b.events[:]
b.events = make([]Event, 0)
return slice
}
// Event is send to server for processing
type Event struct {
// Time is timestamp in UnixMilli
Time int64 `json:"time"`
// Amount if number between 0-10
Amount *big.Float `json:"amount"`
}
func main() {
zl := log.NewZapLogger(os.Stderr)
log.RedirectStdLog(zl)
logger := log.NewZapLoggerAdapter(zl)
cli := CLI{}
_ = kong.Parse(
&cli,
kong.Name("event-producer"),
kong.UsageOnError(),
)
buffer := EventBuffer{}
go func() {
for {
time.Sleep(time.Second * 2)
events := buffer.Flush()
if len(events) == 0 {
continue
}
url := fmt.Sprintf("http://%s:%d", cli.APIHost, cli.APIPort)
var contentType string
var reqBody []byte
if cli.PlainText {
contentType = "application/text"
for _, event := range events {
line, err := json.Marshal(&event)
if err != nil {
logger.Error(log.Error(err))
os.Exit(1)
}
if len(reqBody) > 0 {
reqBody = append(reqBody, []byte("\n")...)
}
reqBody = append(reqBody, line...)
}
} else {
contentType = "application/json"
j, err := json.Marshal(&events)
if err != nil {
logger.Error(log.Error(err))
os.Exit(1)
}
reqBody = j
}
if cli.Disable {
logger.Info("processing disabled, not sending events")
continue
}
resp, err := http.Post(url, contentType, bytes.NewBuffer(reqBody))
if err != nil {
logger.Errorw("request error", log.Error(err))
buffer.Recover(events)
continue
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode >= 400 {
logger.Errorw("request http error",
"statusCode", resp.StatusCode,
"responseBody", respBody)
buffer.Recover(events)
continue
}
logger.Infow("sent events",
"amount", len(events))
}
}()
for {
event := Event{
Time: time.Now().UnixMilli(),
Amount: big.NewFloat(rand.Float64() * 10),
}
buffer.Append(event)
time.Sleep(time.Second)
}
}