Skip to content

* Change IConnection event ConnectionShutdown to AsyncEventHandler #1551

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

Closed
wants to merge 5 commits into from
Closed
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
14 changes: 11 additions & 3 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ jobs:
Receive-Job -Job $tx; `
& "${{ github.workspace }}\.ci\windows\toxiproxy\toxiproxy-cli.exe" list; `
dotnet test `
--environment "RABBITMQ_RABBITMQCTL_PATH=${{ steps.install-start-rabbitmq.outputs.path }}" `
--environment 'RABBITMQ_LONG_RUNNING_TESTS=true' `
--environment "RABBITMQ_RABBITMQCTL_PATH=${{ steps.install-start-rabbitmq.outputs.path }}" `
--environment 'RABBITMQ_TOXIPROXY_TESTS=true' `
--environment 'PASSWORD=grapefruit' `
--environment SSL_CERTS_DIR="${{ github.workspace }}\.ci\certs" `
Expand Down Expand Up @@ -114,7 +114,12 @@ jobs:
id: install-start-rabbitmq
run: ${{ github.workspace }}\.ci\windows\gha-setup.ps1
- name: Sequential Integration Tests
run: dotnet test --environment "RABBITMQ_RABBITMQCTL_PATH=${{ steps.install-start-rabbitmq.outputs.path }}" "${{ github.workspace }}\projects\Test\SequentialIntegration\SequentialIntegration.csproj" --no-restore --no-build --logger 'console;verbosity=detailed'
run: dotnet test `
--environment 'RABBITMQ_LONG_RUNNING_TESTS=true' `
--environment "RABBITMQ_RABBITMQCTL_PATH=${{ steps.install-start-rabbitmq.outputs.path }}" `
--environment 'PASSWORD=grapefruit' `
--environment SSL_CERTS_DIR="${{ github.workspace }}\.ci\certs" `
"${{ github.workspace }}\projects\Test\SequentialIntegration\SequentialIntegration.csproj" --no-restore --no-build --logger 'console;verbosity=detailed'
- name: Maybe upload RabbitMQ logs
if: failure()
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -182,8 +187,8 @@ jobs:
- name: Integration Tests
run: |
dotnet test \
--environment "RABBITMQ_RABBITMQCTL_PATH=DOCKER:${{ steps.start-rabbitmq.outputs.id }}" \
--environment 'RABBITMQ_LONG_RUNNING_TESTS=true' \
--environment "RABBITMQ_RABBITMQCTL_PATH=DOCKER:${{ steps.start-rabbitmq.outputs.id }}" \
--environment 'RABBITMQ_TOXIPROXY_TESTS=true' \
--environment 'PASSWORD=grapefruit' \
--environment SSL_CERTS_DIR="${{ github.workspace }}/.ci/certs" \
Expand Down Expand Up @@ -222,7 +227,10 @@ jobs:
- name: Sequential Integration Tests
run: |
dotnet test \
--environment 'RABBITMQ_LONG_RUNNING_TESTS=true' \
--environment "RABBITMQ_RABBITMQCTL_PATH=DOCKER:${{ steps.start-rabbitmq.outputs.id }}" \
--environment 'PASSWORD=grapefruit' \
--environment SSL_CERTS_DIR="${{ github.workspace }}/.ci/certs" \
"${{ github.workspace }}/projects/Test/SequentialIntegration/SequentialIntegration.csproj" --no-restore --no-build --logger 'console;verbosity=detailed'
- name: Maybe upload RabbitMQ logs
if: failure()
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ build:
test:
dotnet test $(CURDIR)/projects/Test/Unit/Unit.csproj --logger 'console;verbosity=detailed'
dotnet test --environment 'GITHUB_ACTIONS=true' \
--environment "RABBITMQ_RABBITMQCTL_PATH=DOCKER:$$(docker inspect --format='{{.Id}}' $(RABBITMQ_DOCKER_NAME))" \
--environment 'RABBITMQ_LONG_RUNNING_TESTS=true' \
--environment "RABBITMQ_RABBITMQCTL_PATH=DOCKER:$$(docker inspect --format='{{.Id}}' $(RABBITMQ_DOCKER_NAME))" \
--environment 'RABBITMQ_TOXIPROXY_TESTS=true' \
--environment 'PASSWORD=grapefruit' \
--environment SSL_CERTS_DIR="$(CURDIR)/.ci/certs" \
"$(CURDIR)/projects/Test/Integration/Integration.csproj" --logger 'console;verbosity=detailed'
dotnet test --environment "RABBITMQ_RABBITMQCTL_PATH=DOCKER:$$(docker inspect --format='{{.Id}}' $(RABBITMQ_DOCKER_NAME))" $(CURDIR)/projects/Test/SequentialIntegration/SequentialIntegration.csproj --logger 'console;verbosity=detailed'
dotnet test --environment 'RABBITMQ_LONG_RUNNING_TESTS=true' \
--environment "RABBITMQ_RABBITMQCTL_PATH=DOCKER:$$(docker inspect --format='{{.Id}}' $(RABBITMQ_DOCKER_NAME))" \
--environment 'PASSWORD=grapefruit' \
--environment SSL_CERTS_DIR="$(CURDIR)/.ci/certs" \
$(CURDIR)/projects/Test/SequentialIntegration/SequentialIntegration.csproj --logger 'console;verbosity=detailed'

