Skip to content

Commit d0328fe

Browse files
committed
Remove ObjectPool
This complicates the tests. After returning the rented object back to the pool, the data is resetted.
1 parent b572e37 commit d0328fe

File tree

4 files changed

+58
-61
lines changed

4 files changed

+58
-61
lines changed

projects/RabbitMQ.Client/RabbitMQ.Client.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
</ItemGroup>
6767

6868
<ItemGroup>
69-
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="8.0.0" />
7069
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" />
7170
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />
7271
<PackageReference Include="MinVer" Version="4.3.0" PrivateAssets="all" />

projects/RabbitMQ.Client/client/RentedOutgoingMemory.cs

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,48 @@
44
using System.Buffers;
55
using System.IO.Pipelines;
66
using System.Threading.Tasks;
7-
using Microsoft.Extensions.ObjectPool;
87

98
namespace RabbitMQ.Client
109
{
11-
internal class RentedOutgoingMemory : IDisposable, IResettable
10+
internal class RentedOutgoingMemory : IDisposable
1211
{
13-
private static readonly ObjectPool<RentedOutgoingMemory> s_pool = ObjectPool.Create<RentedOutgoingMemory>();
14-
1512
private bool _disposedValue;
1613
private byte[]? _rentedArray;
1714
private TaskCompletionSource<bool>? _sendCompletionSource;
15+
private ReadOnlySequence<byte> _data;
16+
17+
public RentedOutgoingMemory(ReadOnlyMemory<byte> data, byte[]? rentedArray = null, bool waitSend = false)
18+
: this(new ReadOnlySequence<byte>(data), rentedArray, waitSend)
19+
{
20+
}
21+
22+
public RentedOutgoingMemory(ReadOnlySequence<byte> data, byte[]? rentedArray = null, bool waitSend = false)
23+
{
24+
_data = data;
25+
_rentedArray = rentedArray;
26+
27+
if (waitSend)
28+
{
29+
_sendCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
30+
}
31+
}
1832

1933
internal int Size => (int)Data.Length;
2034

2135
public int RentedArraySize => _rentedArray?.Length ?? 0;
2236

23-
internal ReadOnlySequence<byte> Data { get; private set; }
37+
internal ReadOnlySequence<byte> Data
38+
{
39+
get
40+
{
41+
if (_disposedValue)
42+
{
43+
throw new ObjectDisposedException(nameof(RentedOutgoingMemory));
44+
}
45+
46+
return _data;
47+
}
48+
}
2449

2550
/// <summary>
2651
/// Mark the data as sent.
@@ -30,7 +55,6 @@ public void DidSend()
3055
if (_sendCompletionSource is null)
3156
{
3257
Dispose();
33-
s_pool.Return(this);
3458
}
3559
else
3660
{
@@ -50,7 +74,6 @@ async ValueTask WaitForFinishCore()
5074
{
5175
await _sendCompletionSource.Task.ConfigureAwait(false);
5276
Dispose();
53-
s_pool.Return(this);
5477
}
5578
}
5679

@@ -69,57 +92,24 @@ private void Dispose(bool disposing)
6992
return;
7093
}
7194

95+
_disposedValue = true;
96+
7297
if (disposing)
7398
{
99+
_data = default;
100+
74101
if (_rentedArray != null)
75102
{
76103
ClientArrayPool.Return(_rentedArray);
77-
Data = default;
78104
_rentedArray = null;
79105
}
80106
}
81-
82-
_disposedValue = true;
83107
}
84108

85109
public void Dispose()
86110
{
87111
Dispose(disposing: true);
88112
GC.SuppressFinalize(this);
89113
}
90-
91-
bool IResettable.TryReset()
92-
{
93-
if (!_disposedValue)
94-
{
95-
return false;
96-
}
97-
98-
_disposedValue = false;
99-
_rentedArray = default;
100-
Data = default;
101-
_sendCompletionSource = default;
102-
return true;
103-
}
104-
105-
public static RentedOutgoingMemory GetAndInitialize(ReadOnlySequence<byte> mem, byte[]? buffer = null, bool waitSend = false)
106-
{
107-
var rented = s_pool.Get();
108-
109-
rented.Data = mem;
110-
rented._rentedArray = buffer;
111-
112-
if (waitSend)
113-
{
114-
rented._sendCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
115-
}
116-
117-
return rented;
118-
}
119-
120-
public static RentedOutgoingMemory GetAndInitialize(ReadOnlyMemory<byte> mem, byte[]? buffer = null, bool waitSend = false)
121-
{
122-
return GetAndInitialize(new ReadOnlySequence<byte>(mem), buffer, waitSend);
123-
}
124114
}
125115
}

