bbgo: add setAccount for account mutex protection

This commit is contained in:
c9s 2023-11-20 16:15:33 +08:00
parent 3ea333fd52
commit 7c59e3ddc4
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -177,12 +177,16 @@ func (session *ExchangeSession) UpdateAccount(ctx context.Context) (*types.Accou
return nil, err
}
session.accountMutex.Lock()
session.Account = account
session.accountMutex.Unlock()
session.setAccount(account)
return account, nil
}
func (session *ExchangeSession) setAccount(a *types.Account) {
session.accountMutex.Lock()
session.Account = a
session.accountMutex.Unlock()
}
// Init initializes the basic data structure and market information by its exchange.
// Note that the subscribed symbols are not loaded in this stage.
func (session *ExchangeSession) Init(ctx context.Context, environ *Environment) error {
@ -268,13 +272,10 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
return err
}
session.accountMutex.Lock()
session.Account = account
session.accountMutex.Unlock()
session.setAccount(account)
logger.Infof("account %s balances:\n%s", session.Name, account.Balances().String())
}
logger.Infof("account %s balances:\n%s", session.Name, account.Balances().String())
// forward trade updates and order updates to the order executor
session.UserDataStream.OnTradeUpdate(session.OrderExecutor.EmitTradeUpdate)
session.UserDataStream.OnOrderUpdate(session.OrderExecutor.EmitOrderUpdate)