Fix shutdown bug

This commit is contained in:
Kevin Cotugno 2018-01-01 21:14:26 -08:00
parent 7d07aa8e8f
commit 7a65a6f3fa
3 changed files with 27 additions and 25 deletions

View File

@ -100,9 +100,6 @@ func Init() *Terminal {
}
func (t *Terminal) Shutdown() {
t.shutdown <- struct{}{}
t.shutdown <- struct{}{}
t.doneLock.Lock()
t.done = true
t.doneLock.Unlock()
@ -113,9 +110,11 @@ func (t *Terminal) Shutdown() {
t.exitRaw()
t.exitAlt()
t.out.Close()
t.in.Close()
close(t.event)
t.shutdown <- struct{}{}
}
func (t *Terminal) Clear() {
@ -155,6 +154,11 @@ func (t *Terminal) setSize(s image.Point) {
t.display = NewBlock(0, 0, s.X, s.Y)
t.Clear()
select {
case t.sizeChange <- s:
default:
}
}
func (t *Terminal) WriteCells(cells []Cell) {
@ -271,7 +275,6 @@ func (t *Terminal) watchInput() {
go func() {
buf := make([]byte, 1)
Loop:
for {
_, err := t.in.Read(buf)
if err != nil {
@ -279,12 +282,12 @@ func (t *Terminal) watchInput() {
}
select {
case <-t.shutdown:
break Loop
case t.event <- Event(buf[0]):
default:
}
}
close(t.event)
}()
}
@ -301,10 +304,6 @@ func (t *Terminal) watchSize() {
case <-timer.C:
size := t.sizeInternal()
t.setSize(size)
select {
case t.sizeChange <- size:
default:
}
continue
}

View File

@ -155,7 +155,7 @@ func (o *OrderBook) watchBook() {
err = o.conn.WriteJSON(sub)
if err != nil {
o.err <- err
o.sendError(err)
return
}
@ -165,7 +165,7 @@ func (o *OrderBook) watchBook() {
err := o.conn.ReadJSON(&msg)
if err != nil {
if !websocket.IsCloseError(err, websocket.CloseNormalClosure) {
o.err <- err
o.sendError(err)
}
break
}
@ -192,7 +192,7 @@ func (o *OrderBook) watchBook() {
case "change":
o.change(msg)
default:
o.err <- errors.New("Unknown message type")
o.sendError(errors.New("Unknown message type"))
}
o.msg <- msg
@ -256,7 +256,7 @@ func (o *OrderBook) loadOrderBook() {
resp, err := http.Get(fmt.Sprintf("https://api.gdax.com/products/%v/book?level=3", coin))
if err != nil {
o.err <- err
o.sendError(err)
o.Shutdown()
return
}
@ -264,7 +264,7 @@ func (o *OrderBook) loadOrderBook() {
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
o.err <- err
o.sendError(err)
o.Shutdown()
return
}
@ -273,7 +273,7 @@ func (o *OrderBook) loadOrderBook() {
err = json.Unmarshal(buf, &parsed)
if err != nil {
o.err <- err
o.sendError(err)
o.Shutdown()
return
}
@ -283,13 +283,13 @@ func (o *OrderBook) loadOrderBook() {
e.Price, err = decimal.NewFromString(b[0])
if err != nil {
o.err <- err
o.sendError(err)
o.Shutdown()
return
}
e.Size, err = decimal.NewFromString(b[1])
if err != nil {
o.err <- err
o.sendError(err)
o.Shutdown()
return
}
@ -304,13 +304,13 @@ func (o *OrderBook) loadOrderBook() {
e.Price, err = decimal.NewFromString(a[0])
if err != nil {
o.err <- err
o.sendError(err)
o.Shutdown()
return
}
e.Size, err = decimal.NewFromString(a[1])
if err != nil {
o.err <- err
o.sendError(err)
o.Shutdown()
return
}
@ -440,3 +440,10 @@ func ReverseDecimalComparator(a, b interface{}) int {
return 0
}
}
func (o *OrderBook) sendError(err error) {
select {
case o.err <- err:
default:
}
}

View File

@ -77,10 +77,6 @@ func main() {
}
}()
go func() {
<-ob.Err
}()
go renderLoop(&scene, 100*time.Millisecond)
updateOrders("sell")