fix(external-system-gateway): resolve ExternalSystemGateway-012,013,014 — failure logging, connection-limit wiring, test coverage; ExternalSystemGateway-011 flagged
This commit is contained in:
@@ -11,6 +11,10 @@ public static class ErrorClassifier
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines whether an HTTP status code represents a transient failure.
|
||||
/// Transient: HTTP 5xx, 408 (Request Timeout) and 429 (Too Many Requests).
|
||||
/// Every other non-success status (the remaining 4xx) defaults to permanent —
|
||||
/// a permanent failure is the safe default because retrying a 4xx is unlikely to
|
||||
/// succeed and risks duplicate side effects.
|
||||
/// </summary>
|
||||
public static bool IsTransient(HttpStatusCode statusCode)
|
||||
{
|
||||
|
||||
@@ -272,10 +272,21 @@ public class ExternalSystemClient : IExternalSystemClient
|
||||
|
||||
if (ErrorClassifier.IsTransient(response.StatusCode))
|
||||
{
|
||||
// Transient failures are normal operation (handled by retry / S&F) —
|
||||
// record at debug level only so the event log is not noisy.
|
||||
_logger.LogDebug(
|
||||
"Transient HTTP {StatusCode} from external system {System} calling {Method}.",
|
||||
(int)response.StatusCode, system.Name, method.Name);
|
||||
throw ErrorClassifier.AsTransient(
|
||||
$"HTTP {(int)response.StatusCode} from {system.Name}: {errorBody}");
|
||||
}
|
||||
|
||||
// The design requires permanent failures to be visible in Site Event
|
||||
// Logging — emit a warning so the gateway is not silent on a permanent
|
||||
// failure (ExternalSystemGateway-012).
|
||||
_logger.LogWarning(
|
||||
"Permanent HTTP {StatusCode} from external system {System} calling {Method}: {Error}",
|
||||
(int)response.StatusCode, system.Name, method.Name, errorBody);
|
||||
throw new PermanentExternalSystemException(
|
||||
$"HTTP {(int)response.StatusCode} from {system.Name}: {errorBody}",
|
||||
(int)response.StatusCode);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using ScadaLink.Commons.Interfaces.Services;
|
||||
|
||||
namespace ScadaLink.ExternalSystemGateway;
|
||||
@@ -11,6 +12,22 @@ public static class ServiceCollectionExtensions
|
||||
.BindConfiguration("ScadaLink:ExternalSystemGateway");
|
||||
|
||||
services.AddHttpClient();
|
||||
|
||||
// ExternalSystemGateway-013: wire MaxConcurrentConnectionsPerSystem into the
|
||||
// primary handler of every per-system named client ("ExternalSystem_{name}"),
|
||||
// so the option an operator configures actually bounds concurrent connections
|
||||
// instead of being silently ignored. ConfigureHttpClientDefaults applies to
|
||||
// the dynamically-named clients created by ExternalSystemClient.
|
||||
services.ConfigureHttpClientDefaults(builder =>
|
||||
builder.ConfigurePrimaryHttpMessageHandler(sp =>
|
||||
{
|
||||
var options = sp.GetRequiredService<IOptions<ExternalSystemGatewayOptions>>().Value;
|
||||
return new SocketsHttpHandler
|
||||
{
|
||||
MaxConnectionsPerServer = options.MaxConcurrentConnectionsPerSystem,
|
||||
};
|
||||
}));
|
||||
|
||||
services.AddScoped<ExternalSystemClient>();
|
||||
services.AddScoped<IExternalSystemClient>(sp => sp.GetRequiredService<ExternalSystemClient>());
|
||||
services.AddScoped<DatabaseGateway>();
|
||||
|
||||
Reference in New Issue
Block a user