projects/RabbitMQ.Client/client/impl/Frame.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public static RentedOutgoingMemory GetHeartbeatFrame()
161161
byte[] buffer = ClientArrayPool.Rent(FrameSize);
162162
Payload.CopyTo(buffer);
163163
var mem = new ReadOnlyMemory<byte>(buffer, 0, FrameSize);
164-
return RentedOutgoingMemory.GetAndInitialize(mem, buffer);
164+
return new RentedOutgoingMemory(mem, buffer);
165165
}
166166
}
167167

@@ -177,7 +177,7 @@ public static RentedOutgoingMemory SerializeToFrames<T>(ref T method, ushort cha
177177

178178
System.Diagnostics.Debug.Assert(offset == size, $"Serialized to wrong size, expect {size}, offset {offset}");
179179
var mem = new ReadOnlyMemory<byte>(array, 0, size);
180-
return RentedOutgoingMemory.GetAndInitialize(mem, array);
180+
return new RentedOutgoingMemory(mem, array);
181181
}
182182

183183
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -219,7 +219,7 @@ public static RentedOutgoingMemory SerializeToFrames<TMethod, THeader>(ref TMeth
219219
}
220220

221221
System.Diagnostics.Debug.Assert(offset == size, $"Serialized to wrong size, expect {size}, offset {offset}");
222-
return RentedOutgoingMemory.GetAndInitialize(sequenceBuilder.Build(), array, waitSend: !copyBody);
222+
return new RentedOutgoingMemory(sequenceBuilder.Build(), array, waitSend: !copyBody);
223223
}
224224

225225
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,59 @@
1-
using RabbitMQ.Client;
1+
using System.Threading.Tasks;
2+
using RabbitMQ.Client;
23
using Xunit;
34

45
namespace Test.Unit;
56

67
public class TestRentedOutgoingMemory
78
{
89
[Fact]
9-
public void TestNonBlocking()
10+
public async Task TestNonBlocking()
1011
{
1112
// Arrange
1213
byte[] buffer = new byte[] { 1, 2, 3, 4, 5 };
13-
RentedOutgoingMemory rentedMemory = RentedOutgoingMemory.GetAndInitialize(buffer, waitSend: false);
14+
RentedOutgoingMemory rentedMemory = new RentedOutgoingMemory(buffer, waitSend: false);
1415

1516
// Act
16-
var waitTask = rentedMemory.WaitForDataSendAsync();
17+
var waitTask = rentedMemory.WaitForDataSendAsync().AsTask();
18+
var timeoutTask = Task.Delay(100);
19+
var completedTask = await Task.WhenAny(timeoutTask, waitTask);
1720

1821
// Assert
19-
Assert.True(waitTask.IsCompleted);
22+
Assert.Equal(waitTask, completedTask);
2023
}
2124

2225
[Fact]
23-
public void TestBlocking()
26+
public async Task TestBlocking()
2427
{
2528
// Arrange
2629
byte[] buffer = new byte[] { 1, 2, 3, 4, 5 };
27-
RentedOutgoingMemory rentedMemory = RentedOutgoingMemory.GetAndInitialize(buffer, waitSend: true);
30+
RentedOutgoingMemory rentedMemory = new RentedOutgoingMemory(buffer, waitSend: true);
2831

2932
// Act
30-
var waitTask = rentedMemory.WaitForDataSendAsync();
33+
var waitTask = rentedMemory.WaitForDataSendAsync().AsTask();
34+
var timeoutTask = Task.Delay(100);
35+
var completedTask = await Task.WhenAny(timeoutTask, waitTask);
3136

3237
// Assert
33-
Assert.False(waitTask.IsCompleted);
38+
Assert.Equal(timeoutTask, completedTask);
3439
}
3540

3641
[Fact]
37-
public void TestBlockingCompleted()
42+
public async Task TestBlockingCompleted()
3843
{
3944
// Arrange
4045
byte[] buffer = new byte[] { 1, 2, 3, 4, 5 };
41-
RentedOutgoingMemory rentedMemory = RentedOutgoingMemory.GetAndInitialize(buffer, waitSend: true);
46+
RentedOutgoingMemory rentedMemory = new RentedOutgoingMemory(buffer, waitSend: true);
4247

4348
// Act
44-
var waitTask = rentedMemory.WaitForDataSendAsync();
49+
var waitTask = rentedMemory.WaitForDataSendAsync().AsTask();
50+
var timeoutTask = Task.Delay(100);
4551

4652
rentedMemory.DidSend();
4753

54+
var completedTask = await Task.WhenAny(timeoutTask, waitTask);
55+
4856
// Assert
49-
Assert.False(waitTask.IsCompleted);
57+
Assert.Equal(waitTask, completedTask);
5058
}
5159
}

0 commit comments

Comments
 (0)