Skip to content

Commit

Permalink
Caching IModel instead of opening one per invocation (#149)
Browse files Browse the repository at this point in the history
Caching IModel instead of opening one per invocation.
  • Loading branch information
yojagad authored Feb 18, 2021
1 parent a91ffe7 commit 083a832
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
2 changes: 2 additions & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
### Release notes

- Cached IModel instead of opening one per connection. [#148](https://github.com/Azure/azure-functions-rabbitmq-extension/issues/148)
21 changes: 20 additions & 1 deletion src/Bindings/RabbitMQClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Options;
using RabbitMQ.Client;

Expand All @@ -11,14 +12,21 @@ internal class RabbitMQClientBuilder : IConverter<RabbitMQAttribute, IModel>
{
private readonly RabbitMQExtensionConfigProvider _configProvider;
private readonly IOptions<RabbitMQOptions> _options;
private readonly IDictionary<RabbitMQAttribute, IModel> _rabbitMQAttributeToModel;

public RabbitMQClientBuilder(RabbitMQExtensionConfigProvider configProvider, IOptions<RabbitMQOptions> options)
{
_configProvider = configProvider;
_options = options;
_rabbitMQAttributeToModel = new Dictionary<RabbitMQAttribute, IModel>();
}

public IModel Convert(RabbitMQAttribute attribute)
{
return GetOrCreateModel(attribute);
}

private IModel CreateModelFromAttribute(RabbitMQAttribute attribute)
{
if (attribute == null)
{
Expand All @@ -35,5 +43,16 @@ public IModel Convert(RabbitMQAttribute attribute)

return service.Model;
}

private IModel GetOrCreateModel(RabbitMQAttribute attribute)
{
if (!_rabbitMQAttributeToModel.TryGetValue(attribute, out IModel rabbitMQModel))
{
rabbitMQModel = CreateModelFromAttribute(attribute);
_rabbitMQAttributeToModel.Add(attribute, rabbitMQModel);
}

return rabbitMQModel;
}
}
}
}
2 changes: 1 addition & 1 deletion src/WebJobs.Extensions.RabbitMQ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<SignAssembly>false</SignAssembly>
<AssemblyOriginatorKeyFile>PublicKey.snk</AssemblyOriginatorKeyFile>
<DelaySign>true</DelaySign>
<Version>1.1.0-beta</Version>
<Version>1.0.0</Version>
<PackageId>Microsoft.Azure.WebJobs.Extensions.RabbitMQ</PackageId>
<Authors>Microsoft</Authors>
<Product>Azure WebJobs RabbitMQ Extension</Product>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using RabbitMQ.Client;
using Xunit;

namespace WebJobs.Extensions.RabbitMQ.Tests
Expand All @@ -20,28 +21,56 @@ public class RabbitMQClientBuilderTests
public void Opens_Connection()
{
var options = new OptionsWrapper<RabbitMQOptions>(new RabbitMQOptions { HostName = Constants.LocalHost });
var loggerFactory = new LoggerFactory();
var mockServiceFactory = new Mock<IRabbitMQServiceFactory>();
var mockNameResolver = new Mock<INameResolver>();
var config = new RabbitMQExtensionConfigProvider(options, mockNameResolver.Object, mockServiceFactory.Object, loggerFactory, _emptyConfig);
var mockService = new Mock<IRabbitMQService>();
var config = new RabbitMQExtensionConfigProvider(options, new Mock<INameResolver>().Object, mockServiceFactory.Object, new LoggerFactory(), _emptyConfig);
mockServiceFactory.Setup(m => m.CreateService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>())).Returns(new Mock<IRabbitMQService>().Object);
RabbitMQAttribute attr = GetTestAttribute();

RabbitMQClientBuilder clientBuilder = new RabbitMQClientBuilder(config, options);
var model = clientBuilder.Convert(attr);

mockServiceFactory.Verify(m => m.CreateService(It.IsAny<string>(), Constants.LocalHost, "guest", "guest", 5672), Times.Exactly(1));
}

mockServiceFactory.Setup(m => m.CreateService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>())).Returns(mockService.Object);
[Fact]
public void TestWhetherConnectionIsPooled()
{
var options = new OptionsWrapper<RabbitMQOptions>(new RabbitMQOptions { HostName = Constants.LocalHost });
var mockServiceFactory = new Mock<IRabbitMQServiceFactory>();
mockServiceFactory.SetupSequence(m => m.CreateService(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()))
.Returns(GetRabbitMQService())
.Returns(GetRabbitMQService());
var config = new RabbitMQExtensionConfigProvider(options, new Mock<INameResolver>().Object, mockServiceFactory.Object, new LoggerFactory(), _emptyConfig);
RabbitMQAttribute attr = GetTestAttribute();

RabbitMQClientBuilder clientBuilder = new RabbitMQClientBuilder(config, options);

RabbitMQAttribute attr = new RabbitMQAttribute
var model = clientBuilder.Convert(attr);
var model2 = clientBuilder.Convert(attr);

Assert.Equal(model, model2);
}

private RabbitMQAttribute GetTestAttribute()
{
return new RabbitMQAttribute
{
ConnectionStringSetting = string.Empty,
HostName = Constants.LocalHost,
UserName = "guest",
Password = "guest",
Port = 5672
};
}

RabbitMQClientBuilder clientBuilder = new RabbitMQClientBuilder(config, options);

var model = clientBuilder.Convert(attr);
private IRabbitMQService GetRabbitMQService()
{
var mockService = new Mock<IRabbitMQService>();
mockService.Setup(a => a.Model).Returns(
new Mock<IModel>().Object
);

mockServiceFactory.Verify(m => m.CreateService(It.IsAny<string>(), Constants.LocalHost, "guest", "guest", 5672), Times.Exactly(1));
return mockService.Object;
}
}
}
}

0 comments on commit 083a832

Please sign in to comment.