From 63dc2e8d558fca448f99ed59751be10bc5a3f102 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 13 Jul 2020 13:31:40 +0800 Subject: [PATCH] fix graceful shutdown --- bbgo/trader.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/bbgo/trader.go b/bbgo/trader.go index b75b975b2..59ba2068b 100644 --- a/bbgo/trader.go +++ b/bbgo/trader.go @@ -26,16 +26,16 @@ type Strategy interface { Init(trader *Trader, stream *binance.PrivateStream) error } -func (t *Trader) RunStrategy(ctx context.Context, strategy Strategy) error { +func (t *Trader) RunStrategy(ctx context.Context, strategy Strategy) (chan struct{}, error) { symbol := t.Context.Symbol stream, err := t.Exchange.NewPrivateStream() if err != nil { - return err + return nil, err } if err := strategy.Init(t, stream); err != nil { - return err + return nil, err } t.reportTimer = time.AfterFunc(1*time.Second, func() { @@ -65,10 +65,13 @@ func (t *Trader) RunStrategy(ctx context.Context, strategy Strategy) error { var eventC = make(chan interface{}, 20) if err := stream.Connect(ctx, eventC); err != nil { - return err + return nil, err } + done := make(chan struct{}) + go func() { + defer close(done) defer stream.Close() for { @@ -84,7 +87,7 @@ func (t *Trader) RunStrategy(ctx context.Context, strategy Strategy) error { } }() - return nil + return done, nil } func (t *Trader) Infof(format string, args ...interface{}) {