-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathchanges_api.go
697 lines (618 loc) · 20.7 KB
/
changes_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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// Copyright 2012-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rest
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/couchbase/sync_gateway/base"
ch "github.com/couchbase/sync_gateway/channels"
"github.com/couchbase/sync_gateway/db"
"golang.org/x/net/websocket"
)
// Minimum value of _changes?heartbeat property
const kMinHeartbeatMS = 25 * 1000
// Default value of _changes?heartbeat property
const kDefaultHeartbeatMS = 0
// Default value of _changes?timeout property
const kDefaultTimeoutMS = 5 * 60 * 1000
// Maximum value of _changes?timeout property
const kMaxTimeoutMS = 15 * 60 * 1000
// Values for feed parameter on changes request
const feedTypeContinuous = "continuous"
const feedTypeLongpoll = "longpoll"
const feedTypeNormal = "normal"
const feedTypeWebsocket = "websocket"
func (h *handler) handleRevsDiff() error {
var input map[string][]string
err := h.readJSONInto(&input)
if err != nil {
return err
}
_, _ = h.response.Write([]byte("{"))
first := true
for docid, revs := range input {
missing, possible := h.collection.RevDiff(h.ctx(), docid, revs)
if missing != nil {
docOutput := map[string]interface{}{"missing": missing}
if possible != nil {
docOutput["possible_ancestors"] = possible
}
if !first {
_, _ = h.response.Write([]byte(",\n"))
}
first = false
_, _ = h.response.Write([]byte(fmt.Sprintf("%q:", docid)))
err = h.addJSON(docOutput)
if err != nil {
return err
}
}
}
_, _ = h.response.Write([]byte("}"))
return nil
}
// UpdateChangesOptionsFromQuery handles any changes POST requests that send parameters in the POST body AND in the query string. If any parameters
// are present in the query string, they override the values sent in the body.
func (h *handler) updateChangesOptionsFromQuery(feed *string, options *db.ChangesOptions, filter *string, channelsArray []string, docIdsArray []string) (newChannelsArray []string, newDocIdsArray []string, err error) {
if h.rq.URL.RawQuery == "" {
return channelsArray, docIdsArray, nil
}
values := h.getQueryValues()
if _, ok := values["feed"]; ok {
*feed = h.getQuery("feed")
}
if _, ok := values["since"]; ok {
if options.Since, err = db.ParsePlainSequenceID(h.getJSONStringQuery("since")); err != nil {
return nil, nil, err
}
}
if _, ok := values["limit"]; ok {
options.Limit = int(h.getIntQuery("limit", 0))
}
if _, ok := values["style"]; ok {
options.Conflicts = (h.getQuery("style") == "all_docs")
}
if _, ok := values["active_only"]; ok {
options.ActiveOnly = h.getBoolQuery("active_only")
}
if _, ok := values["include_docs"]; ok {
options.IncludeDocs = (h.getBoolQuery("include_docs"))
}
if _, ok := values["filter"]; ok {
*filter = h.getQuery("filter")
}
if _, ok := values["channels"]; ok {
channelsParam := h.getQuery("channels")
if channelsParam != "" {
channelsArray = strings.Split(channelsParam, ",")
}
}
if _, ok := values["doc_ids"]; ok {
docidsParam := h.getQuery("doc_ids")
if docidsParam != "" {
var querydocidKeys []string
err := base.JSONUnmarshal([]byte(docidsParam), &querydocidKeys)
if err == nil {
if len(querydocidKeys) > 0 {
docIdsArray = querydocidKeys
}
} else {
// This is not a JSON array so treat as a simple
// comma separated list of doc id's
docIdsArray = strings.Split(docidsParam, ",")
}
}
}
if _, ok := values["heartbeat"]; ok {
options.HeartbeatMs = base.GetRestrictedIntQuery(
h.getQueryValues(),
"heartbeat",
kDefaultHeartbeatMS,
kMinHeartbeatMS,
uint64(h.server.Config.Replicator.MaxHeartbeat.Value().Milliseconds()),
true,
)
}
if _, ok := values["timeout"]; ok {
options.TimeoutMs = base.GetRestrictedIntQuery(
h.getQueryValues(),
"timeout",
kDefaultTimeoutMS,
0,
kMaxTimeoutMS,
true,
)
}
return channelsArray, docIdsArray, nil
}
// Top-level handler for _changes feed requests. Accepts GET or POST requests.
func (h *handler) handleChanges() error {
// http://wiki.apache.org/couchdb/HTTP_database_API#Changes
// http://docs.couchdb.org/en/latest/api/database/changes.html
var feed string
var options db.ChangesOptions
var filter string
var channelsArray []string
var docIdsArray []string
if h.rq.Method == "GET" {
// GET request has parameters in URL:
feed = h.getQuery("feed")
var err error
if options.Since, err = db.ParsePlainSequenceID(h.getJSONStringQuery("since")); err != nil {
return err
}
options.Limit = int(h.getIntQuery("limit", 0))
options.Conflicts = h.getQuery("style") == "all_docs"
options.ActiveOnly = h.getBoolQuery("active_only")
options.IncludeDocs = h.getBoolQuery("include_docs")
options.Revocations = h.getBoolQuery("revocations")
useRequestPlus, _ := h.getOptBoolQuery("request_plus", h.db.Options.ChangesRequestPlus)
if useRequestPlus && feed != feedTypeContinuous {
var seqErr error
options.RequestPlusSeq, seqErr = h.db.GetRequestPlusSequence()
if seqErr != nil {
return base.HTTPErrorf(http.StatusServiceUnavailable, "Unable to retrieve requestPlus sequence")
}
}
filter = h.getQuery("filter")
channelsParam := h.getQuery("channels")
if channelsParam != "" {
channelsArray = strings.Split(channelsParam, ",")
}
docidsParam := h.getQuery("doc_ids")
if docidsParam != "" {
var docidKeys []string
err := base.JSONUnmarshal([]byte(docidsParam), &docidKeys)
if err == nil {
if len(docidKeys) > 0 {
docIdsArray = docidKeys
}
} else {
// This is not a JSON array so treat as a simple
// comma separated list of doc id's
docIdsArray = strings.Split(docidsParam, ",")
}
}
options.HeartbeatMs = base.GetRestrictedIntQuery(
h.getQueryValues(),
"heartbeat",
kDefaultHeartbeatMS,
kMinHeartbeatMS,
uint64(h.server.Config.Replicator.MaxHeartbeat.Value().Milliseconds()),
true,
)
options.TimeoutMs = base.GetRestrictedIntQuery(
h.getQueryValues(),
"timeout",
kDefaultTimeoutMS,
0,
kMaxTimeoutMS,
true,
)
} else {
// POST request has parameters in JSON body:
body, err := h.readBody()
if err != nil {
return err
}
feed, options, filter, channelsArray, docIdsArray, _, err = h.readChangesOptionsFromJSON(body)
if err != nil {
return err
}
channelsArray, docIdsArray, err = h.updateChangesOptionsFromQuery(&feed, &options, &filter, channelsArray, docIdsArray)
if err != nil {
return err
}
to := ""
if h.user != nil && h.user.Name() != "" {
to = fmt.Sprintf(" (to %s)", h.user.Name())
}
base.DebugfCtx(h.ctx(), base.KeyChanges, "Changes POST request. URL: %v, feed: %v, options: %+v, filter: %v, bychannel: %v, docIds: %v %s",
h.rq.URL, feed, options, filter, base.UD(channelsArray), base.UD(docIdsArray), base.UD(to))
}
// Default to feed type normal
if feed == "" {
feed = "normal"
}
needRelease, concurrentReplicationsErr := h.server.incrementConcurrentReplications(h.rqCtx)
if concurrentReplicationsErr != nil {
return concurrentReplicationsErr
}
// if we haven't incremented the active replicator due to MaxConcurrentReplications being 0, we don't need to decrement it
if needRelease {
defer h.server.decrementConcurrentReplications(h.rqCtx)
}
// Get the channels as parameters to an imaginary "bychannel" filter.
// The default is all channels the user can access.
userChannels := base.SetOf(ch.AllChannelWildcard)
if filter != "" {
if filter == base.ByChannelFilter {
if channelsArray == nil {
return base.HTTPErrorf(http.StatusBadRequest, "Missing 'channels' filter parameter")
}
var err error
userChannels, err = ch.SetFromArray(channelsArray, ch.ExpandStar)
if err != nil {
return err
}
if len(userChannels) == 0 {
return base.HTTPErrorf(http.StatusBadRequest, "Empty channel list")
}
} else if filter == base.DocIDsFilter {
if feed != "normal" {
return base.HTTPErrorf(http.StatusBadRequest, "Filter '_doc_ids' is only valid for feed=normal replications")
}
if docIdsArray == nil {
return base.HTTPErrorf(http.StatusBadRequest, "Missing 'doc_ids' filter parameter")
}
if len(docIdsArray) == 0 {
return base.HTTPErrorf(http.StatusBadRequest, "Empty doc_ids list")
}
} else {
return base.HTTPErrorf(http.StatusBadRequest, "Unknown filter; try sync_gateway/bychannel or _doc_ids")
}
}
// Pull replication stats by type
if feed == "normal" {
h.db.DatabaseContext.DbStats.CBLReplicationPull().NumPullReplActiveOneShot.Add(1)
h.db.DatabaseContext.DbStats.CBLReplicationPull().NumPullReplTotalOneShot.Add(1)
defer h.db.DatabaseContext.DbStats.CBLReplicationPull().NumPullReplActiveOneShot.Add(-1)
} else {
h.db.DbStats.CBLReplicationPull().NumPullReplActiveContinuous.Add(1)
h.db.DbStats.CBLReplicationPull().NumPullReplTotalContinuous.Add(1)
defer h.db.DbStats.CBLReplicationPull().NumPullReplActiveContinuous.Add(-1)
}
// Overall replication counts
h.db.DatabaseContext.DbStats.Database().NumReplicationsActive.Add(1)
h.db.DatabaseContext.DbStats.Database().NumReplicationsTotal.Add(1)
defer h.db.DatabaseContext.DbStats.Database().NumReplicationsActive.Add(-1)
changesCtx, changesCtxCancel := context.WithCancel(h.ctx())
options.ChangesCtx = changesCtx
forceClose := false
h.auditChangesFeedStart(options, feed, filter, docIdsArray, channelsArray)
var err error
switch feed {
case feedTypeNormal:
if filter == base.DocIDsFilter {
err, forceClose = h.sendSimpleChanges(userChannels, options, docIdsArray)
} else {
err, forceClose = h.sendSimpleChanges(userChannels, options, nil)
}
case feedTypeLongpoll:
options.Wait = true
err, forceClose = h.sendSimpleChanges(userChannels, options, nil)
case feedTypeContinuous:
err, forceClose = h.sendContinuousChangesByHTTP(userChannels, options)
case feedTypeWebsocket:
err, forceClose = h.sendContinuousChangesByWebSocket(userChannels, options)
default:
err = base.HTTPErrorf(http.StatusBadRequest, "Unknown feed type")
forceClose = false
}
changesCtxCancel()
// On forceClose, send notify to trigger immediate exit from change waiter
if forceClose {
user := ""
if h.user != nil {
user = h.user.Name()
}
h.db.DatabaseContext.NotifyTerminatedChanges(h.ctx(), user)
}
return err
}
func (h *handler) sendSimpleChanges(channels base.Set, options db.ChangesOptions, docids []string) (error, bool) {
lastSeq := options.Since
var first bool = true
var feed <-chan *db.ChangeEntry
var err error
if len(docids) > 0 {
feed, err = h.collection.DocIDChangesFeed(h.ctx(), channels, docids, options)
} else {
feed, err = h.collection.MultiChangesFeed(h.ctx(), channels, options)
}
if err != nil {
return err, false
}
h.setHeader("Content-Type", "application/json")
h.setHeader("Cache-Control", "private, max-age=0, no-cache, no-store")
_, _ = h.response.Write([]byte("{\"results\":[\r\n"))
logStatus := h.logStatusWithDuration
if options.Wait {
logStatus = h.logStatus
h.flush()
}
message := "OK"
forceClose := false
if feed != nil {
var heartbeat, timeout <-chan time.Time
if options.Wait {
// Set up heartbeat/timeout
if options.HeartbeatMs > 0 {
ticker := time.NewTicker(time.Duration(options.HeartbeatMs) * time.Millisecond)
defer ticker.Stop()
heartbeat = ticker.C
} else if options.TimeoutMs > 0 {
timer := time.NewTimer(time.Duration(options.TimeoutMs) * time.Millisecond)
defer timer.Stop()
timeout = timer.C
}
}
encoder := base.JSONEncoderCanonical(h.response)
loop:
for {
select {
case entry, ok := <-feed:
if !ok {
break loop // end of feed
}
if nil != entry {
if entry.Err != nil {
break loop // error returned by feed - end changes
}
if first {
first = false
} else {
_, _ = h.response.Write([]byte(","))
}
_ = encoder.Encode(entry)
lastSeq = entry.Seq
entry.AuditReadEvent(h.ctx())
}
case <-heartbeat:
_, err = h.response.Write([]byte("\n"))
h.flush()
base.DebugfCtx(h.ctx(), base.KeyChanges, "heartbeat written to _changes feed for request received")
case <-timeout:
message = "OK (timeout)"
forceClose = true
break loop
case <-h.rq.Context().Done():
base.InfofCtx(h.ctx(), base.KeyChanges, "Connection lost from client")
forceClose = true
break loop
case <-h.db.ExitChanges:
message = "OK DB has gone offline"
forceClose = true
break loop
}
if err != nil {
logStatus(599, fmt.Sprintf("Write error: %v", err))
return nil, forceClose // error is probably because the client closed the connection
}
}
}
// set forceClose here if the close was initiated by a ChangesEntry.Err message
if h.rq.Context().Err() != nil {
forceClose = true
}
s := fmt.Sprintf("],\n\"last_seq\":%q}\n", lastSeq.String())
_, _ = h.response.Write([]byte(s))
logStatus(http.StatusOK, message)
return nil, forceClose
}
// This is the core functionality of both the HTTP and WebSocket-based continuous change feed.
// It defers to a callback function 'send()' to actually send the changes to the client.
// It will call send(nil) to notify that it's caught up and waiting for new changes, or as
// a periodic heartbeat while waiting.
func (h *handler) generateContinuousChanges(inChannels base.Set, options db.ChangesOptions, send func([]*db.ChangeEntry) error) (error, bool) {
// Ensure continuous is set, since generateChanges now supports both continuous and one-shot
options.Continuous = true
err, forceClose := db.GenerateChanges(h.ctx(), h.collection, inChannels, options, nil, send)
if sendErr, ok := err.(*db.ChangesSendErr); ok {
h.logStatus(http.StatusOK, fmt.Sprintf("Write error: %v", sendErr))
return nil, forceClose // error is probably because the client closed the connection
} else {
h.logStatus(http.StatusOK, "OK (continuous feed closed)")
}
return err, forceClose
}
func (h *handler) sendContinuousChangesByHTTP(inChannels base.Set, options db.ChangesOptions) (error, bool) {
// Setting a non-default content type will keep the client HTTP framework from trying to sniff
// a real content-type from the response text, which can delay or prevent the client app from
// receiving the response.
h.setHeader("Content-Type", "application/octet-stream")
h.setHeader("Cache-Control", "private, max-age=0, no-cache, no-store")
h.logStatus(http.StatusOK, "sending continuous feed")
return h.generateContinuousChanges(inChannels, options, func(changes []*db.ChangeEntry) error {
var err error
if changes != nil {
for _, change := range changes {
data, _ := base.JSONMarshal(change)
if _, err = h.response.Write(data); err != nil {
break
}
if _, err = h.response.Write([]byte("\n")); err != nil {
break
}
change.AuditReadEvent(h.ctx())
}
} else {
_, err = h.response.Write([]byte("\n"))
}
h.flush()
return err
})
}
func (h *handler) sendContinuousChangesByWebSocket(inChannels base.Set, options db.ChangesOptions) (error, bool) {
forceClose := false
handler := func(conn *websocket.Conn) {
h.logStatus(101, "Upgraded to WebSocket protocol")
defer func() {
if err := conn.Close(); err != nil {
base.WarnfCtx(h.ctx(), "WebSocket connection (%s) closed with error %v", h.formatSerialNumber(), err)
}
base.InfofCtx(h.ctx(), base.KeyHTTP, "%s: --> WebSocket closed", h.formatSerialNumber())
}()
// Read changes-feed options from an initial incoming WebSocket message in JSON format:
var wsoptions db.ChangesOptions
var compress bool
if msg, err := readWebSocketMessage(h.ctx(), conn); err != nil {
return
} else {
var channelNames []string
var err error
if _, wsoptions, _, channelNames, _, compress, err = h.readChangesOptionsFromJSON(msg); err != nil {
return
}
if channelNames != nil {
inChannels, _ = ch.SetFromArray(channelNames, ch.ExpandStar)
}
}
// Copy options.ChangesCtx to new WebSocket options
// options.ChangesCtx will be cancelled automatically when
// changes feed completes
wsoptions.ChangesCtx = options.ChangesCtx
// Set up GZip compression
var writer *bytes.Buffer
var zipWriter *gzip.Writer
if compress {
writer = bytes.NewBuffer(nil)
zipWriter = GetGZipWriter(writer)
}
caughtUp := false
_, forceClose = h.generateContinuousChanges(inChannels, wsoptions, func(changes []*db.ChangeEntry) error {
var data []byte
if changes != nil {
data, _ = base.JSONMarshal(changes)
} else if !caughtUp {
caughtUp = true
data, _ = base.JSONMarshal([]*db.ChangeEntry{})
} else {
data = []byte{}
}
if compress && len(data) > 8 {
// Compress JSON, using same GZip context, and send as binary msg:
_, _ = zipWriter.Write(data)
_ = zipWriter.Flush()
data = writer.Bytes()
writer.Reset()
conn.PayloadType = websocket.BinaryFrame
} else {
conn.PayloadType = websocket.TextFrame
}
_, err := conn.Write(data)
if err != nil {
return err
}
for _, change := range changes {
change.AuditReadEvent(h.ctx())
}
return err
})
if zipWriter != nil {
ReturnGZipWriter(zipWriter)
}
}
server := websocket.Server{
Handshake: func(*websocket.Config, *http.Request) error { return nil },
Handler: handler,
}
server.ServeHTTP(h.response, h.rq)
return nil, forceClose
}
func (h *handler) readChangesOptionsFromJSON(jsonData []byte) (feed string, options db.ChangesOptions, filter string, channelsArray []string, docIdsArray []string, compress bool, err error) {
var input struct {
Feed string `json:"feed"`
Since db.SequenceID `json:"since"`
Limit int `json:"limit"`
Style string `json:"style"`
IncludeDocs bool `json:"include_docs"`
Filter string `json:"filter"`
Channels string `json:"channels"` // a filter query param, so it has to be a string
DocIds []string `json:"doc_ids"`
HeartbeatMs *uint64 `json:"heartbeat"`
TimeoutMs *uint64 `json:"timeout"`
AcceptEncoding string `json:"accept_encoding"`
ActiveOnly bool `json:"active_only"` // Return active revisions only
RequestPlus *bool `json:"request_plus"` // Wait for sequence buffering to catch up to database seq value at time request was issued
}
// Initialize since clock and hasher ahead of unmarshalling sequence
if h.db != nil {
input.Since = db.CreateZeroSinceValue()
}
if err = base.JSONUnmarshal(jsonData, &input); err != nil {
return
}
feed = input.Feed
options.Since = input.Since
options.Limit = input.Limit
options.Conflicts = input.Style == "all_docs"
options.ActiveOnly = input.ActiveOnly
options.IncludeDocs = input.IncludeDocs
filter = input.Filter
if input.Channels != "" {
channelsArray = strings.Split(input.Channels, ",")
}
docIdsArray = input.DocIds
options.HeartbeatMs = base.GetRestrictedInt(
input.HeartbeatMs,
kDefaultHeartbeatMS,
kMinHeartbeatMS,
uint64(h.server.Config.Replicator.MaxHeartbeat.Value().Milliseconds()),
true,
)
options.TimeoutMs = base.GetRestrictedInt(
input.TimeoutMs,
kDefaultTimeoutMS,
0,
kMaxTimeoutMS,
true,
)
compress = (input.AcceptEncoding == "gzip")
if h.db != nil && feed != feedTypeContinuous {
useRequestPlus := h.db.Options.ChangesRequestPlus
if input.RequestPlus != nil {
useRequestPlus = *input.RequestPlus
}
if useRequestPlus {
var seqErr error
options.RequestPlusSeq, seqErr = h.db.GetRequestPlusSequence()
if seqErr != nil {
err = base.HTTPErrorf(http.StatusServiceUnavailable, "Unable to retrieve requestPlus sequence: %v", seqErr)
return
}
}
}
return
}
func (h *handler) auditChangesFeedStart(options db.ChangesOptions, feedType string, filter string, docIDs, channels []string) {
auditFields := base.AuditFields{
base.AuditFieldSince: options.Since.String(),
base.AuditFieldFeedType: feedType,
}
if filter != "" {
auditFields[base.AuditFieldFilter] = filter
if len(docIDs) > 0 {
auditFields[base.AuditFieldDocIDs] = docIDs
}
if len(channels) > 0 {
auditFields[base.AuditFieldChannels] = channels
}
}
if options.IncludeDocs {
auditFields[base.AuditFieldIncludeDocs] = true
}
base.Audit(h.ctx(), base.AuditIDChangesFeedStarted, auditFields)
}
// Helper function to read a complete message from a WebSocket
func readWebSocketMessage(ctx context.Context, conn *websocket.Conn) ([]byte, error) {
var message []byte
if err := websocket.Message.Receive(conn, &message); err != nil {
if err != io.EOF {
base.WarnfCtx(ctx, "Error reading initial websocket message: %v", err)
return nil, err
}
}
return message, nil
}