-
Notifications
You must be signed in to change notification settings - Fork 8
/
handler.go
331 lines (310 loc) · 8.78 KB
/
handler.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
log "github.com/sirupsen/logrus"
"github.com/tonkeeper/bridge/config"
"github.com/tonkeeper/bridge/datatype"
)
var (
activeConnectionMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "number_of_acitve_connections",
Help: "The number of active connections",
})
activeSubscriptionsMetric = promauto.NewGauge(prometheus.GaugeOpts{
Name: "number_of_active_subscriptions",
Help: "The number of active subscriptions",
})
transferedMessagesNumMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "number_of_transfered_messages",
Help: "The total number of transfered_messages",
})
deliveredMessagesMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "number_of_delivered_messages",
Help: "The total number of delivered_messages",
})
badRequestMetric = promauto.NewCounter(prometheus.CounterOpts{
Name: "number_of_bad_requests",
Help: "The total number of bad requests",
})
clientIdsPerConnectionMetric = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "number_of_client_ids_per_connection",
Buckets: []float64{1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 100},
})
)
type stream struct {
Sessions []*Session
mux sync.RWMutex
}
type handler struct {
Mux sync.RWMutex
Connections map[string]*stream
storage db
_eventIDs int64
heartbeatInterval time.Duration
}
type db interface {
GetMessages(ctx context.Context, keys []string, lastEventId int64) ([]datatype.SseMessage, error)
Add(ctx context.Context, key string, ttl int64, mes datatype.SseMessage) error
}
func newHandler(db db, heartbeatInterval time.Duration) *handler {
h := handler{
Mux: sync.RWMutex{},
Connections: make(map[string]*stream),
storage: db,
_eventIDs: time.Now().UnixMicro(),
heartbeatInterval: heartbeatInterval,
}
return &h
}
func (h *handler) EventRegistrationHandler(c echo.Context) error {
log := log.WithField("prefix", "EventRegistrationHandler")
_, ok := c.Response().Writer.(http.Flusher)
if !ok {
http.Error(c.Response().Writer, "streaming unsupported", http.StatusInternalServerError)
return c.JSON(HttpResError("streaming unsupported", http.StatusBadRequest))
}
c.Response().Header().Set("Content-Type", "text/event-stream")
c.Response().Header().Set("Cache-Control", "no-cache")
c.Response().Header().Set("Connection", "keep-alive")
c.Response().Header().Set("Transfer-Encoding", "chunked")
c.Response().WriteHeader(http.StatusOK)
fmt.Fprint(c.Response(), "\n")
c.Response().Flush()
params := c.QueryParams()
var lastEventId int64
var err error
lastEventIDStr := c.Request().Header.Get("Last-Event-ID")
if lastEventIDStr != "" {
lastEventId, err = strconv.ParseInt(lastEventIDStr, 10, 64)
if err != nil {
badRequestMetric.Inc()
errorMsg := "Last-Event-ID should be int"
log.Error(errorMsg)
return c.JSON(HttpResError(errorMsg, http.StatusBadRequest))
}
}
lastEventIdQuery, ok := params["last_event_id"]
if ok && lastEventId == 0 {
lastEventId, err = strconv.ParseInt(lastEventIdQuery[0], 10, 64)
if err != nil {
badRequestMetric.Inc()
errorMsg := "last_event_id should be int"
log.Error(errorMsg)
return c.JSON(HttpResError(errorMsg, http.StatusBadRequest))
}
}
clientId, ok := params["client_id"]
if !ok {
badRequestMetric.Inc()
errorMsg := "param \"client_id\" not present"
log.Error(errorMsg)
return c.JSON(HttpResError(errorMsg, http.StatusBadRequest))
}
clientIds := strings.Split(clientId[0], ",")
clientIdsPerConnectionMetric.Observe(float64(len(clientIds)))
session := h.CreateSession(clientId[0], clientIds, lastEventId)
ctx := c.Request().Context()
notify := ctx.Done()
go func() {
<-notify
close(session.Closer)
h.removeConnection(session)
log.Infof("connection: %v closed with error %v", session.ClientIds, ctx.Err())
}()
ticker := time.NewTicker(h.heartbeatInterval)
defer ticker.Stop()
session.Start()
loop:
for {
select {
case msg, ok := <-session.MessageCh:
if !ok {
log.Errorf("can't read from channel")
break loop
}
_, err = fmt.Fprintf(c.Response(), "event: %v\nid: %v\ndata: %v\n\n", "message", msg.EventId, string(msg.Message))
if err != nil {
log.Errorf("msg can't write to connection: %v", err)
break loop
}
c.Response().Flush()
deliveredMessagesMetric.Inc()
case <-ticker.C:
_, err = fmt.Fprintf(c.Response(), "event: heartbeat\n\n")
if err != nil {
log.Errorf("ticker can't write to connection: %v", err)
break loop
}
c.Response().Flush()
}
}
activeConnectionMetric.Dec()
log.Info("connection closed")
return nil
}
func (h *handler) SendMessageHandler(c echo.Context) error {
ctx := c.Request().Context()
log := log.WithContext(ctx).WithField("prefix", "SendMessageHandler")
params := c.QueryParams()
clientId, ok := params["client_id"]
if !ok {
badRequestMetric.Inc()
errorMsg := "param \"client_id\" not present"
log.Error(errorMsg)
return c.JSON(HttpResError(errorMsg, http.StatusBadRequest))
}
toId, ok := params["to"]
if !ok {
badRequestMetric.Inc()
errorMsg := "param \"to\" not present"
log.Error(errorMsg)
return c.JSON(HttpResError(errorMsg, http.StatusBadRequest))
}
ttlParam, ok := params["ttl"]
if !ok {
badRequestMetric.Inc()
errorMsg := "param \"ttl\" not present"
log.Error(errorMsg)
return c.JSON(HttpResError(errorMsg, http.StatusBadRequest))
}
ttl, err := strconv.ParseInt(ttlParam[0], 10, 32)
if err != nil {
badRequestMetric.Inc()
log.Error(err)
return c.JSON(HttpResError(err.Error(), http.StatusBadRequest))
}
if ttl > 300 { // TODO: config
badRequestMetric.Inc()
errorMsg := "param \"ttl\" too high"
log.Error(errorMsg)
return c.JSON(HttpResError(errorMsg, http.StatusBadRequest))
}
message, err := io.ReadAll(c.Request().Body)
if err != nil {
badRequestMetric.Inc()
log.Error(err)
return c.JSON(HttpResError(err.Error(), http.StatusBadRequest))
}
mes, err := json.Marshal(datatype.BridgeMessage{
From: clientId[0],
Message: string(message),
})
if err != nil {
badRequestMetric.Inc()
log.Error(err)
return c.JSON(HttpResError(err.Error(), http.StatusBadRequest))
}
if config.Config.CopyToURL != "" {
go func() {
u, err := url.Parse(config.Config.CopyToURL)
if err != nil {
return
}
u.RawQuery = params.Encode()
req, err := http.NewRequest(http.MethodPost, u.String(), bytes.NewReader(message))
if err != nil {
return
}
http.DefaultClient.Do(req)
}()
}
topic, ok := params["topic"]
if ok {
go func(clientID, topic, message string) {
SendWebhook(clientID, WebhookData{Topic: topic, Hash: message})
}(clientId[0], topic[0], string(message))
}
sseMessage := datatype.SseMessage{
EventId: h.nextID(),
Message: mes,
}
h.Mux.RLock()
s, ok := h.Connections[toId[0]]
h.Mux.RUnlock()
if ok {
s.mux.Lock()
for _, ses := range s.Sessions {
ses.AddMessageToQueue(ctx, sseMessage)
}
s.mux.Unlock()
}
go func() {
log := log.WithField("prefix", "SendMessageHandler.storge.Add")
err = h.storage.Add(context.Background(), toId[0], ttl, sseMessage)
if err != nil {
log.Errorf("db error: %v", err)
}
}()
transferedMessagesNumMetric.Inc()
return c.JSON(http.StatusOK, HttpResOk())
}
func (h *handler) removeConnection(ses *Session) {
log := log.WithField("prefix", "removeConnection")
log.Infof("remove session: %v", ses.ClientIds)
for _, id := range ses.ClientIds {
h.Mux.RLock()
s, ok := h.Connections[id]
h.Mux.RUnlock()
if !ok {
log.Info("alredy removed")
continue
}
s.mux.Lock()
for i := range s.Sessions {
if s.Sessions[i] == ses {
s.Sessions[i] = s.Sessions[len(s.Sessions)-1]
s.Sessions = s.Sessions[:len(s.Sessions)-1]
break
}
}
s.mux.Unlock()
if len(s.Sessions) == 0 {
h.Mux.Lock()
delete(h.Connections, id)
h.Mux.Unlock()
}
activeSubscriptionsMetric.Dec()
}
}
func (h *handler) CreateSession(sessionId string, clientIds []string, lastEventId int64) *Session {
log := log.WithField("prefix", "CreateSession")
log.Infof("make new session with ids: %v", clientIds)
session := NewSession(h.storage, clientIds, lastEventId)
activeConnectionMetric.Inc()
for _, id := range clientIds {
h.Mux.RLock()
s, ok := h.Connections[id]
h.Mux.RUnlock()
if ok {
s.mux.Lock()
s.Sessions = append(s.Sessions, session)
s.mux.Unlock()
} else {
h.Mux.Lock()
h.Connections[id] = &stream{
mux: sync.RWMutex{},
Sessions: []*Session{session},
}
h.Mux.Unlock()
}
activeSubscriptionsMetric.Inc()
}
return session
}
func (h *handler) nextID() int64 {
return atomic.AddInt64(&h._eventIDs, 1)
}