本文整理汇总了Golang中github.com/couchbase/sync_gateway/db.ChangesOptions类的典型用法代码示例。如果您正苦于以下问题:Golang ChangesOptions类的具体用法?Golang ChangesOptions怎么用?Golang ChangesOptions使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ChangesOptions类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: sendContinuousChangesByWebSocket
func (h *handler) sendContinuousChangesByWebSocket(inChannels base.Set, options db.ChangesOptions) error {
handler := func(conn *websocket.Conn) {
h.logStatus(101, "Upgraded to WebSocket protocol")
defer func() {
conn.Close()
base.LogTo("HTTP+", "#%03d: --> WebSocket closed", h.serialNumber)
}()
// Read changes-feed options from an initial incoming WebSocket message in JSON format:
if msg, err := readWebSocketMessage(conn); err != nil {
return
} else {
var channelNames []string
var err error
if _, options, _, channelNames, err = h.readChangesOptionsFromJSON(msg); err != nil {
return
}
if channelNames != nil {
inChannels, _ = channels.SetFromArray(channelNames, channels.ExpandStar)
}
}
options.Terminator = make(chan bool)
defer close(options.Terminator)
caughtUp := false
h.generateContinuousChanges(inChannels, options, func(changes []*db.ChangeEntry) error {
var data []byte
if changes != nil {
data, _ = json.Marshal(changes)
} else if !caughtUp {
caughtUp = true
data, _ = json.Marshal([]*db.ChangeEntry{})
} else {
data = []byte{}
}
_, err := conn.Write(data)
return err
})
}
server := websocket.Server{
Handshake: func(*websocket.Config, *http.Request) error { return nil },
Handler: handler,
}
server.ServeHTTP(h.response, h.rq)
return nil
}
开发者ID:dbergan,项目名称:sync_gateway,代码行数:47,代码来源:changes_api.go
示例2: handleChanges
// 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
restExpvars.Add("changesFeeds_total", 1)
restExpvars.Add("changesFeeds_active", 1)
defer restExpvars.Add("changesFeeds_active", -1)
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 = h.db.ParseSequenceID(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"))
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 := json.Unmarshal([]byte(docidsParam), &docidKeys)
if err != nil {
err = base.HTTPErrorf(http.StatusBadRequest, "Bad doc id's")
}
if len(docidKeys) > 0 {
docIdsArray = docidKeys
}
}
options.HeartbeatMs = getRestrictedIntQuery(
h.rq.URL.Query(),
"heartbeat",
kDefaultHeartbeatMS,
kMinHeartbeatMS,
h.server.config.MaxHeartbeat*1000,
true,
)
options.TimeoutMs = getRestrictedIntQuery(
h.rq.URL.Query(),
"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
}
}
// Get the channels as parameters to an imaginary "bychannel" filter.
// The default is all channels the user can access.
userChannels := channels.SetOf(channels.AllChannelWildcard)
if filter != "" {
if filter == "sync_gateway/bychannel" {
if channelsArray == nil {
return base.HTTPErrorf(http.StatusBadRequest, "Missing 'channels' filter parameter")
}
var err error
userChannels, err = channels.SetFromArray(channelsArray, channels.ExpandStar)
if err != nil {
return err
}
if len(userChannels) == 0 {
return base.HTTPErrorf(http.StatusBadRequest, "Empty channel list")
}
} else if filter == "_doc_ids" {
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")
}
}
//.........这里部分代码省略.........
开发者ID:ethanfrey,项目名称:sync_gateway,代码行数:101,代码来源:changes_api.go
示例3: generateContinuousChanges
// 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) {
// Set up heartbeat/timeout
var timeoutInterval time.Duration
var timer *time.Timer
var heartbeat <-chan time.Time
if options.HeartbeatMs > 0 {
ticker := time.NewTicker(time.Duration(options.HeartbeatMs) * time.Millisecond)
defer ticker.Stop()
heartbeat = ticker.C
} else if options.TimeoutMs > 0 {
timeoutInterval = time.Duration(options.TimeoutMs) * time.Millisecond
defer func() {
if timer != nil {
timer.Stop()
}
}()
}
options.Wait = true // we want the feed channel to wait for changes
options.Continuous = true // and to keep sending changes indefinitely
var lastSeq db.SequenceID
var feed <-chan *db.ChangeEntry
var timeout <-chan time.Time
var err error
var closeNotify <-chan bool
cn, ok := h.response.(http.CloseNotifier)
if ok {
closeNotify = cn.CloseNotify()
} else {
base.LogTo("Changes", "continuous changes cannot get Close Notifier from ResponseWriter")
}
forceClose := false
loop:
for {
if feed == nil {
// Refresh the feed of all current changes:
if lastSeq.IsNonZero() { // start after end of last feed
options.Since = lastSeq
}
if h.db.IsClosed() {
forceClose = true
break loop
}
feed, err = h.db.MultiChangesFeed(inChannels, options)
if err != nil || feed == nil {
return err, forceClose
}
}
if timeoutInterval > 0 && timer == nil {
// Timeout resets after every change is sent
timer = time.NewTimer(timeoutInterval)
timeout = timer.C
}
// Wait for either a new change, a heartbeat, or a timeout:
select {
case entry, ok := <-feed:
if !ok {
feed = nil
} else if entry == nil {
err = send(nil)
} else if entry.Err != nil {
break loop // error returned by feed - end changes
} else {
entries := []*db.ChangeEntry{entry}
waiting := false
// Batch up as many entries as we can without waiting:
collect:
for len(entries) < 20 {
select {
case entry, ok = <-feed:
if !ok {
feed = nil
break collect
} else if entry == nil {
waiting = true
break collect
} else if entry.Err != nil {
break loop // error returned by feed - end changes
}
entries = append(entries, entry)
default:
break collect
}
}
base.LogTo("Changes", "sending %d change(s)", len(entries))
err = send(entries)
if err == nil && waiting {
err = send(nil)
}
//.........这里部分代码省略.........
开发者ID:ethanfrey,项目名称:sync_gateway,代码行数:101,代码来源:changes_api.go
示例4: updateChangesOptionsFromQuery
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.rq.URL.Query()
if _, ok := values["feed"]; ok {
*feed = h.getQuery("feed")
}
if _, ok := values["since"]; ok {
if options.Since, err = h.db.ParseSequenceID(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 := json.Unmarshal([]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 = getRestrictedIntQuery(
h.rq.URL.Query(),
"heartbeat",
kDefaultHeartbeatMS,
kMinHeartbeatMS,
h.server.config.MaxHeartbeat*1000,
true,
)
}
if _, ok := values["timeout"]; ok {
options.TimeoutMs = getRestrictedIntQuery(
h.rq.URL.Query(),
"timeout",
kDefaultTimeoutMS,
0,
kMaxTimeoutMS,
true,
)
}
return channelsArray, docIdsArray, nil
}
开发者ID:paulharter,项目名称:sync_gateway,代码行数:85,代码来源:changes_api.go
示例5: sendContinuousChangesByWebSocket
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() {
conn.Close()
base.LogTo("HTTP+", "#%03d: --> WebSocket closed", h.serialNumber)
}()
// Read changes-feed options from an initial incoming WebSocket message in JSON format:
var wsoptions db.ChangesOptions
var compress bool
if msg, err := readWebSocketMessage(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.Terminator to new WebSocket options
//options.Terminator will be closed automatically when
//changes feed completes
wsoptions.Terminator = options.Terminator
// 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, _ = json.Marshal(changes)
} else if !caughtUp {
caughtUp = true
data, _ = json.Marshal([]*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)
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
}
开发者ID:paulharter,项目名称:sync_gateway,代码行数:75,代码来源:changes_api.go
示例6: handleChanges
// 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
restExpvars.Add("changesFeeds_total", 1)
restExpvars.Add("changesFeeds_active", 1)
defer restExpvars.Add("changesFeeds_active", -1)
var feed string
var options db.ChangesOptions
var filter string
var channelsArray []string
if h.rq.Method == "GET" {
// GET request has parameters in URL:
feed = h.getQuery("feed")
var err error
if options.Since, err = db.ParseSequenceID(h.getQuery("since")); err != nil {
return err
}
options.Limit = int(h.getIntQuery("limit", 0))
options.Conflicts = (h.getQuery("style") == "all_docs")
options.IncludeDocs = (h.getBoolQuery("include_docs"))
filter = h.getQuery("filter")
channelsParam := h.getQuery("channels")
if channelsParam != "" {
channelsArray = strings.Split(channelsParam, ",")
}
options.HeartbeatMs = getRestrictedIntQuery(h.rq.URL.Query(), "heartbeat", 0, kMinHeartbeatMS, 0)
options.TimeoutMs = getRestrictedIntQuery(h.rq.URL.Query(), "timeout", kDefaultTimeoutMS, 0, kMaxTimeoutMS)
} else {
// POST request has parameters in JSON body:
body, err := h.readBody()
if err != nil {
return err
}
feed, options, filter, channelsArray, err = readChangesOptionsFromJSON(body)
if err != nil {
return err
}
}
// Get the channels as parameters to an imaginary "bychannel" filter.
// The default is all channels the user can access.
userChannels := channels.SetOf(channels.AllChannelWildcard)
if filter != "" {
if filter != "sync_gateway/bychannel" {
return base.HTTPErrorf(http.StatusBadRequest, "Unknown filter; try sync_gateway/bychannel")
}
if channelsArray == nil {
return base.HTTPErrorf(http.StatusBadRequest, "Missing 'channels' filter parameter")
}
var err error
userChannels, err = channels.SetFromArray(channelsArray, channels.ExpandStar)
if err != nil {
return err
}
if len(userChannels) == 0 {
return base.HTTPErrorf(http.StatusBadRequest, "Empty channel list")
}
}
h.db.ChangesClientStats.Increment()
defer h.db.ChangesClientStats.Decrement()
options.Terminator = make(chan bool)
defer close(options.Terminator)
switch feed {
case "normal", "":
return h.sendSimpleChanges(userChannels, options)
case "longpoll":
options.Wait = true
return h.sendSimpleChanges(userChannels, options)
case "continuous":
return h.sendContinuousChangesByHTTP(userChannels, options)
case "websocket":
return h.sendContinuousChangesByWebSocket(userChannels, options)
default:
return base.HTTPErrorf(http.StatusBadRequest, "Unknown feed type")
}
}
开发者ID:guoyu07,项目名称:sync_gateway,代码行数:83,代码来源:changes_api.go
示例7: generateContinuousChanges
// 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 {
// Set up heartbeat/timeout
var timeoutInterval time.Duration
var timer *time.Timer
var heartbeat <-chan time.Time
if options.HeartbeatMs > 0 {
ticker := time.NewTicker(time.Duration(options.HeartbeatMs) * time.Millisecond)
defer ticker.Stop()
heartbeat = ticker.C
} else if options.TimeoutMs > 0 {
timeoutInterval = time.Duration(options.TimeoutMs) * time.Millisecond
defer func() {
if timer != nil {
timer.Stop()
}
}()
}
options.Wait = true // we want the feed channel to wait for changes
options.Continuous = true // and to keep sending changes indefinitely
var lastSeq db.SequenceID
var feed <-chan *db.ChangeEntry
var timeout <-chan time.Time
var err error
loop:
for {
if feed == nil {
// Refresh the feed of all current changes:
if lastSeq.Seq > 0 { // start after end of last feed
options.Since = lastSeq
}
if h.db.IsClosed() {
break loop
}
feed, err = h.db.MultiChangesFeed(inChannels, options)
if err != nil || feed == nil {
return err
}
}
if timeoutInterval > 0 && timer == nil {
// Timeout resets after every change is sent
timer = time.NewTimer(timeoutInterval)
timeout = timer.C
}
// Wait for either a new change, a heartbeat, or a timeout:
select {
case entry, ok := <-feed:
if !ok {
feed = nil
} else if entry == nil {
err = send(nil)
} else {
entries := []*db.ChangeEntry{entry}
waiting := false
// Batch up as many entries as we can without waiting:
collect:
for len(entries) < 20 {
select {
case entry, ok = <-feed:
if !ok {
feed = nil
break collect
} else if entry == nil {
waiting = true
break collect
}
entries = append(entries, entry)
default:
break collect
}
}
base.LogTo("Changes", "sending %d change(s)", len(entries))
err = send(entries)
if err == nil && waiting {
err = send(nil)
}
lastSeq = entries[len(entries)-1].Seq
if options.Limit > 0 {
if len(entries) >= options.Limit {
break loop
}
options.Limit -= len(entries)
}
}
// Reset the timeout after sending an entry:
if timer != nil {
timer.Stop()
timer = nil
}
case <-heartbeat:
err = send(nil)
//.........这里部分代码省略.........
开发者ID:guoyu07,项目名称:sync_gateway,代码行数:101,代码来源:changes_api.go
注:本文中的github.com/couchbase/sync_gateway/db.ChangesOptions类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论