From 2fb700aaabda57103776297c6cac913d33f95136 Mon Sep 17 00:00:00 2001 From: Adam Bozanich Date: Tue, 25 Apr 2017 16:45:51 -0700 Subject: [PATCH] ui: drain events --- ui/processor.go | 10 +++++++--- ui/tuiwriter.go | 41 ++++++++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/ui/processor.go b/ui/processor.go index 952fc7d..94a4c46 100644 --- a/ui/processor.go +++ b/ui/processor.go @@ -34,6 +34,10 @@ const ( ceventResult ceventId = "action-result" ) +const ( + pBufSiz = 15 +) + type cevent struct { id ceventId containerId string @@ -66,8 +70,8 @@ func newProcessor(w writer) *processor { pools: make(map[string]*pool), containers: make(map[string]*container), - poolch: make(chan pevent), - containerch: make(chan cevent), + poolch: make(chan pevent, pBufSiz), + containerch: make(chan cevent, pBufSiz), donech: make(chan bool), } @@ -78,7 +82,7 @@ func newProcessor(w writer) *processor { } func (p *processor) stop() { - close(p.donech) + defer close(p.donech) p.writer.stop() } diff --git a/ui/tuiwriter.go b/ui/tuiwriter.go index 3b2e242..54192ab 100644 --- a/ui/tuiwriter.go +++ b/ui/tuiwriter.go @@ -13,6 +13,8 @@ import ( const ( tuiMaxPeriod = time.Second / 2 tuiMinPeriod = time.Second / 15 + + tuiCHBufsiz = 15 ) type crec struct { @@ -39,8 +41,12 @@ type tuiWriter struct { // closed when user quits. shutdownch chan bool + // closed when completely shut down donech chan bool + // signal closing down + drainch chan bool + app *views.Application window *tuiWindow } @@ -55,14 +61,16 @@ func newTUIWriter(shutdownch chan bool) (writer, error) { pupdate: make(map[string]tuiTR), pdelete: make(map[string]tuiTR), - pch: make(chan pool), - cch: make(chan container), - dch: make(chan container), + pch: make(chan pool, tuiCHBufsiz), + cch: make(chan container, tuiCHBufsiz), + dch: make(chan container, tuiCHBufsiz), drawch: make(chan bool), shutdownch: shutdownch, - donech: make(chan bool), + + donech: make(chan bool), + drainch: make(chan bool), app: app, window: window, @@ -117,18 +125,32 @@ func (w *tuiWriter) deleteContainer(c container) { } func (w *tuiWriter) stop() { - w.throttle.Stop() - close(w.donech) + w.drainch <- true + <-w.donech } func (w *tuiWriter) run() { + defer close(w.donech) defer w.app.Quit() + defer w.throttle.Stop() + + shuttingdown := 0 + for { select { - case <-w.donech: - return + case <-w.drainch: + shuttingdown++ case <-w.drawch: - w.draw() + switch { + case shuttingdown > 2: + return + case shuttingdown > 0: + w.throttle.Trigger() + shuttingdown++ + fallthrough + case shuttingdown == 0: + w.draw() + } case p := <-w.pch: w.handlePool(p) w.throttle.Trigger() @@ -139,6 +161,7 @@ func (w *tuiWriter) run() { w.handleDeleteContainer(c) w.throttle.Trigger() case <-time.After(tuiMaxPeriod): + w.throttle.Trigger() } } }