# Note:
# You must have the expected OAuth2 environment set up for this target
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ RabbitMQ.Client.IConnection.ClientProvidedName.get -> string
RabbitMQ.Client.IConnection.CloseReason.get -> RabbitMQ.Client.ShutdownEventArgs
RabbitMQ.Client.IConnection.ConnectionBlocked -> System.EventHandler<RabbitMQ.Client.Events.ConnectionBlockedEventArgs>
RabbitMQ.Client.IConnection.ConnectionRecoveryError -> System.EventHandler<RabbitMQ.Client.Events.ConnectionRecoveryErrorEventArgs>
RabbitMQ.Client.IConnection.ConnectionShutdown -> System.EventHandler<RabbitMQ.Client.ShutdownEventArgs>
RabbitMQ.Client.IConnection.ConnectionShutdownAsync -> RabbitMQ.Client.Events.AsyncEventHandler<RabbitMQ.Client.ShutdownEventArgs>
RabbitMQ.Client.IConnection.ConnectionUnblocked -> System.EventHandler<System.EventArgs>
RabbitMQ.Client.IConnection.ConsumerTagChangeAfterRecovery -> System.EventHandler<RabbitMQ.Client.Events.ConsumerTagChangedAfterRecoveryEventArgs>
RabbitMQ.Client.IConnection.Endpoint.get -> RabbitMQ.Client.AmqpTcpEndpoint
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/api/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public interface IConnection : INetworkConnection, IDisposable
/// event handler is added to this event, the event handler
/// will be fired immediately.
/// </remarks>
event EventHandler<ShutdownEventArgs> ConnectionShutdown;
event AsyncEventHandler<ShutdownEventArgs> ConnectionShutdownAsync;

/// <summary>
/// Raised when the connection completes recovery.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal sealed partial class AutorecoveringConnection
private Task? _recoveryTask;
private readonly CancellationTokenSource _recoveryCancellationTokenSource = new CancellationTokenSource();

private void HandleConnectionShutdown(object _, ShutdownEventArgs args)
private Task HandleConnectionShutdownAsync(object _, ShutdownEventArgs args)
{
if (ShouldTriggerConnectionRecovery(args))
{
Expand Down Expand Up @@ -80,6 +80,8 @@ static bool ShouldTriggerConnectionRecovery(ShutdownEventArgs args)

return false;
}

return Task.CompletedTask;
}

private async Task RecoverConnectionAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void onException(Exception exception, string context) =>
_consumerTagChangeAfterRecoveryWrapper = new EventingWrapper<ConsumerTagChangedAfterRecoveryEventArgs>("OnConsumerRecovery", onException);
_queueNameChangedAfterRecoveryWrapper = new EventingWrapper<QueueNameChangedAfterRecoveryEventArgs>("OnQueueRecovery", onException);

ConnectionShutdown += HandleConnectionShutdown;
ConnectionShutdownAsync += HandleConnectionShutdownAsync;
}

public event EventHandler<EventArgs> RecoverySucceeded
Expand Down Expand Up @@ -117,10 +117,10 @@ public event EventHandler<ConnectionBlockedEventArgs> ConnectionBlocked
remove => InnerConnection.ConnectionBlocked -= value;
}

public event EventHandler<ShutdownEventArgs> ConnectionShutdown
public event AsyncEventHandler<ShutdownEventArgs> ConnectionShutdownAsync
{
add => InnerConnection.ConnectionShutdown += value;
remove => InnerConnection.ConnectionShutdown -= value;
add => InnerConnection.ConnectionShutdownAsync += value;
remove => InnerConnection.ConnectionShutdownAsync -= value;
}

