refactor: SqlSugarScope注册为Singleton实现线程安全,移除手动信号量限制

This commit is contained in:
OpenClaw Agent 2026-03-17 09:26:26 +00:00
parent d30c3076bd
commit 6b50d7fc50
2 changed files with 19 additions and 54 deletions

View File

@ -8,7 +8,9 @@ public static class DatabaseExtensions
{ {
public static IServiceCollection AddDatabase(this IServiceCollection services) public static IServiceCollection AddDatabase(this IServiceCollection services)
{ {
services.AddScoped<ISqlSugarClient>(s => // 使用 Singleton 注册 SqlSugarScope线程安全内部使用 AsyncLocal
// SqlSugarScope 设计上支持并发,会自动管理连接池
services.AddSingleton<ISqlSugarClient>(s =>
{ {
return SqlSugarConfig.GetSqlSugarClient(); return SqlSugarConfig.GetSqlSugarClient();
}); });

View File

@ -21,9 +21,6 @@ public class MarketDataService : IMarketDataService
private readonly IOkxMarketService _okxService; private readonly IOkxMarketService _okxService;
private readonly IMarketDataRepository _marketDataRepo; private readonly IMarketDataRepository _marketDataRepo;
// 限制数据库并发访问,避免 SqlSugar 连接状态冲突
private static readonly SemaphoreSlim _dbSemaphore = new(3, 3);
public MarketDataService( public MarketDataService(
ILogger<MarketDataService> logger, ILogger<MarketDataService> logger,
ITencentMarketService tencentService, ITencentMarketService tencentService,
@ -47,10 +44,6 @@ public class MarketDataService : IMarketDataService
{ {
_logger.LogInformation("获取实时价格: {Symbol}, 资产类型: {AssetType}", symbol, assetType); _logger.LogInformation("获取实时价格: {Symbol}, 资产类型: {AssetType}", symbol, assetType);
// 限制数据库并发访问
await _dbSemaphore.WaitAsync();
try
{
// 先查缓存 // 先查缓存
var cached = await _marketDataRepo.GetPriceCacheAsync(symbol, assetType); var cached = await _marketDataRepo.GetPriceCacheAsync(symbol, assetType);
@ -66,11 +59,6 @@ public class MarketDataService : IMarketDataService
AssetType = cached.AssetType AssetType = cached.AssetType
}; };
} }
}
finally
{
_dbSemaphore.Release();
}
// 缓存未命中调用API // 缓存未命中调用API
MarketPriceResponse response; MarketPriceResponse response;
@ -120,17 +108,8 @@ public class MarketDataService : IMarketDataService
_logger.LogInformation("获取历史数据: {Symbol}, 资产类型: {AssetType}, 周期: {Timeframe}, 数量: {Limit}", _logger.LogInformation("获取历史数据: {Symbol}, 资产类型: {AssetType}, 周期: {Timeframe}, 数量: {Limit}",
symbol, assetType, timeframe, limit); symbol, assetType, timeframe, limit);
// 限制数据库并发访问 // 先查缓存
await _dbSemaphore.WaitAsync(); var cachedKlines = await _marketDataRepo.GetKlineCacheAsync(symbol, assetType, timeframe, limit);
List<MarketKlineCache> cachedKlines;
try
{
cachedKlines = await _marketDataRepo.GetKlineCacheAsync(symbol, assetType, timeframe, limit);
}
finally
{
_dbSemaphore.Release();
}
// 缓存足够,直接返回 // 缓存足够,直接返回
if (cachedKlines.Count >= limit) if (cachedKlines.Count >= limit)
@ -235,15 +214,7 @@ public class MarketDataService : IMarketDataService
ExpiredAt = GetCacheExpirationTime(assetType) ExpiredAt = GetCacheExpirationTime(assetType)
}; };
await _dbSemaphore.WaitAsync();
try
{
await _marketDataRepo.SavePriceCacheAsync(cacheEntity); await _marketDataRepo.SavePriceCacheAsync(cacheEntity);
}
finally
{
_dbSemaphore.Release();
}
_logger.LogDebug("缓存写入: {Symbol} {AssetType}, 过期时间: {ExpiredAt}", symbol, assetType, cacheEntity.ExpiredAt); _logger.LogDebug("缓存写入: {Symbol} {AssetType}, 过期时间: {ExpiredAt}", symbol, assetType, cacheEntity.ExpiredAt);
} }
@ -264,15 +235,7 @@ public class MarketDataService : IMarketDataService
FetchedAt = DateTime.Now FetchedAt = DateTime.Now
}).ToList(); }).ToList();
await _dbSemaphore.WaitAsync();
try
{
await _marketDataRepo.SaveKlineCacheBatchAsync(cacheEntities); await _marketDataRepo.SaveKlineCacheBatchAsync(cacheEntities);
}
finally
{
_dbSemaphore.Release();
}
_logger.LogDebug("历史K线缓存写入: {Symbol} {AssetType} {Timeframe}, 数量: {Count}", symbol, assetType, timeframe, cacheEntities.Count); _logger.LogDebug("历史K线缓存写入: {Symbol} {AssetType} {Timeframe}, 数量: {Count}", symbol, assetType, timeframe, cacheEntities.Count);
} }