Skip to content

Instantly share code, notes, and snippets.

@janpieterz
Created November 21, 2017 13:13
Show Gist options
  • Save janpieterz/cb24b1840ba9639b31c49b1f6303172a to your computer and use it in GitHub Desktop.
Save janpieterz/cb24b1840ba9639b31c49b1f6303172a to your computer and use it in GitHub Desktop.
NServiceBus RoundRobinFailoverPartitioningStrategy
public class RoundRobinPartitioningFailoverStrategy : INamespacePartitioningStrategy
{
private readonly CircularBuffer<RuntimeNamespaceInfo[]> _namespaces;
public RoundRobinPartitioningFailoverStrategy(ReadOnlySettings settings)
{
if (!settings.TryGet("AzureServiceBus.Settings.Topology.Addressing.Namespaces",
out NamespaceConfigurations namespaces))
{
throw new ConfigurationErrorsException($"The '{nameof(RoundRobinPartitioningFailoverStrategy)}' strategy requires exactly two namespaces to be configured, please use {nameof(AzureServiceBusTransportExtensions.NamespacePartitioning)}().{nameof(AzureServiceBusNamespacePartitioningSettings.AddNamespace)}() to register the namespaces.");
}
var partitioningNamespaces = namespaces.Where(x => x.Purpose == NamespacePurpose.Partitioning).ToList();
if (partitioningNamespaces.Count != 2)
{
throw new ConfigurationErrorsException($"The '{nameof(RoundRobinPartitioningFailoverStrategy)}' strategy requires exactly two namespaces to be configured, please use {nameof(AzureServiceBusTransportExtensions.NamespacePartitioning)}().{nameof(AzureServiceBusNamespacePartitioningSettings.AddNamespace)}() to register the namespaces.");
}
_namespaces = new CircularBuffer<RuntimeNamespaceInfo[]>(partitioningNamespaces.Count);
var first = namespaces.First();
var second = namespaces.Last();
_namespaces.Put(new []
{
new RuntimeNamespaceInfo(first.Alias, first.ConnectionString, first.Purpose, NamespaceMode.Active),
new RuntimeNamespaceInfo(second.Alias, second.ConnectionString, second.Purpose, NamespaceMode.Passive),
});
_namespaces.Put(new []
{
new RuntimeNamespaceInfo(first.Alias, first.ConnectionString, first.Purpose, NamespaceMode.Passive),
new RuntimeNamespaceInfo(second.Alias, second.ConnectionString, second.Purpose, NamespaceMode.Active),
});
}
public IEnumerable<RuntimeNamespaceInfo> GetNamespaces(PartitioningIntent partitioningIntent)
{
return _namespaces.Get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment