Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (force) reconnect method #684

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/NATS.Client.Core/INatsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,12 @@ ValueTask<NatsMsg<TReply>> RequestAsync<TReply>(
INatsDeserialize<TReply>? replySerializer = default,
NatsSubOpts? replyOpts = default,
CancellationToken cancellationToken = default);

/// <summary>
/// Force a reconnection to the NATS server.
/// </summary>
/// <remarks>
/// This method doesn't wait for the connection to be established.
/// </remarks>
ValueTask ReconnectAsync();
}
18 changes: 18 additions & 0 deletions src/NATS.Client.Core/NatsConnection.Reconnect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.Extensions.Logging;

namespace NATS.Client.Core;

public partial class NatsConnection
{
/// <inheritdoc/>
public async ValueTask ReconnectAsync()
{
if (ConnectionState != NatsConnectionState.Open)
{
return;
}

_logger.LogInformation(NatsLogEvents.Connection, "Forcing reconnection to NATS server");
await _socket!.AbortConnectionAsync(CancellationToken.None).ConfigureAwait(false);
}
}
3 changes: 3 additions & 0 deletions src/NATS.Client.Simplified/NatsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public ValueTask<NatsMsg<TReply>> RequestAsync<TRequest, TReply>(string subject,
public ValueTask<NatsMsg<TReply>> RequestAsync<TReply>(string subject, INatsDeserialize<TReply>? replySerializer = default, NatsSubOpts? replyOpts = default, CancellationToken cancellationToken = default)
=> Connection.RequestAsync(subject, replySerializer, replyOpts, cancellationToken);

/// <inheritdoc />
public ValueTask ReconnectAsync() => Connection.ReconnectAsync();

/// <inheritdoc />
public ValueTask DisposeAsync() => Connection.DisposeAsync();
}
39 changes: 39 additions & 0 deletions tests/NATS.Client.Core.Tests/NatsConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,45 @@ public async Task OnSocketAvailableAsync_ShouldBeInvokedOnReconnection()
// Assert
Assert.Equal(2, invocationCount);
}

[Fact]
public async Task ReconnectOnOpenConnection_ShouldDisconnectAndOpenNewConnection()
{
// Arrange
await using var server = NatsServer.Start(_output, _transportType);
await using var connection = server.CreateClientConnection();
await connection.ConnectAsync(); // wait first connection open

var openedCount = 0;
var disconnectedCount = 0;

var openSignal = new WaitSignal();
var disconnectSignal = new WaitSignal();

connection.ConnectionOpened += (_, _) =>
{
Interlocked.Increment(ref openedCount);
openSignal.Pulse();
return default;
};
connection.ConnectionDisconnected += (_, _) =>
{
Interlocked.Increment(ref disconnectedCount);
disconnectSignal.Pulse();
return default;
};

// Act
await connection.ReconnectAsync();
await disconnectSignal;
await openSignal;

// Assert
// First connection is not taken into account, so one invocation of
// disconnected event and open connection event are expected
openedCount.ShouldBe(1);
disconnectedCount.ShouldBe(1);
}
}

[JsonSerializable(typeof(SampleClass))]
Expand Down
2 changes: 2 additions & 0 deletions tests/NATS.Client.JetStream.Tests/NatsJsContextFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ public ValueTask<NatsSub<TReply>> CreateRequestSubAsync<TRequest, TReply>(string

public ValueTask ConnectAsync() => throw new NotImplementedException();

public ValueTask ReconnectAsync() => throw new NotImplementedException();

public ValueTask DisposeAsync() => throw new NotImplementedException();
}
}
Expand Down