Skip to content

Commit

Permalink
ui: drain events
Browse files Browse the repository at this point in the history
  • Loading branch information
boz committed Apr 25, 2017
1 parent 887af7f commit 2fb700a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
10 changes: 7 additions & 3 deletions ui/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ const (
ceventResult ceventId = "action-result"
)

const (
pBufSiz = 15
)

type cevent struct {
id ceventId
containerId string
Expand Down Expand Up @@ -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),
}

Expand All @@ -78,7 +82,7 @@ func newProcessor(w writer) *processor {
}

func (p *processor) stop() {
close(p.donech)
defer close(p.donech)
p.writer.stop()
}

Expand Down
41 changes: 32 additions & 9 deletions ui/tuiwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
const (
tuiMaxPeriod = time.Second / 2
tuiMinPeriod = time.Second / 15

tuiCHBufsiz = 15
)

type crec struct {
Expand All @@ -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
}
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -139,6 +161,7 @@ func (w *tuiWriter) run() {
w.handleDeleteContainer(c)
w.throttle.Trigger()
case <-time.After(tuiMaxPeriod):
w.throttle.Trigger()
}
}
}
Expand Down

0 comments on commit 2fb700a

Please sign in to comment.