Skip to content

Added option to bypass FindReplyQueueForMe if you are on k8s #756

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

Merged
merged 3 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions src/Helsenorge.Messaging/MessagingServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public async Task StartAsync()
if (!await HasCommonAncestorAsync(AmqpCore.Settings.MyHerIds.ToArray()))
throw new MessagingException(
"There must be a common set of ancestor queues when receiving from multiple HER-Ids.");

if (!string.IsNullOrWhiteSpace(Settings.AmqpSettings.Synchronous.StaticReplyQueue))
{
var queueNames = new QueueNames
Expand Down Expand Up @@ -595,12 +596,18 @@ async Task IMessagingNotification.NotifyUnhandledExceptionAsync(IAmqpMessage mes

private async Task<bool> HasCommonAncestorAsync(int[] herIds)
{
if (herIds.Count() <= 1)
if (herIds.Length <= 1)
{
return true;
}

var communicationPartyDetailsList = new List<CommunicationPartyDetails>();
foreach (var herId in herIds)
communicationPartyDetailsList.Add(await AmqpCore.AddressRegistry.FindCommunicationPartyDetailsAsync(herId));
{
communicationPartyDetailsList.Add(
await AmqpCore.AddressRegistry.FindCommunicationPartyDetailsAsync(herId)
);
}

var queueNames = GetCommonAncestor(communicationPartyDetailsList);

Expand Down Expand Up @@ -638,7 +645,10 @@ private async Task<QueueNames> GetCommonAncestorAsync(IEnumerable<int> herIds)
{
var communicationPartyDetailsList = new List<CommunicationPartyDetails>();
foreach (var herId in herIds)
{
communicationPartyDetailsList.Add(await AmqpCore.AddressRegistry.FindCommunicationPartyDetailsAsync(herId));
}

return GetCommonAncestor(communicationPartyDetailsList);
}

Expand Down
17 changes: 13 additions & 4 deletions src/Helsenorge.Messaging/MessagingSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ public AmqpSettings(MessagingSettings settings)
/// Gets or set the Type of Message Broker we are using
/// </summary>
public MessageBrokerDialect MessageBrokerDialect { get; set; } = MessageBrokerDialect.RabbitMQ;

/// <summary>
/// Gets or sets the connection string
/// </summary>
Expand Down Expand Up @@ -188,7 +187,6 @@ public AmqpSettings(MessagingSettings settings)
/// If true logs every the MessageListener has finished a read call to the broker.
/// </summary>
public bool LogReadTime { get; set; } = false;

/// <summary>
/// The HER-ids we are representing.
/// </summary>
Expand Down Expand Up @@ -242,8 +240,14 @@ public class SynchronousSettings
/// The timout for synchronous calls
/// </summary>
public TimeSpan CallTimeout { get; set; } = TimeSpan.FromSeconds(15);

/// <summary>
/// Specify the reply queue you are listening on if you have implemented <see cref="Helsenorge.Messaging.Amqp.Receivers.SynchronousReplySingleQueueListener"/>
/// </summary>
public string StaticReplyQueue { get; set; }
/// <summary>
/// If you want to handle the syncreply queue separately and skip validation of the <see cref="ReplyQueueMapping"/>. Default false.
/// </summary>
public bool SingleSyncreplyQueueEnabled { get; set; } = false;

internal SynchronousSettings() { }

Expand All @@ -252,7 +256,7 @@ internal void Validate()
if (TimeToLive == TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(TimeToLive));
if (ReadTimeout == TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(ReadTimeout));
if (CallTimeout == TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(CallTimeout));

if (FindReplyQueueForMe() == null)
{
throw new InvalidOperationException("Could not determine reply to queue for this server");
Expand All @@ -268,6 +272,11 @@ internal string FindReplyQueueForMe()
where s.Equals(Environment.MachineName, StringComparison.OrdinalIgnoreCase)
select s).FirstOrDefault();

if (SingleSyncreplyQueueEnabled && server == null)
Copy link
Collaborator

@kennethmyhra kennethmyhra Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Burde man i det hele tatt forsøke å hente ReplyQueueMapping dersom SingleSyncReplyQueueEnabled er satt til true? Virker på meg som hele den funksjonaliteten burde være slått av dersom SingleSyncReplyQueueEnabled er satt til true

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oppdaterte sjekken til å skippe validering hvis den er true

{
return ReplyQueueMapping.LastOrDefault().Value;
}

return server == null ? null : ReplyQueueMapping[server];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,36 @@ public void Send_Synchronous_OK()
// message should be gone from our sync reply
Assert.AreEqual(0, MockFactory.Helsenorge.SynchronousReply.Messages.Count);
}


[TestMethod]
public void Send_Synchronous_SingleSyncreplyQueueEnabled_True_OK()
{
Settings.AmqpSettings.Synchronous.SingleSyncreplyQueueEnabled = true;
var message = CreateMessage();

// post a reply on the syncreply queue before posting the actual message
var mockMessage = CreateMockMessage(message);
mockMessage.To = MockFactory.Helsenorge.SynchronousReply.Name;
mockMessage.ReplyTo = MockFactory.OtherParty.Synchronous.Name;
mockMessage.Queue = MockFactory.Helsenorge.SynchronousReply.Messages;
MockFactory.Helsenorge.SynchronousReply.Messages.Add(mockMessage);

var response = RunAndHandleException(Client.SendAndWaitAsync(message));

var logProcessedMessage = MockLoggerProvider.Entries.Where(l => l.LogLevel == LogLevel.Information
&& l.Message.Contains($"Removing processed message {mockMessage.MessageId} from Herid {MockFactory.OtherHerId} from queue {MockFactory.Helsenorge.SynchronousReply.Name}. Correlation = {message.MessageId}"));
Assert.AreEqual(1, logProcessedMessage.Count());

var logEntry = MockLoggerProvider.Entries.Where(l => l.LogLevel == LogLevel.Information
&& l.Message.StartsWith("ResponseTime")
&& l.Message.EndsWith(" ms"));
Assert.AreEqual(1, logEntry.Count());
// make sure the content is what we expect
Assert.AreEqual(GenericResponse.ToString(), response.ToString());
// message should be gone from our sync reply
Assert.AreEqual(0, MockFactory.Helsenorge.SynchronousReply.Messages.Count);
}

[TestMethod]
[ExpectedException(typeof(MessagingException))]
public void Send_Synchronous_ErrorQueue()
Expand Down