From e8c69dfaef52de217a55600d1148b5970ea3e185 Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 7 Feb 2023 01:38:25 +0800 Subject: [PATCH] grid2: add mutex lock for the grid object field --- pkg/strategy/grid2/strategy.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/strategy/grid2/strategy.go b/pkg/strategy/grid2/strategy.go index 31aa942a5..52a4e1576 100644 --- a/pkg/strategy/grid2/strategy.go +++ b/pkg/strategy/grid2/strategy.go @@ -140,6 +140,9 @@ type Strategy struct { gridProfitCallbacks []func(stats *GridProfitStats, profit *GridProfit) gridClosedCallbacks []func() gridErrorCallbacks []func(err error) + + // mu is used for locking the grid object field, avoid double grid opening + mu sync.Mutex } func (s *Strategy) ID() string { @@ -764,6 +767,9 @@ func (s *Strategy) newGrid() *Grid { // 2) if baseInvestment, quoteInvestment is set, then we should calculate the quantity from the given base investment and quote investment. func (s *Strategy) openGrid(ctx context.Context, session *bbgo.ExchangeSession) error { // grid object guard + s.mu.Lock() + defer s.mu.Unlock() + if s.grid != nil { return nil } @@ -1151,7 +1157,10 @@ func (s *Strategy) recoverGrid(ctx context.Context, historyService types.Exchang s.logger.Infof("GRID RECOVER: found %d filled grid orders", len(filledOrders)) + s.mu.Lock() s.grid = grid + s.mu.Unlock() + for _, o := range filledOrders { s.processFilledOrder(o) }