-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
169 lines (139 loc) · 3.44 KB
/
api.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
/*
Copyright 2021 Adevinta
*/
package stream
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
metrics "github.com/adevinta/vulcan-metrics-client"
"github.com/sirupsen/logrus"
)
const (
// abort check action
actionAbort = "abort"
// metrics
metricNotified = "vulcan.stream.mssgs.notified"
metricBroadcasted = "vulcan.stream.mssgs.broadcasted"
componentTag = "component:stream"
)
// APIConfig represents the config
// necessary for stream API.
type APIConfig struct {
Port int
}
// API represents the stream REST API.
type API struct {
sender *Sender
storage Storage
logger logrus.FieldLogger
metrics metrics.Client
mux *http.ServeMux
port int
}
// AbortRequest represents the body
// for an abort cheks request.
type AbortRequest struct {
Checks []string `json:"checks"`
}
// NewAPI builds a new stream API.
func NewAPI(port int, sender *Sender, storage Storage, logger logrus.FieldLogger,
metrics metrics.Client) *API {
a := &API{
sender: sender,
storage: storage,
logger: logger,
metrics: metrics,
mux: http.NewServeMux(),
port: port,
}
a.mux.HandleFunc("/stream", a.connHandler)
a.mux.HandleFunc("/checks", a.checksHandler)
a.mux.HandleFunc("/abort", a.abortHandler)
a.mux.HandleFunc("/status", a.statusHandler)
return a
}
// Start starts the stream API.
func (a *API) Start() {
a.logger.WithFields(logrus.Fields{
"details": a.port,
}).Info("Vulcan Stream API started")
go a.sender.Start()
a.logger.Panic(http.ListenAndServe(fmt.Sprintf(":%v", a.port), a.mux))
}
// connHandler handles a new connection to the stream.
func (a *API) connHandler(w http.ResponseWriter, r *http.Request) {
a.sender.HandleConn(w, r)
}
// checksHandler returns the list of currently aborted checks.
func (a *API) checksHandler(w http.ResponseWriter, r *http.Request) {
checks, err := a.storage.GetAbortedChecks(context.Background())
if err != nil {
writeErr(w, err)
return
}
checksArray, err := json.Marshal(checks)
if err != nil {
writeErr(w, err)
return
}
w.Write(checksArray)
}
// abortHandler handles an abort checks request.
func (a *API) abortHandler(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
writeErr(w, err)
return
}
var req AbortRequest
err = json.Unmarshal(body, &req)
if err != nil {
writeErr(w, err)
return
}
err = a.storage.AddAbortedChecks(context.Background(), req.Checks)
if err != nil {
writeErr(w, err)
return
}
a.incrNotifiedMssgs(len(req.Checks))
// TODO: should we broadcast
// checks async once they are
// stored so we don't block
// sync HTTP request?
// E.g: have a backlog in sender
// and a goroutine which consumes
// from that.
for _, c := range req.Checks {
m := Message{
CheckID: c,
Action: actionAbort,
}
a.sender.Broadcast(m)
a.incrBroadcastedMssgs(m)
}
}
func (a *API) statusHandler(w http.ResponseWriter, r *http.Request) { /* 200 OK */ }
func writeErr(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("err: %v", err)))
}
func (a *API) incrNotifiedMssgs(count int) {
a.metrics.Push(metrics.Metric{
Name: metricNotified,
Typ: metrics.Count,
Value: float64(count),
Tags: []string{componentTag},
})
}
func (a *API) incrBroadcastedMssgs(m Message) {
a.metrics.Push(metrics.Metric{
Name: metricBroadcasted,
Typ: metrics.Count,
Value: 1,
Tags: []string{componentTag, fmt.Sprint("action:", m.Action)},
})
}