Skip to content

Commit 1a4b8bc

Browse files
Merge pull request #1220 from rabbitmq/rabbitmq-dotnet-client-1217-6.x
Add ability to specify maximum message size
2 parents b67f4fb + da56a2c commit 1a4b8bc

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

projects/RabbitMQ.Client/client/api/AmqpTcpEndpoint.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ public IProtocol Protocol
175175
/// </summary>
176176
public SslOption Ssl { get; set; }
177177

178+
/// <summary>
179+
/// Set the maximum size for a message in bytes. The default value is 0 (unlimited)
180+
/// </summary>
181+
public uint MaxMessageSize { get; set; }
182+
178183
/// <summary>
179184
/// Construct an instance from a protocol and an address in "hostname:port" format.
180185
/// </summary>

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private static void ProcessProtocolHeader(Stream reader)
205205
}
206206
}
207207

208-
internal static InboundFrame ReadFrom(Stream reader, byte[] frameHeaderBuffer, ArrayPool<byte> pool)
208+
internal static InboundFrame ReadFrom(Stream reader, byte[] frameHeaderBuffer, ArrayPool<byte> pool, uint maxMessageSize)
209209
{
210210
int type = default;
211211
try
@@ -239,7 +239,11 @@ internal static InboundFrame ReadFrom(Stream reader, byte[] frameHeaderBuffer, A
239239

240240
reader.Read(frameHeaderBuffer, 0, frameHeaderBuffer.Length);
241241
int channel = NetworkOrderDeserializer.ReadUInt16(new ReadOnlySpan<byte>(frameHeaderBuffer));
242-
int payloadSize = NetworkOrderDeserializer.ReadInt32(new ReadOnlySpan<byte>(frameHeaderBuffer, 2, 4)); // FIXME - throw exn on unreasonable value
242+
int payloadSize = NetworkOrderDeserializer.ReadInt32(new ReadOnlySpan<byte>(frameHeaderBuffer, 2, 4));
243+
if ((maxMessageSize > 0) && (payloadSize > maxMessageSize))
244+
{
245+
throw new MalformedFrameException($"Frame payload size '{payloadSize}' exceeds maximum of '{maxMessageSize}' bytes");
246+
}
243247

244248
const int EndMarkerLength = 1;
245249
// Is returned by InboundFrame.Dispose in Connection.MainLoopIteration

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public static async Task TimeoutAfter(this Task task, TimeSpan timeout)
6262

6363
class SocketFrameHandler : IFrameHandler
6464
{
65+
private readonly AmqpTcpEndpoint _endpoint;
6566
// Socket poll timeout in ms. If the socket does not
6667
// become writeable in this amount of time, we throw
6768
// an exception.
@@ -82,7 +83,7 @@ public SocketFrameHandler(AmqpTcpEndpoint endpoint,
8283
Func<AddressFamily, ITcpClient> socketFactory,
8384
TimeSpan connectionTimeout, TimeSpan readTimeout, TimeSpan writeTimeout)
8485
{
85-
Endpoint = endpoint;
86+
_endpoint = endpoint;
8687
_frameHeaderBuffer = new byte[6];
8788
var channel = Channel.CreateUnbounded<ReadOnlyMemory<byte>>(
8889
new UnboundedChannelOptions
@@ -135,7 +136,11 @@ public SocketFrameHandler(AmqpTcpEndpoint endpoint,
135136
WriteTimeout = writeTimeout;
136137
_writerTask = Task.Run(WriteLoop, CancellationToken.None);
137138
}
138-
public AmqpTcpEndpoint Endpoint { get; set; }
139+
140+
public AmqpTcpEndpoint Endpoint
141+
{
142+
get { return _endpoint; }
143+
}
139144

140145
internal ArrayPool<byte> MemoryPool
141146
{
@@ -229,7 +234,7 @@ public void Close()
229234

230235
public InboundFrame ReadFrame()
231236
{
232-
return InboundFrame.ReadFrom(_reader, _frameHeaderBuffer, MemoryPool);
237+
return InboundFrame.ReadFrom(_reader, _frameHeaderBuffer, MemoryPool, _endpoint.MaxMessageSize);
233238
}
234239

235240
public void SendHeader()

projects/Unit/APIApproval.Approve.verified.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace RabbitMQ.Client
1313
public AmqpTcpEndpoint(string hostName, int portOrMinusOne, RabbitMQ.Client.SslOption ssl) { }
1414
public System.Net.Sockets.AddressFamily AddressFamily { get; set; }
1515
public string HostName { get; set; }
16+
public uint MaxMessageSize { get; set; }
1617
public int Port { get; set; }
1718
public RabbitMQ.Client.IProtocol Protocol { get; }
1819
public RabbitMQ.Client.SslOption Ssl { get; set; }

0 commit comments

Comments
 (0)