mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
grid2: add mutex lock for the grid object field
This commit is contained in:
parent
06c3f5f79c
commit
e8c69dfaef
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user