本文整理匯總了Golang中github.com/cloudfoundry-incubator/bbs/events.EventSource.Close方法的典型用法代碼示例。如果您正苦於以下問題:Golang EventSource.Close方法的具體用法?Golang EventSource.Close怎麽用?Golang EventSource.Close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/cloudfoundry-incubator/bbs/events.EventSource
的用法示例。
在下文中一共展示了EventSource.Close方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Run
func (watcher *Watcher) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
logger := watcher.logger.Session("watcher")
logger.Info("starting")
defer logger.Info("finished")
var subscription events.EventSource
subscriptionChan := make(chan events.EventSource, 1)
go subscribeToEvents(logger, watcher.bbsClient, subscriptionChan)
eventChan := make(chan models.Event, 1)
nextErrCount := 0
close(ready)
logger.Info("started")
for {
select {
case subscription = <-subscriptionChan:
if subscription != nil {
go nextEvent(logger, subscription, eventChan)
} else {
go subscribeToEvents(logger, watcher.bbsClient, subscriptionChan)
}
case event := <-eventChan:
if event != nil {
watcher.handleEvent(logger, event)
} else {
nextErrCount += 1
if nextErrCount > 2 {
nextErrCount = 0
go subscribeToEvents(logger, watcher.bbsClient, subscriptionChan)
break
}
}
go nextEvent(logger, subscription, eventChan)
case <-signals:
logger.Info("stopping")
err := subscription.Close()
if err != nil {
logger.Error("failed-closing-event-source", err)
}
return nil
}
}
}
示例2:
var event models.Event
for {
Eventually(eventChannel).Should(Receive(&event))
if event.EventType() == models.EventTypeDesiredLRPRemoved {
break
}
}
})
AfterEach(func() {
Eventually(done).Should(BeClosed())
ginkgomon.Kill(bbsProcess)
})
It("does not emit latency metrics", func() {
eventSource.Close()
timeout := time.After(50 * time.Millisecond)
for {
select {
case envelope := <-testMetricsChan:
if envelope.GetEventType() == sonde_events.Envelope_ValueMetric {
Expect(*envelope.ValueMetric.Name).NotTo(Equal("RequestLatency"))
}
case <-timeout:
return
}
}
})
It("emits request counting metrics", func() {
示例3: EventStream
func (h *EventStreamHandler) EventStream(w http.ResponseWriter, req *http.Request) {
logger := h.logger.Session("event-stream-handler")
closeNotifier := w.(http.CloseNotifier).CloseNotify()
sourceChan := make(chan events.EventSource)
flusher := w.(http.Flusher)
go func() {
source, err := h.bbs.SubscribeToEvents()
if err != nil {
logger.Error("failed-to-subscribe-to-events", err)
close(sourceChan)
return
}
sourceChan <- source
}()
var source events.EventSource
select {
case source = <-sourceChan:
if source == nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
case <-closeNotifier:
return
}
defer source.Close()
go func() {
<-closeNotifier
source.Close()
}()
w.Header().Add("Content-Type", "text/event-stream; charset=utf-8")
w.Header().Add("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Add("Connection", "keep-alive")
w.WriteHeader(http.StatusOK)
flusher.Flush()
eventID := 0
for {
bbsEvent, err := source.Next()
if err != nil {
logger.Error("failed-to-get-next-event", err)
return
}
event, err := NewEventFromBBS(bbsEvent)
if err != nil {
logger.Error("failed-to-marshal-event", err)
return
}
payload, err := json.Marshal(event)
if err != nil {
logger.Error("failed-to-marshal-event", err)
return
}
err = sse.Event{
ID: strconv.Itoa(eventID),
Name: string(event.EventType()),
Data: payload,
}.Write(w)
if err != nil {
break
}
flusher.Flush()
eventID++
}
}