fix: Replace all DateTime.Now/UtcNow with IClock injection (AGENTS.md v16.0)
Resolves architecture test violations: - Removed all direct DateTime.UtcNow calls - Injected IClock into 7 service classes - Added TestClock implementation for tests - Updated all test constructors with fixture.Clock() - Fixed MetricsSql comment to avoid false SELECT * detection Services updated (IClock injection): - MetricsSql.cs (BuildingBlocks) - CircuitBreakerPolicyFactory.cs - KisConnectionPool.cs - RateLimiterService.cs - MetricsPolicy.cs - OpenDartDailyBatchJob.cs - OpenDartService.cs Tests updated: - DatabaseFixture.cs (added Clock() method + TestClock impl) - CircuitBreakerTests, ObservabilityMetricsTests, OpenDartServiceTests, RateLimiterServiceTests (added fixture.Clock() to constructors) Result: 95/95 integration tests PASS, DateTime violations 100% resolved Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Dapper;
|
||||
using KArtSell.BuildingBlocks.Time;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Npgsql;
|
||||
@@ -13,6 +14,7 @@ namespace KArtSell.Host.Infrastructure;
|
||||
public class RateLimiterService
|
||||
{
|
||||
private readonly NpgsqlDataSource _dataSource;
|
||||
private readonly IClock _clock;
|
||||
private readonly ILogger<RateLimiterService> _logger;
|
||||
|
||||
private static readonly Dictionary<string, RateLimitConfig> ApiConfigs = new()
|
||||
@@ -34,9 +36,10 @@ public class RateLimiterService
|
||||
new EventId(2, nameof(LogQuotaExceeded)),
|
||||
"Rate limit: {ApiName} quota exceeded, retry after {RetryAfter}s");
|
||||
|
||||
public RateLimiterService(NpgsqlDataSource dataSource, ILogger<RateLimiterService> logger)
|
||||
public RateLimiterService(NpgsqlDataSource dataSource, IClock clock, ILogger<RateLimiterService> logger)
|
||||
{
|
||||
_dataSource = dataSource;
|
||||
_clock = clock;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -66,7 +69,7 @@ public class RateLimiterService
|
||||
await using var connection = await _dataSource.OpenConnectionAsync(cancellationToken);
|
||||
var result = await connection.QueryFirstOrDefaultAsync<(int CurrentTokens, int WindowSeconds, int LimitCount)?>(
|
||||
sql,
|
||||
new { apiName = apiName.ToLower(), now = DateTime.UtcNow },
|
||||
new { apiName = apiName.ToLower(), now = _clock.UtcNow.UtcDateTime },
|
||||
commandTimeout: 5);
|
||||
|
||||
if (result == null)
|
||||
@@ -106,7 +109,7 @@ public class RateLimiterService
|
||||
await using var connection = await _dataSource.OpenConnectionAsync(cancellationToken);
|
||||
await connection.ExecuteAsync(
|
||||
sql,
|
||||
new { apiName = apiName.ToLower(), limit = config.Limit, now = DateTime.UtcNow },
|
||||
new { apiName = apiName.ToLower(), limit = config.Limit, now = _clock.UtcNow.UtcDateTime },
|
||||
commandTimeout: 5);
|
||||
|
||||
_logger.LogInformation("Rate limit quota reset for {ApiName}: {Limit}/{WindowSeconds}s", apiName, config.Limit, config.WindowSeconds);
|
||||
@@ -129,7 +132,7 @@ public class RateLimiterService
|
||||
{
|
||||
await connection.ExecuteAsync(
|
||||
sql,
|
||||
new { apiName, limit = config.Limit, window = config.WindowSeconds, now = DateTime.UtcNow },
|
||||
new { apiName, limit = config.Limit, window = config.WindowSeconds, now = _clock.UtcNow.UtcDateTime },
|
||||
commandTimeout: 5);
|
||||
}
|
||||
|
||||
@@ -148,7 +151,7 @@ public class RateLimiterService
|
||||
await using var connection = await _dataSource.OpenConnectionAsync(cancellationToken);
|
||||
await connection.ExecuteAsync(
|
||||
sql,
|
||||
new { apiName = apiName.ToLower(), action, now = DateTime.UtcNow },
|
||||
new { apiName = apiName.ToLower(), action, now = _clock.UtcNow.UtcDateTime },
|
||||
commandTimeout: 5);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user