31e16fbc80
- 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)
84 lines
3.0 KiB
C#
84 lines
3.0 KiB
C#
using ZB.MOM.WW.MxGateway.Client;
|
|
|
|
namespace ZB.MOM.WW.MxGateway.Client.Cli;
|
|
|
|
/// <summary>
|
|
/// Reuses one CLI client per distinct connection configuration across batch-mode lines so each
|
|
/// line does not pay a fresh TCP/TLS/HTTP/2 handshake. Disposing the cache disposes every client.
|
|
/// </summary>
|
|
internal sealed class CliClientCache : IAsyncDisposable
|
|
{
|
|
private readonly Dictionary<string, IMxGatewayCliClient> _clients = [];
|
|
|
|
/// <summary>Gets the cached client for the options' connection settings, creating it on first use.</summary>
|
|
/// <param name="options">The connection options for this command.</param>
|
|
/// <param name="factory">Creates a client when none is cached.</param>
|
|
/// <returns>A lease that does not dispose the shared client.</returns>
|
|
public Lease Acquire(
|
|
MxGatewayClientOptions options,
|
|
Func<MxGatewayClientOptions, IMxGatewayCliClient> factory)
|
|
{
|
|
string key = string.Join(
|
|
'\n',
|
|
options.Endpoint,
|
|
options.ApiKey,
|
|
options.UseTls,
|
|
options.CaCertificatePath,
|
|
options.ServerNameOverride,
|
|
options.RequireCertificateValidation,
|
|
options.ConnectTimeout,
|
|
options.DefaultCallTimeout,
|
|
options.StreamTimeout,
|
|
options.KeepAlivePingDelay,
|
|
options.KeepAlivePingTimeout);
|
|
|
|
if (!_clients.TryGetValue(key, out IMxGatewayCliClient? client))
|
|
{
|
|
client = factory(options);
|
|
_clients[key] = client;
|
|
}
|
|
|
|
return Lease.Shared(client);
|
|
}
|
|
|
|
/// <summary>Disposes every cached client.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
foreach (IMxGatewayCliClient client in _clients.Values)
|
|
{
|
|
await client.DisposeAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
_clients.Clear();
|
|
}
|
|
|
|
/// <summary>A client handed to one command; disposes it only when the command owns it.</summary>
|
|
internal sealed class Lease : IAsyncDisposable
|
|
{
|
|
private readonly bool _owned;
|
|
|
|
private Lease(IMxGatewayCliClient client, bool owned)
|
|
{
|
|
Client = client;
|
|
_owned = owned;
|
|
}
|
|
|
|
/// <summary>Gets the leased client.</summary>
|
|
public IMxGatewayCliClient Client { get; }
|
|
|
|
/// <summary>Wraps a client the command created and must dispose.</summary>
|
|
/// <param name="client">The client.</param>
|
|
/// <returns>The lease.</returns>
|
|
public static Lease Owned(IMxGatewayCliClient client) => new(client, owned: true);
|
|
|
|
/// <summary>Wraps a cache-owned client.</summary>
|
|
/// <param name="client">The client.</param>
|
|
/// <returns>The lease.</returns>
|
|
public static Lease Shared(IMxGatewayCliClient client) => new(client, owned: false);
|
|
|
|
/// <inheritdoc />
|
|
public ValueTask DisposeAsync() => _owned ? Client.DisposeAsync() : ValueTask.CompletedTask;
|
|
}
|
|
}
|