Filter stream events before queueing them
This commit is contained in:
@@ -8,18 +8,18 @@ import (
|
|||||||
|
|
||||||
type hub struct {
|
type hub struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
subs map[chan api.Event]bool
|
subs map[chan api.Event]func(api.Event) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHub() *hub {
|
func newHub() *hub {
|
||||||
return &hub{subs: map[chan api.Event]bool{}}
|
return &hub{subs: map[chan api.Event]func(api.Event) bool{}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *hub) subscribe() chan api.Event {
|
func (h *hub) subscribe(wanted func(api.Event) bool) chan api.Event {
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
defer h.mu.Unlock()
|
defer h.mu.Unlock()
|
||||||
ch := make(chan api.Event, 256)
|
ch := make(chan api.Event, 256)
|
||||||
h.subs[ch] = true
|
h.subs[ch] = wanted
|
||||||
return ch
|
return ch
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,7 +37,10 @@ func (h *hub) unsubscribe(ch chan api.Event) {
|
|||||||
func (h *hub) publish(ev api.Event) {
|
func (h *hub) publish(ev api.Event) {
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
defer h.mu.Unlock()
|
defer h.mu.Unlock()
|
||||||
for ch := range h.subs {
|
for ch, wanted := range h.subs {
|
||||||
|
if !wanted(ev) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
select {
|
select {
|
||||||
case ch <- ev:
|
case ch <- ev:
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ func (s *Server) StreamEvents(w http.ResponseWriter, r *http.Request, params api
|
|||||||
w.Header().Set("Cache-Control", "no-cache")
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
live := s.hub.subscribe()
|
live := s.hub.subscribe(wanted)
|
||||||
defer s.clientsChanged()
|
defer s.clientsChanged()
|
||||||
defer s.hub.unsubscribe(live)
|
defer s.hub.unsubscribe(live)
|
||||||
s.clientsChanged()
|
s.clientsChanged()
|
||||||
@@ -96,9 +96,6 @@ func (s *Server) StreamEvents(w http.ResponseWriter, r *http.Request, params api
|
|||||||
if ev.Seq != 0 && ev.Seq <= last {
|
if ev.Seq != 0 && ev.Seq <= last {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !wanted(ev) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !writeEvent(w, ev) {
|
if !writeEvent(w, ev) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user