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 size hint variable for PublishBatch creation #888

Merged
merged 1 commit into from
Jul 5, 2020
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
6 changes: 6 additions & 0 deletions projects/RabbitMQ.Client/client/api/IModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ void BasicPublish(string exchange, string routingKey, bool mandatory,
[AmqpMethodDoNotImplement(null)]
IBasicPublishBatch CreateBasicPublishBatch();

/// <summary>
/// Creates a BasicPublishBatch instance
/// </summary>
[AmqpMethodDoNotImplement(null)]
IBasicPublishBatch CreateBasicPublishBatch(int sizeHint);

/// <summary>
/// Construct a completely empty content header for use with the Basic content class.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions projects/RabbitMQ.Client/client/impl/AutorecoveringModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1960,5 +1960,15 @@ public IBasicPublishBatch CreateBasicPublishBatch()

return ((IFullModel)_delegate).CreateBasicPublishBatch();
}

public IBasicPublishBatch CreateBasicPublishBatch(int sizeHint)
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}

return ((IFullModel)_delegate).CreateBasicPublishBatch(sizeHint);
}
}
}
14 changes: 10 additions & 4 deletions projects/RabbitMQ.Client/client/impl/BasicPublishBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,27 @@
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
//---------------------------------------------------------------------------

using System;
using System.Buffers;
using System.Collections.Generic;

using RabbitMQ.Client.Framing.Impl;

namespace RabbitMQ.Client.Impl
{
class BasicPublishBatch : IBasicPublishBatch
internal sealed class BasicPublishBatch : IBasicPublishBatch
{
private readonly List<Command> _commands = new List<Command>();
private readonly List<Command> _commands;
private readonly ModelBase _model;

internal BasicPublishBatch (ModelBase model)
{
_model = model;
_commands = new List<Command>();
}

internal BasicPublishBatch (ModelBase model, int sizeHint)
{
_model = model;
_commands = new List<Command>(sizeHint);
}

public void Add(string exchange, string routingKey, bool mandatory, IBasicProperties basicProperties, byte[] body)
Expand Down
5 changes: 5 additions & 0 deletions projects/RabbitMQ.Client/client/impl/ModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,11 @@ public IBasicPublishBatch CreateBasicPublishBatch()
return new BasicPublishBatch(this);
}

public IBasicPublishBatch CreateBasicPublishBatch(int sizeHint)
{
return new BasicPublishBatch(this, sizeHint);
}


public void ExchangeBind(string destination,
string source,
Expand Down
34 changes: 34 additions & 0 deletions projects/Unit/TestBasicPublishBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,39 @@ public void TestBasicPublishBatchSend()
BasicGetResult resultB = Model.BasicGet("test-message-batch-b", true);
Assert.NotNull(resultB);
}

[Test]
public void TestBasicPublishBatchSendWithSizeHint()
{
Model.ConfirmSelect();
Model.QueueDeclare(queue: "test-message-batch-a", durable: false);
Model.QueueDeclare(queue: "test-message-batch-b", durable: false);
IBasicPublishBatch batch = Model.CreateBasicPublishBatch(2);
batch.Add("", "test-message-batch-a", false, null, new byte [] {});
batch.Add("", "test-message-batch-b", false, null, new byte [] {});
batch.Publish();
Model.WaitForConfirmsOrDie(TimeSpan.FromSeconds(15));
BasicGetResult resultA = Model.BasicGet("test-message-batch-a", true);
Assert.NotNull(resultA);
BasicGetResult resultB = Model.BasicGet("test-message-batch-b", true);
Assert.NotNull(resultB);
}

[Test]
public void TestBasicPublishBatchSendWithWrongSizeHint()
{
Model.ConfirmSelect();
Model.QueueDeclare(queue: "test-message-batch-a", durable: false);
Model.QueueDeclare(queue: "test-message-batch-b", durable: false);
IBasicPublishBatch batch = Model.CreateBasicPublishBatch(1);
batch.Add("", "test-message-batch-a", false, null, new byte [] {});
batch.Add("", "test-message-batch-b", false, null, new byte [] {});
batch.Publish();
Model.WaitForConfirmsOrDie(TimeSpan.FromSeconds(15));
BasicGetResult resultA = Model.BasicGet("test-message-batch-a", true);
Assert.NotNull(resultA);
BasicGetResult resultB = Model.BasicGet("test-message-batch-b", true);
Assert.NotNull(resultB);
}
}
}