signals items change with closed channel

This commit is contained in:
Fabricio 2018-12-02 12:17:23 -02:00
parent b336d33dd3
commit a1d03ff264
2 changed files with 13 additions and 11 deletions

View file

@ -14,8 +14,7 @@ type CaptureList struct {
items []Capture
mux sync.Mutex
maxItems int
// signals any change in "items"
Updated chan struct{}
updated chan struct{} // signals any change in "items"
}
// Capture saves our traffic data
@ -54,7 +53,7 @@ func (c *Capture) Metadata() CaptureMetadata {
func NewCaptureList(maxItems int) *CaptureList {
return &CaptureList{
maxItems: maxItems,
Updated: make(chan struct{}),
updated: make(chan struct{}),
}
}
@ -67,7 +66,7 @@ func (c *CaptureList) Insert(capture Capture) {
if len(c.items) > c.maxItems {
c.items = c.items[1:]
}
c.signalsItemsChange()
c.signalsChange()
}
// Find finds a capture by its id
@ -88,7 +87,7 @@ func (c *CaptureList) RemoveAll() {
c.mux.Lock()
defer c.mux.Unlock()
c.items = nil
c.signalsItemsChange()
c.signalsChange()
}
// Items returns all the captures
@ -112,9 +111,12 @@ func newID() int {
return captureID
}
func (c *CaptureList) signalsItemsChange() {
select {
case c.Updated <- struct{}{}:
default:
}
func (c *CaptureList) signalsChange() {
close(c.updated)
c.updated = make(chan struct{})
}
// Updated signals any change in the list
func (c *CaptureList) Updated() <-chan struct{} {
return c.updated
}

View file

@ -68,7 +68,7 @@ func NewDashboardConnHandler(list *CaptureList) http.HandlerFunc {
rw.(http.Flusher).Flush()
select {
case <-list.Updated:
case <-list.Updated():
case <-req.Context().Done():
return
}