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:
2026-01-06 17:39:01 +07:00
parent 520ec7dfaf
commit e0a064456a
2 changed files with 6 additions and 12 deletions

View File

@@ -321,11 +321,9 @@ public class AgentService : IAgentService
var botsAllocationUsdValue = 0m; var botsAllocationUsdValue = 0m;
if (activeStrategies.Any()) if (activeStrategies.Any())
{ {
var botIds = activeStrategies.Select(b => b.Identifier); // Calculate bots allocation USD value directly from Bot entities (avoid calling grains to prevent deadlocks)
var botConfigs = await ServiceScopeHelpers.WithScopedService<IBotService, IEnumerable<TradingBotConfig>>( // Bot entities already contain BotTradingBalance from the database
_serviceScopeFactory, botsAllocationUsdValue = activeStrategies.Sum(bot => bot.BotTradingBalance);
async botService => await botService.GetBotConfigsByIdsAsync(botIds));
botsAllocationUsdValue = botConfigs.Sum(config => config.BotTradingBalance);
} }
var freshBalance = new AgentBalance var freshBalance = new AgentBalance

View File

@@ -261,13 +261,9 @@ public class AgentGrain : Grain, IAgentGrain
{ {
runtime = activeStrategies.Min(p => p.StartupTime); runtime = activeStrategies.Min(p => p.StartupTime);
// Calculate bots allocation USD value from bot configurations // Calculate bots allocation USD value directly from Bot entities (avoid calling grains to prevent deadlocks)
var botIds = activeStrategies.Select(b => b.Identifier); // Bot entities already contain BotTradingBalance from the database
var botConfigs = botsAllocationUsdValue = activeStrategies.Sum(bot => bot.BotTradingBalance);
await ServiceScopeHelpers.WithScopedService<IBotService, IEnumerable<TradingBotConfig>>(
_scopeFactory,
async (botService) => { return await botService.GetBotConfigsByIdsAsync(botIds); });
botsAllocationUsdValue = botConfigs.Sum(config => config.BotTradingBalance);
} }
var summary = new AgentSummary var summary = new AgentSummary