Skip to content

Commit

Permalink
* Change connection so it disposes its channel and session
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebakken committed Apr 26, 2024
1 parent be31dd6 commit 722aa36
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions projects/RabbitMQ.Client/client/impl/ChannelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ protected virtual void Dispose(bool disposing)
this.AbortAsync().GetAwaiter().GetResult();
}
ConsumerDispatcher.Dispose();
Session.Dispose();
_rpcSemaphore.Dispose();
_confirmSemaphore?.Dispose();
}
Expand Down
5 changes: 3 additions & 2 deletions projects/RabbitMQ.Client/client/impl/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal sealed partial class Connection : IConnection
private volatile bool _closed;

private readonly ConnectionConfig _config;
private readonly ChannelBase _channel0; // FUTURE Note: this is not disposed
private readonly ChannelBase _channel0;
private readonly MainSession _session0;

private Guid _id = Guid.NewGuid();
Expand Down Expand Up @@ -513,7 +513,8 @@ private void Dispose(bool disposing)
{
this.AbortAsync().GetAwaiter().GetResult();
}
_session0.Dispose();
((IDisposable)_channel0).Dispose();
((IDisposable)_session0).Dispose();
_mainLoopCts.Dispose();
_sessionManager.Dispose();
}
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/ISession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace RabbitMQ.Client.Impl
{
internal delegate Task CommandReceivedAction(IncomingCommand cmd, CancellationToken cancellationToken);

internal interface ISession
internal interface ISession : IDisposable
{
/// <summary>
/// Gets the channel number.
Expand Down
15 changes: 13 additions & 2 deletions projects/RabbitMQ.Client/client/impl/MainSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
namespace RabbitMQ.Client.Impl
{
///<summary>Small ISession implementation used only for channel 0.</summary>
internal sealed class MainSession : Session, IDisposable
internal sealed class MainSession : Session
{
private volatile bool _closeIsServerInitiated;
private volatile bool _closing;
Expand Down Expand Up @@ -121,6 +121,17 @@ public override ValueTask TransmitAsync<T>(in T cmd, CancellationToken cancellat
return base.TransmitAsync(in cmd, cancellationToken);
}

public void Dispose() => ((IDisposable)_closingSemaphore).Dispose();
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (false == _disposedValue)
{
((IDisposable)_closingSemaphore).Dispose();
}
}

base.Dispose(disposing);
}
}
}
16 changes: 16 additions & 0 deletions projects/RabbitMQ.Client/client/impl/SessionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace RabbitMQ.Client.Impl
{
internal abstract class SessionBase : ISession
{
protected bool _disposedValue;
private ShutdownEventArgs _closeReason;
public ShutdownEventArgs CloseReason => Volatile.Read(ref _closeReason);

Expand Down Expand Up @@ -169,5 +170,20 @@ public ValueTask TransmitAsync<TMethod, THeader>(in TMethod cmd, in THeader head

private void ThrowAlreadyClosedException()
=> throw new AlreadyClosedException(CloseReason);

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_disposedValue = true;
}
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,6 @@ public ISession Lookup(int number)
}
}

public void Dispose() => _sessionMapSemaphore.Dispose();
public void Dispose() => ((IDisposable)_sessionMapSemaphore).Dispose();
}
}

0 comments on commit 722aa36

Please sign in to comment.