-
Notifications
You must be signed in to change notification settings - Fork 5
/
rxtx.go
163 lines (135 loc) · 3.71 KB
/
rxtx.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
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"time"
"github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/txn2/rxtx/rtq"
"go.uber.org/zap"
)
func main() {
var port = flag.String("port", "8080", "Server port.")
var path = flag.String("path", "./", "Directory to store database.")
var interval = flag.Int("interval", 60, "Seconds between intervals.")
var batch = flag.Int("batch", 100, "Batch size.")
var maxq = flag.Int("maxq", 100000, "Max number of message in queue.")
var ingest = flag.String("ingest", "http://localhost:8081/in", "Ingest server.")
var verbose = flag.Bool("verbose", false, "Verbose")
// Instrumentation
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
err = pprof.StartCPUProfile(f)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
// Instrumentation and Signal handling
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
if sig.String() == "interrupt" {
fmt.Printf("Exiting on interrupt...")
if *cpuprofile != "" {
pprof.StopCPUProfile()
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
fmt.Println("could not create memory profile: " + err.Error())
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
fmt.Println("could not write memory profile: " + err.Error())
}
err = f.Close()
if err != nil {
fmt.Printf("Can not close memory profile: %s\n", err.Error())
return
}
}
os.Exit(0)
}
}
}()
zapCfg := zap.NewProductionConfig()
zapCfg.DisableCaller = true
zapCfg.DisableStacktrace = true
logger, err := zapCfg.Build()
if err != nil {
fmt.Printf("Can not build logger: %s\n", err.Error())
return
}
err = logger.Sync()
if err != nil {
fmt.Println("WARNING: Logger sync error: " + err.Error())
}
logger.Info("Starting rxtx...")
// database
q, err := rtq.NewQ("rxtx", rtq.Config{
Interval: time.Duration(*interval) * time.Second,
Batch: *batch,
MaxInQueue: *maxq,
Logger: logger,
Receiver: *ingest,
Path: *path,
})
if err != nil {
panic(err)
}
// gin config
gin.SetMode(gin.ReleaseMode)
gin.DisableConsoleColor()
// discard default logger
gin.DefaultWriter = io.Discard
// gin router
r := gin.New()
// add queue to the context
r.Use(func(c *gin.Context) {
c.Set("Q", q)
c.Next()
})
// use zap logger on http
if *verbose == true {
r.Use(ginzap.Ginzap(logger, time.RFC3339, true))
}
rxRoute := "/rx/:producer/:key/*label"
r.POST(rxRoute, q.RxRouteHandlerAsync)
r.OPTIONS(rxRoute, preflight)
rxRouteSync := "/rxs/:producer/:key/*label"
r.POST(rxRouteSync, q.RxRouteHandler)
r.OPTIONS(rxRouteSync, preflight)
rxRouteAsync := "/rxa/:producer/:key/*label"
r.POST(rxRouteAsync, q.RxRouteHandlerAsync)
r.OPTIONS(rxRouteAsync, preflight)
// Prometheus Metrics
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
logger.Info("Listening on port: " + *port)
// block on server run
err = r.Run(":" + *port)
if err != nil {
logger.Fatal("unable to start server", zap.Error(err))
}
}
// preflight permissive CORS headers
func preflight(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers", "access-control-allow-origin, access-control-allow-headers, content-type")
c.JSON(http.StatusOK, struct{}{})
}