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