-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimpl.go
371 lines (342 loc) · 10.8 KB
/
impl.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright (c) 2021-present unTill Pro, Ltd.
*/
package router2
import (
"context"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"runtime/debug"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
ibus "github.com/untillpro/airs-ibus"
ibusnats "github.com/untillpro/airs-ibusnats"
"github.com/untillpro/goutils/logger"
"github.com/valyala/bytebufferpool"
istructs "github.com/voedger/voedger/pkg/istructs"
coreutils "github.com/voedger/voedger/pkg/utils"
)
const (
queueAliasVar = "queue-alias"
wSIDVar = "partition-dividend"
resourceNameVar = "resource-name"
bp3AppOwner = "app-owner"
bp3AppName = "app-name"
bp3BLOBID = "blobID"
bp3PrincipalToken = "principalToken"
DefaultRouterPort = 8822
DefaultRouterConnectionsLimit = 10000
//Timeouts should be greater than NATS timeouts to proper use in browser(multiply responses)
DefaultRouterReadTimeout = 15
DefaultRouterWriteTimeout = 15
)
var (
queueNamesJSON []byte
airsBPPartitionsAmount int = 100 // changes in tests
onRequestCtxClosed func() = nil // used in tests
onAfterSectionWrite func(w http.ResponseWriter) = nil // used in tests
)
func partitionHandler(queueNumberOfPartitions ibusnats.QueuesPartitionsMap, bus ibus.IBus, busTimeout time.Duration, appsWSAmount map[istructs.AppQName]istructs.AppWSAmount) http.HandlerFunc {
return func(resp http.ResponseWriter, req *http.Request) {
if logger.IsVerbose() {
logger.Verbose("serving ", req.Method, " ", req.URL.Path)
}
vars := mux.Vars(req)
queueRequest, ok := createRequest(req.Method, req, resp, appsWSAmount)
if !ok {
return
}
if len(queueNumberOfPartitions) > 0 {
// note: Partition here is not used in BP3
numberOfPartitions := queueNumberOfPartitions[vars[queueAliasVar]]
queueRequest.PartitionNumber = int(queueRequest.WSID % int64(numberOfPartitions))
}
queueRequest.Resource = vars[resourceNameVar]
// req's BaseContext is router service's context. See service.Start()
// router app closing or client disconnected -> req.Context() is done
// will create new cancellable context and cancel it if http section send is failed.
// requestCtx.Done() -> SendRequest2 implementation will notify the handler that the consumer has left us
requestCtx, cancel := context.WithCancel(req.Context())
defer cancel() // to avoid context leak
res, sections, secErr, err := bus.SendRequest2(requestCtx, queueRequest, busTimeout)
if err != nil {
logger.Error("IBus.SendRequest2 failed on ", queueRequest.Resource, ":", err, ". Body:\n", string(queueRequest.Body))
writeTextResponse(resp, err.Error(), http.StatusInternalServerError)
return
}
if sections == nil {
resp.Header().Set(coreutils.ContentType, res.ContentType)
resp.WriteHeader(res.StatusCode)
writeResponse(resp, string(res.Data))
return
}
writeSectionedResponse(requestCtx, resp, sections, secErr, cancel)
}
}
func discardSection(iSection ibus.ISection) {
switch t := iSection.(type) {
case nil:
case ibus.IObjectSection:
t.Value()
case ibus.IMapSection:
for _, _, ok := t.Next(); ok; _, _, ok = t.Next() {
}
case ibus.IArraySection:
for _, ok := t.Next(); ok; _, ok = t.Next() {
}
}
}
func startSectionedResponse(w http.ResponseWriter) bool {
w.Header().Set(coreutils.ContentType, coreutils.ApplicationJSON)
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusOK)
return writeResponse(w, "{")
}
func writeSectionedResponse(requestCtx context.Context, w http.ResponseWriter, sections <-chan ibus.ISection, secErr *error, onSendFailed func()) {
ok := true
var iSection ibus.ISection
defer func() {
if !ok {
onSendFailed()
// consume all pending sections or elems to avoid hanging on ibusnats side
// normally should one pending elem or section because ibusnats implementation
// will terminate on next elem or section because `onSendFailed()` actually closes the context
discardSection(iSection)
for iSection := range sections {
discardSection(iSection)
}
}
}()
sectionsOpened := false
sectionedResponseStarted := false
closer := ""
// ctx done -> sections will be closed by ibusnats implementation
for iSection = range sections {
// possible: ctx is done but on select {sections<-section, <-ctx.Done()} write to sections channel is triggered.
// ctx.Done() must have the priority
if requestCtx.Err() != nil {
ok = false
break
}
if !sectionedResponseStarted {
if ok = startSectionedResponse(w); !ok {
return
}
sectionedResponseStarted = true
}
if !sectionsOpened {
if ok = writeResponse(w, `"sections":[`); !ok {
return
}
closer = "]"
sectionsOpened = true
} else {
if ok = writeResponse(w, ","); !ok {
return
}
}
if ok = writeSection(w, iSection); !ok {
return
}
if onAfterSectionWrite != nil {
// happens in tests
onAfterSectionWrite(w)
}
}
if requestCtx.Err() != nil {
if onRequestCtxClosed != nil {
onRequestCtxClosed()
}
log.Println("client disconnected during sections sending")
return
}
if *secErr != nil {
if !sectionedResponseStarted {
if !startSectionedResponse(w) {
return
}
}
if sectionsOpened {
closer = "],"
}
var jsonableErr interface{ ToJSON() string }
if errors.As(*secErr, &jsonableErr) {
jsonErr := jsonableErr.ToJSON()
jsonErr = strings.TrimPrefix(jsonErr, "{")
jsonErr = strings.TrimSuffix(jsonErr, "}")
writeResponse(w, fmt.Sprintf(`%s%s}`, closer, jsonErr))
} else {
writeResponse(w, fmt.Sprintf(`%s"status":%d,"errorDescription":"%s"}`, closer, http.StatusInternalServerError, *secErr))
}
} else {
if sectionedResponseStarted {
writeResponse(w, fmt.Sprintf(`%s}`, closer))
}
}
}
func queueNamesHandler() http.HandlerFunc {
return func(resp http.ResponseWriter, req *http.Request) {
if _, err := resp.Write(queueNamesJSON); err != nil {
log.Println("failed to write queues names", err)
}
}
}
func checkHandler() http.HandlerFunc {
return func(resp http.ResponseWriter, req *http.Request) {
if _, err := resp.Write([]byte("ok")); err != nil {
log.Println("failed to write 'ok' response:", err)
}
}
}
func createRequest(reqMethod string, req *http.Request, rw http.ResponseWriter, appsWSAmount map[istructs.AppQName]istructs.AppWSAmount) (res ibus.Request, ok bool) {
vars := mux.Vars(req)
wsidStr := vars[wSIDVar]
wsidInt, err := strconv.ParseInt(wsidStr, parseInt64Base, parseInt64Bits)
if err != nil {
// impossible because of regexp in a handler
// notest
panic(err)
}
appQNameStr := vars[bp3AppOwner] + "/" + vars[bp3AppName]
wsid := istructs.WSID(wsidInt)
if appQName, err := istructs.ParseAppQName(appQNameStr); err == nil {
if appWSAmount, ok := appsWSAmount[appQName]; ok {
baseWSID := wsid.BaseWSID()
if baseWSID < istructs.MaxPseudoBaseWSID {
wsid = coreutils.GetAppWSID(wsid, appWSAmount)
}
}
}
res = ibus.Request{
Method: ibus.NameToHTTPMethod[reqMethod],
WSID: int64(wsid),
Query: req.URL.Query(),
Header: req.Header,
QueueID: vars[queueAliasVar],
AppQName: appQNameStr,
Host: req.Host,
}
if req.Body != nil && req.Body != http.NoBody {
if res.Body, err = ioutil.ReadAll(req.Body); err != nil {
http.Error(rw, "failed to read body", http.StatusInternalServerError)
}
}
return res, err == nil
}
func corsHandler(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization")
if r.Method == "OPTIONS" {
return
}
h.ServeHTTP(w, r)
}
}
func writeTextResponse(w http.ResponseWriter, msg string, code int) {
w.Header().Set(coreutils.ContentType, "text/plain")
w.WriteHeader(code)
writeResponse(w, msg)
}
func writeUnauthorized(rw http.ResponseWriter) {
writeTextResponse(rw, "not authorized", http.StatusUnauthorized)
}
func writeResponse(w http.ResponseWriter, data string) bool {
if _, err := w.Write([]byte(data)); err != nil {
stack := debug.Stack()
log.Println("failed to write response:", err, "\n", string(stack))
return false
}
w.(http.Flusher).Flush()
return true
}
func writeSectionHeader(w http.ResponseWriter, sec ibus.IDataSection) bool {
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
_, _ = buf.WriteString(fmt.Sprintf(`{"type":%q`, sec.Type())) // error impossible
if len(sec.Path()) > 0 {
_, _ = buf.WriteString(`,"path":[`) // error impossible
for i, p := range sec.Path() {
if i > 0 {
_, _ = buf.WriteString(",") // error impossible
}
_, _ = buf.WriteString(fmt.Sprintf(`%q`, p)) // error impossible
}
_, _ = buf.WriteString("]") // error impossible
}
if !writeResponse(w, string(buf.Bytes())) {
return false
}
return true
}
func writeSection(w http.ResponseWriter, isec ibus.ISection) bool {
switch sec := isec.(type) {
case ibus.IArraySection:
if !writeSectionHeader(w, sec) {
return false
}
isFirst := true
closer := "}"
// ctx.Done() is tracked by ibusnats implementation: writing to section elem channel -> read here, ctxdone -> close elem channel
for val, ok := sec.Next(); ok; val, ok = sec.Next() {
if isFirst {
if !writeResponse(w, fmt.Sprintf(`,"elements":[%s`, string(val))) {
return false
}
isFirst = false
closer = "]}"
} else {
if !writeResponse(w, fmt.Sprintf(`,%s`, string(val))) {
return false
}
}
}
if !writeResponse(w, closer) {
return false
}
case ibus.IObjectSection:
if !writeSectionHeader(w, sec) {
return false
}
val := sec.Value()
if !writeResponse(w, fmt.Sprintf(`,"elements":%s}`, string(val))) {
return false
}
case ibus.IMapSection:
if !writeSectionHeader(w, sec) {
return false
}
isFirst := true
closer := "}"
// ctx.Done() is tracked by ibusnats implementation: writing to section elem channel -> read here, ctxdone -> close elem channel
for name, val, ok := sec.Next(); ok; name, val, ok = sec.Next() {
if isFirst {
if !writeResponse(w, fmt.Sprintf(`,"elements":{%q:%s`, name, string(val))) {
return false
}
isFirst = false
closer = "}}"
} else {
if !writeResponse(w, fmt.Sprintf(`,%q:%s`, name, string(val))) {
return false
}
}
}
if !writeResponse(w, closer) {
return false
}
}
return true
}
func (i *implIBusBP2) SendRequest2(ctx context.Context, request ibus.Request, timeout time.Duration) (res ibus.Response, sections <-chan ibus.ISection, secError *error, err error) {
return ibus.SendRequest2(ctx, request, timeout)
}
func (i *implIBusBP2) SendResponse(sender interface{}, response ibus.Response) {}
func (i *implIBusBP2) SendParallelResponse2(sender interface{}) (rsender ibus.IResultSenderClosable) {
return nil
}