-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
* Suggested fixes to interfaces * Additional Adjustments * missed blank line between properties * Fix to .editorconfig and referenced files. Made HeaderParser internal and removed it from the interface. * Removed EnumeratorCancellation * Adjustments to meet requested changes.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ public enum NatsConnectionState | |
Reconnecting, | ||
} | ||
|
||
public partial class NatsConnection : IAsyncDisposable, INatsConnection | ||
public partial class NatsConnection : INatsConnection | ||
{ | ||
#pragma warning disable SA1401 | ||
/// <summary> | ||
|
@@ -25,6 +25,8 @@ public partial class NatsConnection : IAsyncDisposable, INatsConnection | |
|
||
internal readonly ConnectionStatsCounter Counter; // allow to call from external sources | ||
internal ServerInfo? WritableServerInfo; | ||
internal bool IsDisposed; | ||
|
||
#pragma warning restore SA1401 | ||
private readonly object _gate = new object(); | ||
private readonly WriterState _writerState; | ||
|
@@ -36,12 +38,11 @@ public partial class NatsConnection : IAsyncDisposable, INatsConnection | |
private readonly string _name; | ||
private readonly TimeSpan _socketComponentDisposeTimeout = TimeSpan.FromSeconds(5); | ||
private readonly BoundedChannelOptions _defaultSubscriptionChannelOpts; | ||
|
||
private readonly ClientOpts _clientOpts; | ||
private int _pongCount; | ||
private bool _isDisposed; | ||
private int _connectionState; | ||
|
||
// when reconnect, make new instance. | ||
// when reconnected, make new instance. | ||
private ISocketConnection? _socket; | ||
private CancellationTokenSource? _pingTimerCancellationTokenSource; | ||
private volatile NatsUri? _currentConnectUri; | ||
|
@@ -50,7 +51,6 @@ public partial class NatsConnection : IAsyncDisposable, INatsConnection | |
private NatsPipeliningWriteProtocolProcessor? _socketWriter; | ||
private TaskCompletionSource _waitForOpenConnection; | ||
private TlsCerts? _tlsCerts; | ||
private ClientOpts _clientOpts; | ||
private UserCredentials? _userCredentials; | ||
private int _connectRetry; | ||
private TimeSpan _backoff = TimeSpan.Zero; | ||
|
@@ -107,7 +107,7 @@ public NatsConnectionState ConnectionState | |
|
||
public INatsServerInfo? ServerInfo => WritableServerInfo; // server info is set when received INFO | ||
|
||
public NatsHeaderParser HeaderParser { get; } | ||
internal NatsHeaderParser HeaderParser { get; } | ||
|
||
internal SubscriptionManager SubscriptionManager { get; } | ||
|
||
|
@@ -150,13 +150,13 @@ public async ValueTask ConnectAsync() | |
} | ||
} | ||
|
||
public NatsStats GetStats() => Counter.ToStats(); | ||
internal NatsStats GetStats() => Counter.ToStats(); | ||
|
||
public async ValueTask DisposeAsync() | ||
public virtual async ValueTask DisposeAsync() | ||
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (main)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (main)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (latest)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (latest)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (release/v2.9.23)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / test (release/v2.9.23)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / dotnet (main)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / dotnet (main)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / dotnet (latest)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / dotnet (latest)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / memory test (latest)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / memory test (latest)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / memory test (main)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / memory test (main)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / memory test (release/v2.9.23)
Check warning on line 155 in src/NATS.Client.Core/NatsConnection.cs GitHub Actions / memory test (release/v2.9.23)
|
||
{ | ||
if (!_isDisposed) | ||
if (!IsDisposed) | ||
{ | ||
_isDisposed = true; | ||
IsDisposed = true; | ||
_logger.Log(LogLevel.Information, NatsLogEvents.Connection, "Disposing connection {Name}", _name); | ||
|
||
await DisposeSocketAsync(false).ConfigureAwait(false); | ||
|
@@ -237,7 +237,7 @@ internal ValueTask UnsubscribeAsync(int sid) | |
catch (Exception ex) | ||
{ | ||
// connection is disposed, don't need to unsubscribe command. | ||
if (_isDisposed) | ||
if (IsDisposed) | ||
{ | ||
return ValueTask.CompletedTask; | ||
} | ||
|
@@ -526,9 +526,9 @@ private async void ReconnectLoop() | |
|
||
var defaultScheme = _currentConnectUri!.Uri.Scheme; | ||
var urls = (Opts.NoRandomize | ||
? WritableServerInfo?.ClientConnectUrls?.Select(x => new NatsUri(x, false, defaultScheme)).Distinct().ToArray() | ||
: WritableServerInfo?.ClientConnectUrls?.Select(x => new NatsUri(x, false, defaultScheme)).OrderBy(_ => Guid.NewGuid()).Distinct().ToArray()) | ||
?? Array.Empty<NatsUri>(); | ||
? WritableServerInfo?.ClientConnectUrls?.Select(x => new NatsUri(x, false, defaultScheme)).Distinct().ToArray() | ||
: WritableServerInfo?.ClientConnectUrls?.Select(x => new NatsUri(x, false, defaultScheme)).OrderBy(_ => Guid.NewGuid()).Distinct().ToArray()) | ||
?? Array.Empty<NatsUri>(); | ||
if (urls.Length == 0) | ||
urls = Opts.GetSeedUris(); | ||
|
||
|
@@ -538,7 +538,7 @@ private async void ReconnectLoop() | |
_currentConnectUri = null; | ||
var urlEnumerator = urls.AsEnumerable().GetEnumerator(); | ||
NatsUri? url = null; | ||
CONNECT_AGAIN: | ||
CONNECT_AGAIN: | ||
try | ||
{ | ||
if (urlEnumerator.MoveNext()) | ||
|
@@ -748,7 +748,7 @@ private void EnqueueCommandSync(ICommand command) | |
|
||
private async ValueTask EnqueueCommandAsync(ICommand command) | ||
{ | ||
RETRY: | ||
RETRY: | ||
if (_commandWriter.TryWrite(command)) | ||
{ | ||
Interlocked.Increment(ref Counter.PendingMessages); | ||
|
@@ -812,7 +812,7 @@ private async ValueTask DisposeSocketAsync(bool asyncReaderDispose) | |
|
||
private void ThrowIfDisposed() | ||
{ | ||
if (_isDisposed) | ||
if (IsDisposed) | ||
throw new ObjectDisposedException(null); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace NATS.Client.Core; | ||
|
||
internal sealed class NatsPooledConnection : NatsConnection | ||
{ | ||
public NatsPooledConnection(NatsOpts opts) | ||
: base(opts) | ||
{ | ||
} | ||
|
||
public override ValueTask DisposeAsync() => ValueTask.CompletedTask; | ||
|
||
internal ValueTask ForceDisposeAsync() => base.DisposeAsync(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace NATS.Client.Core.Tests; | ||
|
||
public class NatsConnectionPoolTest | ||
{ | ||
[Fact] | ||
public async Task ConnectionsShouldBeNonDisposable() | ||
{ | ||
// Arrange | ||
NatsConnectionPool pool = new(1); | ||
|
||
// Act | ||
await using (var con = pool.GetConnection()) | ||
{ | ||
} | ||
|
||
// Assert | ||
var con2 = (NatsConnection) pool.GetConnection(); | ||
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (main)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (main)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (main)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (latest)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (latest)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (latest)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (latest)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (latest)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (latest)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (main)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (main)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (main)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (release/v2.9.23)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (release/v2.9.23)
Check warning on line 17 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (release/v2.9.23)
|
||
con2.IsDisposed.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public async Task ConnectionsShouldBeDisposedWhenPoolIsDisposed() | ||
{ | ||
// Arrange | ||
NatsConnectionPool pool = new(1); | ||
|
||
var con = (NatsConnection) pool.GetConnection(); | ||
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (release/v2.9.23)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (main)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (main)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (main)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (latest)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (latest)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / dotnet (latest)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (latest)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (latest)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (latest)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (main)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (main)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (main)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (release/v2.9.23)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (release/v2.9.23)
Check warning on line 27 in tests/NATS.Client.Core.Tests/NatsConnectionPoolTest.cs GitHub Actions / memory test (release/v2.9.23)
|
||
|
||
// Act | ||
await pool.DisposeAsync(); | ||
|
||
// Assert | ||
con.IsDisposed.Should().BeTrue(); | ||
} | ||
} |