Refactor bots allocation USD value calculation in AgentService and AgentGrain
- Updated the calculation of bots allocation USD value to directly sum BotTradingBalance from Bot entities, eliminating the need for additional service calls to fetch bot configurations. - This change aims to prevent potential deadlocks and improve performance by reducing unnecessary asynchronous calls.
This commit is contained in:
@@ -321,11 +321,9 @@ public class AgentService : IAgentService
|
||||
var botsAllocationUsdValue = 0m;
|
||||
if (activeStrategies.Any())
|
||||
{
|
||||
var botIds = activeStrategies.Select(b => b.Identifier);
|
||||
var botConfigs = await ServiceScopeHelpers.WithScopedService<IBotService, IEnumerable<TradingBotConfig>>(
|
||||
_serviceScopeFactory,
|
||||
async botService => await botService.GetBotConfigsByIdsAsync(botIds));
|
||||
botsAllocationUsdValue = botConfigs.Sum(config => config.BotTradingBalance);
|
||||
// Calculate bots allocation USD value directly from Bot entities (avoid calling grains to prevent deadlocks)
|
||||
// Bot entities already contain BotTradingBalance from the database
|
||||
botsAllocationUsdValue = activeStrategies.Sum(bot => bot.BotTradingBalance);
|
||||
}
|
||||
|
||||
var freshBalance = new AgentBalance
|
||||
|
||||
@@ -261,13 +261,9 @@ public class AgentGrain : Grain, IAgentGrain
|
||||
{
|
||||
runtime = activeStrategies.Min(p => p.StartupTime);
|
||||
|
||||
// Calculate bots allocation USD value from bot configurations
|
||||
var botIds = activeStrategies.Select(b => b.Identifier);
|
||||
var botConfigs =
|
||||
await ServiceScopeHelpers.WithScopedService<IBotService, IEnumerable<TradingBotConfig>>(
|
||||
_scopeFactory,
|
||||
async (botService) => { return await botService.GetBotConfigsByIdsAsync(botIds); });
|
||||
botsAllocationUsdValue = botConfigs.Sum(config => config.BotTradingBalance);
|
||||
// Calculate bots allocation USD value directly from Bot entities (avoid calling grains to prevent deadlocks)
|
||||
// Bot entities already contain BotTradingBalance from the database
|
||||
botsAllocationUsdValue = activeStrategies.Sum(bot => bot.BotTradingBalance);
|
||||
}
|
||||
|
||||
var summary = new AgentSummary
|
||||
|
||||
Reference in New Issue
Block a user