public event EventHandler<EventArgs> ConnectionUnblocked
Expand Down
28 changes: 16 additions & 12 deletions projects/RabbitMQ.Client/client/impl/ChannelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ protected ChannelBase(ConnectionConfig config, ISession session)

Action<Exception, string> onException = (exception, context) =>
OnCallbackException(CallbackExceptionEventArgs.Build(exception, context));

_basicAcksWrapper = new EventingWrapper<BasicAckEventArgs>("OnBasicAck", onException);
_basicNacksWrapper = new EventingWrapper<BasicNackEventArgs>("OnBasicNack", onException);
_basicReturnWrapper = new EventingWrapper<BasicReturnEventArgs>("OnBasicReturn", onException);
Expand All @@ -93,7 +94,7 @@ protected ChannelBase(ConnectionConfig config, ISession session)
_channelShutdownWrapper = new EventingWrapper<ShutdownEventArgs>("OnChannelShutdown", onException);
_recoveryWrapper = new EventingWrapper<EventArgs>("OnChannelRecovery", onException);
session.CommandReceived = HandleCommandAsync;
session.SessionShutdown += OnSessionShutdown;
session.SessionShutdownAsync += OnSessionShutdownAsync;
Session = session;
}

Expand Down Expand Up @@ -407,12 +408,13 @@ await ModelSendAsync(method, k.CancellationToken)
}
}

internal void FinishClose()
internal async Task FinishCloseAsync()
{
ShutdownEventArgs reason = CloseReason;
if (reason != null)
{
Session.Close(reason);
await Session.CloseAsync(reason)
.ConfigureAwait(false);
}

m_connectionStartCell?.TrySetResult(null);
Expand Down Expand Up @@ -488,7 +490,7 @@ private void OnChannelShutdown(ShutdownEventArgs reason)
if (_confirmsTaskCompletionSources?.Count > 0)
{
var exception = new AlreadyClosedException(reason);
foreach (var confirmsTaskCompletionSource in _confirmsTaskCompletionSources)
foreach (TaskCompletionSource<bool> confirmsTaskCompletionSource in _confirmsTaskCompletionSources)
{
confirmsTaskCompletionSource.TrySetException(exception);
}
Expand All @@ -505,14 +507,12 @@ private void OnChannelShutdown(ShutdownEventArgs reason)
_flowControlBlock.Set();
}

// TODO async
private void OnSessionShutdown(object sender, ShutdownEventArgs reason)
private Task OnSessionShutdownAsync(object sender, ShutdownEventArgs reason)
{
ConsumerDispatcher.Quiesce();
SetCloseReason(reason);
OnChannelShutdown(reason);
// TODO async
ConsumerDispatcher.Shutdown(reason);
return ConsumerDispatcher.ShutdownAsync(reason);
}

internal bool SetCloseReason(ShutdownEventArgs reason)
Expand Down Expand Up @@ -723,7 +723,8 @@ protected async Task<bool> HandleChannelCloseAsync(IncomingCommand cmd, Cancella
channelClose._classId,
channelClose._methodId));

Session.Close(CloseReason, false);
await Session.CloseAsync(CloseReason, false)
.ConfigureAwait(false);

var method = new ChannelCloseOk();
await ModelSendAsync(method, cancellationToken)
Expand All @@ -734,7 +735,8 @@ await ModelSendAsync(method, cancellationToken)
finally
{
cmd.ReturnBuffers();
Session.Notify();
await Session.NotifyAsync()
.ConfigureAwait(false);
}
}

