mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-19 07:02:16 +08:00
f358078b1c
# Description Introduces a central `IClientChannelManager` that owns client-side transport channels with reference counting, sharing, coalesced reconnect, and asynchronous participant notification. Sessions, discovery clients, and registration clients targeting the same `ConfiguredEndpoint` (with the same reverse-connect identity) now share a single underlying `ITransportChannel`; reconnect is transparent to callers; and the previous `AttachChannel`/`DetachChannel` + `SessionReconnectHandler` patterns are obsoleted. ## What lands ### Core types (`Stack/Opc.Ua.Core/Stack/Client/`) - `ManagedChannelKey`, `ChannelState`, `ChannelStateChange`, `ParticipantReconnectResult` - `IReconnectParticipant`, `IManagedTransportChannel`, `IClientChannelManager` - `IChannelReconnectPolicy` + `ExponentialBackoffChannelReconnectPolicy` (default mirrors historical `SessionReconnectHandler`: 500 ms → 30 s exponential) - `IRetryBudget` + `RetryBudget` — shared retry deadline so the two-level retry no longer compounds multiplicatively - `ClientChannelManager` — single non-partial sealed class with SOLID-extracted internal types under `Stack/Opc.Ua.Core/Stack/Client/Channels/Internal/`: `ChannelEntry` (behind `IChannelEntryHost`), `ManagedTransportChannelLease`, `ClientChannel`, `ClientChannelManagerMetrics` (IMeter), `ClientChannelManagerCertRotation` (auto `ReconnectAllAsync` on `CertificateManager` rotation), `ClientChannelManagerDiagnostics` (ActivitySource + EventSource). Refcount, coalesced reconnect, and transparent faulted-entry recovery via `SwapFaultedEntryAsync` (swaps a lease's underlying entry on subsequent `ReconnectAsync` when the prior cycle Faulted/Closed, preserves participant, back-off via policy `GetDelay(SwapCount)`) ### Three-state lifecycle gate `Disconnected` → `TransportConnecting` / `TransportReconnecting` → `TransportConnectedSessionReactivating` → `Ready` (or `Faulted` / `Closed`). Only `Ready` releases the service-call gate; the manager bypasses the gate internally for participant `ActivateSession` traffic during reactivation via an `AsyncLocal` scope. ### Session integration (`Libraries/Opc.Ua.Client/Session/`) - New `Session.CreateAsync(IClientChannelManager, …)` overload - `Session` implements `IReconnectParticipant`; reactivation handler distinguishes `Reactivated` / `RequiresSessionRecreate` / `TransientFailure` / `FatalForParticipant` / `FatalForChannel` - `Session.ReconnectAsync(ct)` automatically delegates to the channel manager when wired - `Session.RecreateInPlaceAsync` swaps managed-channel leases for failover-to-different-endpoint via a new atomic `IClientChannelManager.GetAsync(endpoint, participantFactory, …)` overload; same-key recreates delegate to manager reconnect; explicit-channel/connection callers retain legacy behavior - KA failure routes through `IClientChannelManager.ReconnectAsync(channel)` ### ManagedSession integration - `WithChannelManager(...)` builder + ctor param; DI-resolved automatically - Two-level retry: channel-mgr handles transparent reconnect; outer `ConnectionStateMachine` + `IReconnectPolicy` only triggers on terminal channel `Faulted`. Outer-state churn suppressed during channel-mgr cycles via `_channelReconnectInProgress` flag wired through `IManagedTransportChannel.StateChanged` - New `ManagedSession.ChannelStateChanged` event surfaces transparent reconnects - `ConnectionStateChangedEventArgs.UnderlyingChannelState` populated when outer reconnect was triggered by a channel fault - Shared `IRetryBudget` propagated so the layers enforce a single max-total-time ### Discovery / Registration / GDS Channel-manager-aware `CreateAsync` overloads on `DiscoveryClient`, `RegistrationClient`, `ServerPushConfigurationClient`, `LocalDiscoveryServerClient`, `GlobalDiscoveryServerClient`. Each registers a minimal `IReconnectParticipant`; co-located sessions and discovery probes targeting the same endpoint share a channel. ### HTTPS transport (`Stack/Opc.Ua.Bindings.Https/`) - New `IOpcUaHttpClientFactory` abstraction - HTTPS / OPC-HTTPS channels migrated to `IHttpClientFactory` + `Microsoft.Extensions.Http.Resilience` (10.6.0; AOT-compatible on net10 via source generators; legacy direct-HTTP path retained for older TFMs / no-DI consumers) ### DI `AddOpcUa().AddClient(...)` registers `IClientChannelManager` and the named HTTPS `HttpClient` + standard resilience handler automatically. `ManagedSessionBuilder` resolves the manager from DI when present. ### Obsoletions (functional, `[Obsolete]` with guidance) - `IClientBase.AttachChannel` / `DetachChannel` - `SessionReconnectHandler` - `SessionExtensions.ReconnectAsync(connection, ct)` and `(channel, ct)` legacy overloads ### Tests - Core: `ClientChannelManagerManagedTests`, `ClientChannelManagerCertRotationTests`, `RetryBudgetTests`, `OpcUaHttpClientFactoryTests` — **56 / 56 pass** - Client: `ManagedSession*Tests` updated; legacy `SessionReconnectHandlerTests` + `SessionExtensionsTests` preserved with `#pragma warning disable CS0618` — **129 / 129 pass** - Sessions integration: `ChannelManagerSharing` / `TransparentReconnect` / `SessionLifecycle` / `CertRotation` — **7 pass, 1 skipped** (`ChannelManagerExhaustionEscalatesAndRecoversWhenServerReturns` un-`[Ignore]`d and implemented in this PR via `SwapFaultedEntryAsync`; remaining skip is end-to-end `RequiresSessionRecreate` plumbing — carry-forward) - Stress (consolidated `Tests/Opc.Ua.Stress.Tests/Subscriptions/` + `Channels/`): chaos / contract / soak / lifecycle / leak-accuracy / gap / participant-result-aggregation tests merged from the previously-separate `Opc.Ua.Channels.Stress.Tests` project; nightly run via `.github/workflows/stress-test.yml` Builds clean (0 warnings / 0 errors) across all six TFMs: `net472`, `net48`, `netstandard2.1`, `net8.0`, `net9.0`, `net10.0`. ### Docs - `Docs/Sessions.md` new § 4 documents the channel manager, three-state gating, two-level retry with shared `IRetryBudget`, HTTPS resilience layering - `Docs/MigrationGuide.md` migration recipes for the four obsoleted API groups - `Docs/DependencyInjection.md` channel-manager and HTTPS factory registration ## Carry-forward (separate PRs) - End-to-end `ParticipantReconnectResult.RequiresSessionRecreate` → `Session.RecreateAsync` lifecycle wiring (currently incomplete — flagged in the 1 remaining skipped integration test) ## Related Issues - Fixes #3288 - Builds on #3282 (PR that introduced the initial `ITransportChannelManager` skeleton) ## Checklist - [x] I have signed the [CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf) and read the [CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md) doc. - [x] I have added tests that prove my fix is effective or that my feature works and increased code coverage. - [x] I have added all necessary documentation. - [x] I have verified that my changes do not introduce (new) build or analyzer warnings. - [ ] I ran **all** tests locally using the **UA.slnx** solution against at least .net **framework** and .net **10**, and all passed. *(Channel-manager-specific tests + ManagedSession/client tests verified across all 6 TFMs; full UA.slnx run is pending CI.)* - [ ] I fixed **all** failing and flaky tests in the CI pipelines and **all** CodeQL warnings. - [ ] I have addressed **all** PR feedback received.
750 lines
25 KiB
C#
750 lines
25 KiB
C#
/* ========================================================================
|
|
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
|
|
*
|
|
* OPC Foundation MIT License 1.00
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person
|
|
* obtaining a copy of this software and associated documentation
|
|
* files (the "Software"), to deal in the Software without
|
|
* restriction, including without limitation the rights to use,
|
|
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the
|
|
* Software is furnished to do so, subject to the following
|
|
* conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be
|
|
* included in all copies or substantial portions of the Software.
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
*
|
|
* The complete license agreement can be found here:
|
|
* http://opcfoundation.org/License/MIT/1.00/
|
|
* ======================================================================*/
|
|
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Opc.Ua.Client
|
|
{
|
|
/// <summary>
|
|
/// Callback used to execute a state-machine operation.
|
|
/// </summary>
|
|
internal delegate Task<ServiceResult> ConnectionStateOperation(CancellationToken ct);
|
|
|
|
/// <summary>
|
|
/// Callback used to execute a state-machine operation with a retry budget.
|
|
/// </summary>
|
|
internal delegate Task<ServiceResult> ConnectionStateBudgetOperation(IRetryBudget budget, CancellationToken ct);
|
|
|
|
/// <summary>
|
|
/// Callback used to close the current session.
|
|
/// </summary>
|
|
internal delegate Task ConnectionStateCloseOperation(CancellationToken ct);
|
|
|
|
/// <summary>
|
|
/// Manages the connection lifecycle state machine for ManagedSession.
|
|
/// Runs a background worker that handles connect, reconnect, and
|
|
/// failover based on the configured reconnect policy.
|
|
/// </summary>
|
|
internal sealed class ConnectionStateMachine : IAsyncDisposable
|
|
{
|
|
private volatile ConnectionState m_state = ConnectionState.Disconnected;
|
|
private readonly IReconnectPolicy m_reconnectPolicy;
|
|
private readonly ILogger m_logger;
|
|
private readonly TimeProvider m_timeProvider;
|
|
private readonly TimeSpan m_maxTotalReconnectTime;
|
|
private readonly CancellationTokenSource m_cts = new();
|
|
private readonly AsyncAutoResetEvent m_trigger = new(false);
|
|
private readonly AsyncManualResetEvent m_connected = new(false);
|
|
private readonly AsyncManualResetEvent m_closed = new(false);
|
|
private readonly Lock m_lock = new();
|
|
private IRetryBudget? m_reconnectBudget;
|
|
private Task? m_worker;
|
|
private int m_disposed;
|
|
|
|
/// <summary>
|
|
/// Delegate invoked to perform the actual session connect.
|
|
/// Returns a <see cref="ServiceResult"/> indicating success or
|
|
/// failure.
|
|
/// </summary>
|
|
internal ConnectionStateOperation? ConnectAsync { get; set; }
|
|
|
|
/// <summary>
|
|
/// Delegate invoked to perform a session reconnect (reactivate).
|
|
/// Returns a <see cref="ServiceResult"/> indicating success or
|
|
/// failure.
|
|
/// </summary>
|
|
internal ConnectionStateOperation? ReconnectAsync { get; set; }
|
|
|
|
/// <summary>
|
|
/// Delegate invoked to perform a session reconnect with a shared
|
|
/// retry budget.
|
|
/// </summary>
|
|
internal ConnectionStateBudgetOperation? ReconnectWithBudgetAsync { get; set; }
|
|
|
|
/// <summary>
|
|
/// Delegate invoked to attempt failover to a redundant server.
|
|
/// Returns a <see cref="ServiceResult"/> indicating success or
|
|
/// failure.
|
|
/// </summary>
|
|
internal ConnectionStateOperation? FailoverAsync { get; set; }
|
|
|
|
/// <summary>
|
|
/// Delegate invoked to attempt failover with a shared retry budget.
|
|
/// </summary>
|
|
internal ConnectionStateBudgetOperation? FailoverWithBudgetAsync { get; set; }
|
|
|
|
/// <summary>
|
|
/// Delegate invoked to close the session cleanly.
|
|
/// </summary>
|
|
internal ConnectionStateCloseOperation? CloseSessionAsync { get; set; }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the
|
|
/// <see cref="ConnectionStateMachine"/> class.
|
|
/// </summary>
|
|
/// <param name="reconnectPolicy">The reconnect policy that
|
|
/// controls backoff and retry limits.</param>
|
|
/// <param name="logger">Logger instance.</param>
|
|
/// <param name="maxTotalReconnectTime">Maximum total elapsed
|
|
/// time for one reconnect cycle.</param>
|
|
/// <param name="timeProvider">Optional <see cref="TimeProvider"/>
|
|
/// used for reconnect delay timing. Defaults to
|
|
/// <see cref="TimeProvider.System"/> when <c>null</c>.</param>
|
|
public ConnectionStateMachine(
|
|
IReconnectPolicy reconnectPolicy,
|
|
ILogger logger,
|
|
TimeSpan? maxTotalReconnectTime = null,
|
|
TimeProvider? timeProvider = null)
|
|
{
|
|
m_reconnectPolicy = reconnectPolicy
|
|
?? throw new ArgumentNullException(nameof(reconnectPolicy));
|
|
m_logger = logger
|
|
?? throw new ArgumentNullException(nameof(logger));
|
|
m_timeProvider = timeProvider ?? TimeProvider.System;
|
|
m_maxTotalReconnectTime = maxTotalReconnectTime
|
|
?? ReconnectPolicy.DefaultMaxTotalReconnectTime;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Current connection state.
|
|
/// </summary>
|
|
public ConnectionState State => m_state;
|
|
|
|
/// <summary>
|
|
/// Whether the session is connected.
|
|
/// </summary>
|
|
public bool IsConnected => m_state == ConnectionState.Connected;
|
|
|
|
/// <summary>
|
|
/// Event raised when the state changes.
|
|
/// </summary>
|
|
public event EventHandler<ConnectionStateChangedEventArgs>?
|
|
StateChanged;
|
|
|
|
/// <summary>
|
|
/// Wait until connected or cancelled.
|
|
/// </summary>
|
|
/// <param name="ct">Cancellation token.</param>
|
|
/// <returns>A task that completes when connected.</returns>
|
|
public ValueTask WaitForConnectedAsync(CancellationToken ct)
|
|
{
|
|
if (m_connected.IsSet)
|
|
{
|
|
return default;
|
|
}
|
|
|
|
return new ValueTask(m_connected.WaitAsync(ct));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wait until closed or cancelled.
|
|
/// </summary>
|
|
/// <param name="ct">Cancellation token.</param>
|
|
/// <returns>A task that completes when closed.</returns>
|
|
public ValueTask WaitForClosedAsync(CancellationToken ct)
|
|
{
|
|
if (m_closed.IsSet)
|
|
{
|
|
return default;
|
|
}
|
|
|
|
return new ValueTask(m_closed.WaitAsync(ct));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Start the background worker loop.
|
|
/// </summary>
|
|
public void Start()
|
|
{
|
|
lock (m_lock)
|
|
{
|
|
if (m_worker != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
m_worker = Task.Run(
|
|
() => WorkerLoopAsync(m_cts.Token));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Trigger a state re-evaluation (e.g., keep-alive failed).
|
|
/// Transitions to <see cref="ConnectionState.Reconnecting"/>
|
|
/// if currently connected.
|
|
/// </summary>
|
|
public void TriggerReconnect(ChannelStateChange? underlyingChannelState = null)
|
|
{
|
|
lock (m_lock)
|
|
{
|
|
if (m_state == ConnectionState.Connected)
|
|
{
|
|
TransitionTo(
|
|
ConnectionState.Reconnecting,
|
|
error: underlyingChannelState?.Error,
|
|
reconnectAttempt: 0,
|
|
underlyingChannelState);
|
|
m_connected.Reset();
|
|
}
|
|
}
|
|
|
|
m_trigger.Set();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Request closing the session.
|
|
/// </summary>
|
|
public void RequestClose()
|
|
{
|
|
lock (m_lock)
|
|
{
|
|
if (m_state is ConnectionState.Closed
|
|
or ConnectionState.Closing)
|
|
{
|
|
return;
|
|
}
|
|
|
|
TransitionTo(
|
|
ConnectionState.Closing,
|
|
error: null,
|
|
reconnectAttempt: 0);
|
|
}
|
|
|
|
m_trigger.Set();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Request initial connection. Transitions from
|
|
/// <see cref="ConnectionState.Disconnected"/> to
|
|
/// <see cref="ConnectionState.Connecting"/> and wakes the
|
|
/// worker.
|
|
/// </summary>
|
|
public void RequestConnect()
|
|
{
|
|
lock (m_lock)
|
|
{
|
|
if (m_state != ConnectionState.Disconnected)
|
|
{
|
|
return;
|
|
}
|
|
|
|
TransitionTo(
|
|
ConnectionState.Connecting,
|
|
error: null,
|
|
reconnectAttempt: 0);
|
|
}
|
|
|
|
m_trigger.Set();
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (Interlocked.Exchange(ref m_disposed, 1) != 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
RequestClose();
|
|
|
|
await m_cts.CancelAsync().ConfigureAwait(false);
|
|
m_trigger.Set();
|
|
|
|
if (m_worker != null)
|
|
{
|
|
try
|
|
{
|
|
await m_worker.ConfigureAwait(false);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
// Expected during shutdown.
|
|
}
|
|
}
|
|
|
|
m_cts.Dispose();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The main background worker loop that drives state
|
|
/// transitions.
|
|
/// </summary>
|
|
private async Task WorkerLoopAsync(CancellationToken ct)
|
|
{
|
|
m_logger.LogDebug("ConnectionStateMachine: Worker started.");
|
|
|
|
try
|
|
{
|
|
while (!ct.IsCancellationRequested)
|
|
{
|
|
await m_trigger.WaitAsync(ct).ConfigureAwait(false);
|
|
|
|
ConnectionState current;
|
|
lock (m_lock)
|
|
{
|
|
current = m_state;
|
|
}
|
|
|
|
switch (current)
|
|
{
|
|
case ConnectionState.Connecting:
|
|
await HandleConnectingAsync(ct).ConfigureAwait(false);
|
|
break;
|
|
case ConnectionState.Reconnecting:
|
|
await HandleReconnectingAsync(ct).ConfigureAwait(false);
|
|
break;
|
|
case ConnectionState.Failover:
|
|
await HandleFailoverAsync(ct).ConfigureAwait(false);
|
|
break;
|
|
case ConnectionState.Closing:
|
|
await HandleClosingAsync(ct).ConfigureAwait(false);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
// Normal shutdown.
|
|
}
|
|
finally
|
|
{
|
|
// Ensure final state is Closed if not already.
|
|
lock (m_lock)
|
|
{
|
|
if (m_state != ConnectionState.Closed)
|
|
{
|
|
TransitionTo(
|
|
ConnectionState.Closed,
|
|
error: null,
|
|
reconnectAttempt: 0);
|
|
m_connected.Reset();
|
|
}
|
|
}
|
|
|
|
m_logger.LogDebug(
|
|
"ConnectionStateMachine: Worker exiting.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handle the Connecting state: attempt initial connection.
|
|
/// </summary>
|
|
private async Task HandleConnectingAsync(CancellationToken ct)
|
|
{
|
|
m_logger.LogInformation(
|
|
"ConnectionStateMachine: Attempting initial connection.");
|
|
|
|
ServiceResult result = await InvokeConnectAsync(ct)
|
|
.ConfigureAwait(false);
|
|
|
|
lock (m_lock)
|
|
{
|
|
if (ServiceResult.IsGood(result))
|
|
{
|
|
m_reconnectPolicy.Reset();
|
|
TransitionTo(
|
|
ConnectionState.Connected,
|
|
error: null,
|
|
reconnectAttempt: 0);
|
|
m_connected.Set();
|
|
}
|
|
else
|
|
{
|
|
TransitionTo(
|
|
ConnectionState.Reconnecting,
|
|
error: result,
|
|
reconnectAttempt: 0);
|
|
m_connected.Reset();
|
|
m_trigger.Set();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handle the Reconnecting state: use the reconnect policy
|
|
/// for backoff and retry.
|
|
/// </summary>
|
|
private async Task HandleReconnectingAsync(CancellationToken ct)
|
|
{
|
|
m_connected.Reset();
|
|
IRetryBudget budget = GetOrCreateReconnectBudget();
|
|
|
|
for (int attempt = 0; !ct.IsCancellationRequested; attempt++)
|
|
{
|
|
TimeSpan? delay = m_reconnectPolicy.GetNextDelay(attempt, ct);
|
|
|
|
if (delay == null)
|
|
{
|
|
TransitionToFailover(attempt, budgetExhausted: false);
|
|
return;
|
|
}
|
|
|
|
if (!budget.TryConsume(out TimeSpan remaining))
|
|
{
|
|
TransitionToFailover(attempt, budgetExhausted: true);
|
|
return;
|
|
}
|
|
|
|
if (remaining < delay.Value)
|
|
{
|
|
delay = remaining;
|
|
}
|
|
|
|
m_logger.LogInformation(
|
|
"ConnectionStateMachine:Reconnect attempt {Attempt}, delay {DelayMs} ms.",
|
|
attempt, (int)delay.Value.TotalMilliseconds);
|
|
|
|
await m_timeProvider.Delay(delay.Value, ct)
|
|
.ConfigureAwait(false);
|
|
|
|
if (budget.IsExhausted)
|
|
{
|
|
TransitionToFailover(attempt, budgetExhausted: true);
|
|
return;
|
|
}
|
|
|
|
ServiceResult result = await InvokeReconnectAsync(budget, ct)
|
|
.ConfigureAwait(false);
|
|
|
|
if (ServiceResult.IsGood(result))
|
|
{
|
|
m_logger.LogInformation(
|
|
"ConnectionStateMachine: Reconnected on attempt {Attempt}.",
|
|
attempt);
|
|
|
|
budget.Reset();
|
|
ClearReconnectBudget();
|
|
m_reconnectPolicy.Reset();
|
|
|
|
lock (m_lock)
|
|
{
|
|
TransitionTo(
|
|
ConnectionState.Connected,
|
|
error: null,
|
|
reconnectAttempt: 0);
|
|
m_connected.Set();
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
lock (m_lock)
|
|
{
|
|
// Stay in Reconnecting but notify observers
|
|
// of the failed attempt.
|
|
if (m_state is ConnectionState.Closing
|
|
or ConnectionState.Closed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
OnStateChanged(new ConnectionStateChangedEventArgs
|
|
{
|
|
PreviousState = ConnectionState.Reconnecting,
|
|
NewState = ConnectionState.Reconnecting,
|
|
Error = result,
|
|
ReconnectAttempt = attempt
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
private IRetryBudget GetOrCreateReconnectBudget()
|
|
{
|
|
IRetryBudget? budget = m_reconnectBudget;
|
|
if (budget != null)
|
|
{
|
|
return budget;
|
|
}
|
|
|
|
budget = new RetryBudget(m_maxTotalReconnectTime, m_timeProvider);
|
|
m_reconnectBudget = budget;
|
|
return budget;
|
|
}
|
|
|
|
private void ClearReconnectBudget()
|
|
{
|
|
m_reconnectBudget = null;
|
|
}
|
|
|
|
private void TransitionToFailover(
|
|
int attempt,
|
|
bool budgetExhausted)
|
|
{
|
|
if (budgetExhausted)
|
|
{
|
|
m_logger.LogWarning(
|
|
"ConnectionStateMachine: Reconnect budget exhausted after " +
|
|
"{Attempt} attempts, entering failover.",
|
|
attempt);
|
|
}
|
|
else
|
|
{
|
|
m_logger.LogWarning(
|
|
"ConnectionStateMachine: Reconnect policy exhausted after " +
|
|
"{Attempt} attempts, entering failover.",
|
|
attempt);
|
|
}
|
|
|
|
lock (m_lock)
|
|
{
|
|
TransitionTo(
|
|
ConnectionState.Failover,
|
|
error: null,
|
|
reconnectAttempt: attempt);
|
|
}
|
|
|
|
m_trigger.Set();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handle the Failover state: attempt connection to a
|
|
/// redundant server.
|
|
/// </summary>
|
|
private async Task HandleFailoverAsync(CancellationToken ct)
|
|
{
|
|
m_logger.LogInformation(
|
|
"ConnectionStateMachine: Attempting failover to redundant server.");
|
|
|
|
IRetryBudget budget = GetOrCreateReconnectBudget();
|
|
ServiceResult result = await InvokeFailoverAsync(budget, ct).ConfigureAwait(false);
|
|
|
|
lock (m_lock)
|
|
{
|
|
if (ServiceResult.IsGood(result))
|
|
{
|
|
budget.Reset();
|
|
ClearReconnectBudget();
|
|
m_reconnectPolicy.Reset();
|
|
TransitionTo(
|
|
ConnectionState.Connected,
|
|
error: null,
|
|
reconnectAttempt: 0);
|
|
m_connected.Set();
|
|
}
|
|
else
|
|
{
|
|
ClearReconnectBudget();
|
|
m_logger.LogError(
|
|
"ConnectionStateMachine: Failover failed: {Error}.",
|
|
result);
|
|
|
|
TransitionTo(
|
|
ConnectionState.Disconnected,
|
|
error: result,
|
|
reconnectAttempt: 0);
|
|
m_connected.Reset();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handle the Closing state: close the session and
|
|
/// transition to Closed.
|
|
/// </summary>
|
|
private async Task HandleClosingAsync(CancellationToken ct)
|
|
{
|
|
m_logger.LogInformation("ConnectionStateMachine: Closing session.");
|
|
|
|
try
|
|
{
|
|
if (CloseSessionAsync != null)
|
|
{
|
|
await CloseSessionAsync(ct).ConfigureAwait(false);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
when (ex is not OperationCanceledException)
|
|
{
|
|
m_logger.LogWarning(
|
|
ex,
|
|
"ConnectionStateMachine: Error during session close.");
|
|
}
|
|
|
|
lock (m_lock)
|
|
{
|
|
TransitionTo(
|
|
ConnectionState.Closed,
|
|
error: null,
|
|
reconnectAttempt: 0);
|
|
m_connected.Reset();
|
|
m_closed.Set();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Safely invoke the connect delegate.
|
|
/// </summary>
|
|
private async Task<ServiceResult> InvokeConnectAsync(CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
if (ConnectAsync != null)
|
|
{
|
|
return await ConnectAsync(ct).ConfigureAwait(false);
|
|
}
|
|
|
|
return new ServiceResult(StatusCodes.BadInvalidState);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
m_logger.LogError(
|
|
ex,
|
|
"ConnectionStateMachine: Connect failed with exception.");
|
|
return new ServiceResult(ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Safely invoke the reconnect delegate.
|
|
/// </summary>
|
|
private async Task<ServiceResult> InvokeReconnectAsync(
|
|
IRetryBudget budget,
|
|
CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
if (ReconnectWithBudgetAsync != null)
|
|
{
|
|
return await ReconnectWithBudgetAsync(budget, ct).ConfigureAwait(false);
|
|
}
|
|
if (ReconnectAsync != null)
|
|
{
|
|
return await ReconnectAsync(ct).ConfigureAwait(false);
|
|
}
|
|
|
|
// Fall back to connect if no reconnect delegate.
|
|
return await InvokeConnectAsync(ct).ConfigureAwait(false);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
m_logger.LogError(
|
|
ex,
|
|
"ConnectionStateMachine: Reconnect failed with exception.");
|
|
return new ServiceResult(ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Safely invoke the failover delegate.
|
|
/// </summary>
|
|
private async Task<ServiceResult> InvokeFailoverAsync(
|
|
IRetryBudget budget,
|
|
CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
if (FailoverWithBudgetAsync != null)
|
|
{
|
|
return await FailoverWithBudgetAsync(budget, ct).ConfigureAwait(false);
|
|
}
|
|
if (FailoverAsync != null)
|
|
{
|
|
return await FailoverAsync(ct).ConfigureAwait(false);
|
|
}
|
|
|
|
return new ServiceResult(StatusCodes.BadNotSupported);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
m_logger.LogError(
|
|
ex,
|
|
"ConnectionStateMachine: Failover failed with exception.");
|
|
return new ServiceResult(ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Transition to a new state and raise the <see cref="StateChanged"/> event.
|
|
/// Must be called under <see cref="m_lock"/>.
|
|
/// </summary>
|
|
private void TransitionTo(
|
|
ConnectionState newState,
|
|
ServiceResult? error,
|
|
int reconnectAttempt,
|
|
ChannelStateChange? underlyingChannelState = null)
|
|
{
|
|
ConnectionState previous = m_state;
|
|
if (previous == newState)
|
|
{
|
|
return;
|
|
}
|
|
|
|
m_state = newState;
|
|
|
|
m_logger.LogInformation(
|
|
"ConnectionStateMachine: State changed from {Old} to {New}.",
|
|
previous, newState);
|
|
|
|
OnStateChanged(new ConnectionStateChangedEventArgs
|
|
{
|
|
PreviousState = previous,
|
|
NewState = newState,
|
|
Error = error,
|
|
ReconnectAttempt = reconnectAttempt,
|
|
UnderlyingChannelState = underlyingChannelState
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Raise the <see cref="StateChanged"/> event, swallowing handler exceptions.
|
|
/// </summary>
|
|
private void OnStateChanged(
|
|
ConnectionStateChangedEventArgs args)
|
|
{
|
|
try
|
|
{
|
|
StateChanged?.Invoke(this, args);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
m_logger.LogError(
|
|
ex,
|
|
"ConnectionStateMachine: StateChanged handler threw an exception.");
|
|
}
|
|
}
|
|
}
|
|
}
|