From 7c59e3ddc4633b55afa78f40c997ebe9c8721a19 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 20 Nov 2023 16:15:33 +0800 Subject: [PATCH] bbgo: add setAccount for account mutex protection --- pkg/bbgo/session.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index ba10b1e1e..bac3e0cf1 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -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)