Skip to the content.

Task 78 — IHttpClientFactory adoption and standard resilience handlers

Phase: 5 Initiative: 45 Status: done Owner: — Slug: httpclient-factory-adoption

Goal

HttpClient создаётся per-call в 8 местах → socket exhaustion под нагрузкой (TIME_WAIT linger). Дополнительно long-lived new HttpClient хранится в singleton-полях без DNS refresh. Mesh layer уже использует IHttpClientFactory правильно (MeshServiceExtensions.cs:112,173,349), но CLI Program.cs не регистрирует AddHttpClient для tools/detection. Нет Polly resilience handlers на HttpClient-level (AddStandardResilienceHandler).

Точки исправления:

Acceptance criteria

Sub-tasks

Scope / Likely files

src/agent/Program.cs, src/agent/Hercules.WebApi/Program.cs, src/agent/Degradation/DegradationManager.cs, src/agent/LLM/Providers/LMStudioClient.cs, src/agent/LLM/ProviderHealthChecker.cs, src/agent/LLM/ProviderCapabilityDetector.cs, src/agent/LLM/LlmClientFactory.cs, src/agent/Tools/HttpTool.cs, src/agent/Tools/A2AClient.cs, src/agent/Mesh/Transport/HttpTransportAdapter.cs, src/agent/Mesh/Transport/GrpcTransportAdapter.cs, src/agent/Mesh/IntentTransport.cs, src/agent/Offline/NetworkMonitor.cs, src/agent/Degradation/OperatorNotificationService.cs, src/agent/Skills/SkillMarketplace.cs, src/agent/Config/AppConfig.cs, src/agent/Hercules.csproj, src/agent/appsettings.json, tests/Hercules.Agent.Tests/Http/HttpClientFactoryTests.cs (new)

Dependencies

Risks / Rollback

IHttpClientFactory требует DI — CLI Program.cs должен использовать Host builder (уже использует). AddStandardResilienceHandler добавляет latency на retry; настроить timeout чтобы не превышать deadline. Rollback: вернуть new HttpClient (но socket exhaustion останется).

Implementation notes

src/agent/Hercules.csproj — добавлен Microsoft.Extensions.Http.Resilience 10.0.0.

src/agent/Config/AppConfig.cs — добавлена HttpResilienceConfig (TimeoutSeconds, RetryCount, RetryBaseDelayMs, CircuitBreakerFailureRatio/SamplingDuration/MinimumThroughput/BreakDuration) + property HttpConfig.Resilience со default = new HttpResilienceConfig().

src/agent/appsettings.json — добавлена секция Http.Resilience с дефолтами (TimeoutSeconds=30, RetryCount=3, CircuitBreakerFailureRatio=0.5 и т.д.).

Named-client constants (все уникальные, проверяется тестом NamedClientNames_AreUnique):

Refactored classes (все используют IHttpClientFactory? + legacy fallback):

src/agent/Program.cs (CLI) + src/agent/Hercules.WebApi/Program.cs:

tests/Hercules.Agent.Tests/Http/HttpClientFactoryTests.cs (new, 13 tests):

Validation

Commit