fix(client-dotnet): keepalive, error mapping, lifecycle, CLI batch/redaction (CDN-001..011, #257)
- keepalive: 30 s ping / 10 s timeout / Always, configurable via KeepAlivePingDelay/Timeout (CDN-004, D7) - GrpcStatusMapper shared by both transports: server Cancelled with a live token is no longer OperationCanceledException; NotFound/FailedPrecondition on session RPCs -> MxGatewaySessionException; MxGatewayException.GrpcStatusCode preserves the status (CDN-001, CDN-002) - Register/AddItem/AddItem2 raise MxGatewayMalformedReplyException on a payload-less reply (CDN-008) - MxGatewayEventStream.DisposeAsync waits for the in-flight read (CDN-003); session DisposeAsync is bounded to 5 s and swallows cancellation (CDN-005); owned handler + pinned CA disposed (CDN-006) - ResourceExhausted no longer retried (CDN-007) - CLI: batch reuses clients, stream/bench commands exempt from the 30 s timeout, OCE reported as an error with exit 1, --value redacted only for write-secured* and only as a whole token, bench backs off on failures (CDN-009, CDN-010, CDN-011)
This commit is contained in:
@@ -5,7 +5,6 @@ using Microsoft.Extensions.Logging;
|
||||
using ZB.MOM.WW.MxGateway.Contracts.Proto.Galaxy;
|
||||
using Polly;
|
||||
using System.Net.Http;
|
||||
using System.Net.Security;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
@@ -25,6 +24,7 @@ public sealed class GalaxyRepositoryClient : IAsyncDisposable
|
||||
private readonly bool _ownsChannel;
|
||||
private readonly IGalaxyRepositoryClientTransport _transport;
|
||||
private readonly ResiliencePipeline _safeUnaryRetryPipeline;
|
||||
private readonly X509Certificate2? _ownedCertificate;
|
||||
private bool _disposed;
|
||||
|
||||
/// <summary>
|
||||
@@ -50,10 +50,12 @@ public sealed class GalaxyRepositoryClient : IAsyncDisposable
|
||||
private GalaxyRepositoryClient(
|
||||
GrpcChannel channel,
|
||||
IGalaxyRepositoryClientTransport transport,
|
||||
bool ownsChannel)
|
||||
bool ownsChannel,
|
||||
X509Certificate2? ownedCertificate = null)
|
||||
{
|
||||
_channel = channel;
|
||||
_ownsChannel = ownsChannel;
|
||||
_ownedCertificate = ownedCertificate;
|
||||
_transport = transport;
|
||||
Options = transport.Options;
|
||||
_safeUnaryRetryPipeline = MxGatewayClientRetryPolicy.Create(
|
||||
@@ -83,12 +85,15 @@ public sealed class GalaxyRepositoryClient : IAsyncDisposable
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
options.Validate();
|
||||
|
||||
HttpMessageHandler handler = CreateHttpHandler(options);
|
||||
SocketsHttpHandler handler = GatewayHttpHandlerFactory.Create(options, out X509Certificate2? trustedRoot);
|
||||
var channel = GrpcChannel.ForAddress(
|
||||
options.Endpoint,
|
||||
new GrpcChannelOptions
|
||||
{
|
||||
HttpHandler = handler,
|
||||
// The channel owns this handler (CDN-006): disposing the channel must also close
|
||||
// its connection pool, otherwise every recreated client leaks a handler.
|
||||
DisposeHttpClient = true,
|
||||
LoggerFactory = options.LoggerFactory,
|
||||
MaxReceiveMessageSize = options.MaxGrpcMessageBytes,
|
||||
MaxSendMessageSize = options.MaxGrpcMessageBytes,
|
||||
@@ -99,7 +104,8 @@ public sealed class GalaxyRepositoryClient : IAsyncDisposable
|
||||
new GrpcGalaxyRepositoryClientTransport(
|
||||
options,
|
||||
new GalaxyRepository.GalaxyRepositoryClient(channel)),
|
||||
ownsChannel: true);
|
||||
ownsChannel: true,
|
||||
ownedCertificate: trustedRoot);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -467,6 +473,7 @@ public sealed class GalaxyRepositoryClient : IAsyncDisposable
|
||||
if (_ownsChannel)
|
||||
{
|
||||
_channel?.Dispose();
|
||||
_ownedCertificate?.Dispose();
|
||||
}
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
@@ -529,60 +536,13 @@ public sealed class GalaxyRepositoryClient : IAsyncDisposable
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static HttpMessageHandler CreateHttpHandler(MxGatewayClientOptions options) =>
|
||||
CreateHttpHandlerForTests(options);
|
||||
|
||||
/// <summary>Creates the <see cref="SocketsHttpHandler"/> used for gRPC calls, exposed for test configuration.</summary>
|
||||
/// <param name="options">The client options supplying TLS and timeout settings.</param>
|
||||
/// <returns>The configured HTTP handler.</returns>
|
||||
internal static SocketsHttpHandler CreateHttpHandlerForTests(MxGatewayClientOptions options)
|
||||
{
|
||||
SocketsHttpHandler handler = new()
|
||||
{
|
||||
ConnectTimeout = options.ConnectTimeout,
|
||||
};
|
||||
|
||||
if (options.UseTls)
|
||||
{
|
||||
handler.SslOptions = new SslClientAuthenticationOptions();
|
||||
if (!string.IsNullOrWhiteSpace(options.ServerNameOverride))
|
||||
{
|
||||
handler.SslOptions.TargetHost = options.ServerNameOverride;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(options.CaCertificatePath))
|
||||
{
|
||||
X509Certificate2 trustedRoot = X509CertificateLoader.LoadCertificateFromFile(options.CaCertificatePath);
|
||||
handler.SslOptions.RemoteCertificateValidationCallback = (_, certificate, chain, errors) =>
|
||||
{
|
||||
if ((errors & System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch) != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (certificate is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
using X509Chain customChain = new();
|
||||
customChain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
|
||||
customChain.ChainPolicy.CustomTrustStore.Add(trustedRoot);
|
||||
customChain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
|
||||
customChain.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;
|
||||
X509Certificate2 certificateToValidate = certificate as X509Certificate2
|
||||
?? X509CertificateLoader.LoadCertificate(certificate.Export(X509ContentType.Cert));
|
||||
return customChain.Build(certificateToValidate);
|
||||
};
|
||||
}
|
||||
else if (!options.RequireCertificateValidation)
|
||||
{
|
||||
handler.SslOptions.RemoteCertificateValidationCallback = (_, _, _, _) => true;
|
||||
}
|
||||
}
|
||||
|
||||
return handler;
|
||||
}
|
||||
/// <summary>
|
||||
/// Creates the <see cref="SocketsHttpHandler"/> used for gRPC calls; exposed internally so tests can inspect it.
|
||||
/// </summary>
|
||||
/// <param name="options">The client options that configure connect timeout, keepalive, and TLS behavior.</param>
|
||||
/// <returns>A configured <see cref="SocketsHttpHandler"/>.</returns>
|
||||
internal static SocketsHttpHandler CreateHttpHandlerForTests(MxGatewayClientOptions options) =>
|
||||
GatewayHttpHandlerFactory.Create(options, out _);
|
||||
|
||||
private void ThrowIfDisposed()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user