Expand All @@ -746,7 +748,8 @@ protected async Task<bool> HandleChannelCloseOkAsync(IncomingCommand cmd, Cancel
* Note:
* This call _must_ come before completing the async continuation
*/
FinishClose();
await FinishCloseAsync()
.ConfigureAwait(false);

if (_continuationQueue.TryPeek<ChannelCloseAsyncRpcContinuation>(out var k))
{
Expand Down Expand Up @@ -815,7 +818,8 @@ protected async Task<bool> HandleConnectionCloseAsync(IncomingCommand cmd, Cance
var reason = new ShutdownEventArgs(ShutdownInitiator.Peer, method._replyCode, method._replyText, method._classId, method._methodId);
try
{
Session.Connection.ClosedViaPeer(reason);
await Session.Connection.ClosedViaPeerAsync(reason)
.ConfigureAwait(false);

var replyMethod = new ConnectionCloseOk();
await ModelSendAsync(replyMethod, cancellationToken)
Expand Down
10 changes: 9 additions & 1 deletion projects/RabbitMQ.Client/client/impl/Connection.Heartbeat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ private async void HeartbeatReadTimerCallback(object? state)
{
var eose = new EndOfStreamException($"Heartbeat missing with heartbeat == {_heartbeat} seconds");
LogCloseError(eose.Message, eose);
HandleMainLoopException(new ShutdownEventArgs(ShutdownInitiator.Library, 0, "End of stream", eose));
await HandleMainLoopExceptionAsync(new ShutdownEventArgs(ShutdownInitiator.Library, 0, "End of stream", eose))
.ConfigureAwait(false);
shouldTerminate = true;
}
}
Expand Down Expand Up @@ -158,6 +159,13 @@ await WriteAsync(Client.Impl.Framing.Heartbeat.GetHeartbeatFrame(), _mainLoopCts
_heartbeatWriteTimer?.Change((int)_heartbeatWriteTimeSpan.TotalMilliseconds, Timeout.Infinite);
}
}
catch (OperationCanceledException ocex)
{
if (ocex.CancellationToken != _mainLoopCts.Token)
{
throw;
}
}
catch (ObjectDisposedException)
{
// timer is already disposed,
Expand Down
32 changes: 25 additions & 7 deletions projects/RabbitMQ.Client/client/impl/Connection.Receive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ await ReceiveLoopAsync(mainLoopToken)
var ea = new ShutdownEventArgs(ShutdownInitiator.Library,
0, "End of stream",
exception: eose);
HandleMainLoopException(ea);
await HandleMainLoopExceptionAsync(ea)
.ConfigureAwait(false);
}
catch (HardProtocolException hpe)
{
Expand All @@ -75,15 +76,29 @@ await HardProtocolExceptionHandlerAsync(hpe, mainLoopToken)
var ea = new ShutdownEventArgs(ShutdownInitiator.Library,
Constants.InternalError, fileLoadException.Message,
exception: fileLoadException);
HandleMainLoopException(ea);
await HandleMainLoopExceptionAsync(ea)
.ConfigureAwait(false);
}
catch (OperationCanceledException ocex)
{
if (ocex.CancellationToken != mainLoopToken)
{
var ea = new ShutdownEventArgs(ShutdownInitiator.Library,
Constants.InternalError,
$"Unexpected Exception: {ocex.Message}",
exception: ocex);
await HandleMainLoopExceptionAsync(ea)
.ConfigureAwait(false);
}
}
catch (Exception ex)
{
var ea = new ShutdownEventArgs(ShutdownInitiator.Library,
Constants.InternalError,
$"Unexpected Exception: {ex.Message}",
exception: ex);
HandleMainLoopException(ea);
await HandleMainLoopExceptionAsync(ea)
.ConfigureAwait(false);
}

using var cts = new CancellationTokenSource(InternalConstants.DefaultConnectionCloseTimeout);
Expand Down Expand Up @@ -175,26 +190,29 @@ private void MaybeTerminateMainloopAndStopHeartbeatTimers(bool cancelMainLoop =
MaybeStopHeartbeatTimers();
}

private void HandleMainLoopException(ShutdownEventArgs reason)
private Task HandleMainLoopExceptionAsync(ShutdownEventArgs reason)
{
string message = reason.GetLogMessage();
if (false == SetCloseReason(reason))
{
LogCloseError($"Unexpected Main Loop Exception while closing: {message}", reason.Exception);
return;
return Task.CompletedTask;
}

_channel0.MaybeSetConnectionStartException(reason.Exception);

OnShutdown(reason);
LogCloseError($"Unexpected connection closure: {message}", reason.Exception);

return OnShutdownAsync(reason);
}

private async Task HardProtocolExceptionHandlerAsync(HardProtocolException hpe, CancellationToken cancellationToken)
{
if (SetCloseReason(hpe.ShutdownReason))
{
OnShutdown(hpe.ShutdownReason);
await OnShutdownAsync(hpe.ShutdownReason)
.ConfigureAwait(false);

await _session0.SetSessionClosingAsync(false)
.ConfigureAwait(false);
try
Expand Down
Loading