grid2: add mutex lock for the grid object field

This commit is contained in:
c9s 2023-02-07 01:38:25 +08:00
parent 06c3f5f79c
commit e8c69dfaef
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -140,6 +140,9 @@ type Strategy struct {
gridProfitCallbacks []func(stats *GridProfitStats, profit *GridProfit) gridProfitCallbacks []func(stats *GridProfitStats, profit *GridProfit)
gridClosedCallbacks []func() gridClosedCallbacks []func()
gridErrorCallbacks []func(err error) 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 { 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. // 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 { func (s *Strategy) openGrid(ctx context.Context, session *bbgo.ExchangeSession) error {
// grid object guard // grid object guard
s.mu.Lock()
defer s.mu.Unlock()
if s.grid != nil { if s.grid != nil {
return 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.logger.Infof("GRID RECOVER: found %d filled grid orders", len(filledOrders))
s.mu.Lock()
s.grid = grid s.grid = grid
s.mu.Unlock()
for _, o := range filledOrders { for _, o := range filledOrders {
s.processFilledOrder(o) s.processFilledOrder(o)
} }