Skip to content

Commit

Permalink
Merge pull request #1943 from preardon/GetProducerByTopicOrDefault
Browse files Browse the repository at this point in the history
Add LookupByOrDefault to the Producer Registry to allow setting Default
  • Loading branch information
preardon authored Jan 25, 2022
2 parents 4d0d944 + 28bbcaa commit 3bd423d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Paramore.Brighter/ExternalBusServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ internal void ClearOutbox(params Guid[] posts)

s_logger.LogInformation("Decoupled invocation of message: Topic:{Topic} Id:{Id}", message.Header.Topic, messageId.ToString());

var producer = ProducerRegistry.LookupBy(message.Header.Topic);
var producer = ProducerRegistry.LookupByOrDefault(message.Header.Topic);

if (producer is IAmAMessageProducerSync producerSync)
{
Expand Down Expand Up @@ -168,7 +168,7 @@ internal async Task ClearOutboxAsync(IEnumerable<Guid> posts, bool continueOnCap

s_logger.LogInformation("Decoupled invocation of message: Topic:{Topic} Id:{Id}", message.Header.Topic, messageId.ToString());

var producer = ProducerRegistry.LookupBy(message.Header.Topic);
var producer = ProducerRegistry.LookupByOrDefault(message.Header.Topic);

if (producer is IAmAMessageProducerAsync producerAsync)
{
Expand Down Expand Up @@ -339,7 +339,7 @@ internal async Task<bool> RetryAsync(Func<CancellationToken, Task> send, bool co
internal void CallViaExternalBus<T, TResponse>(Message outMessage) where T : class, ICall where TResponse : class, IResponse
{
//We assume that this only occurs over a blocking producer
var producer = ProducerRegistry.LookupBy(outMessage.Header.Topic);
var producer = ProducerRegistry.LookupByOrDefault(outMessage.Header.Topic);
if (producer is IAmAMessageProducerSync producerSync)
Retry(() => producerSync.Send(outMessage));
}
Expand Down
7 changes: 7 additions & 0 deletions src/Paramore.Brighter/IAmAProducerRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public interface IAmAProducerRegistry : IDisposable
/// <returns>A producer</returns>
IAmAMessageProducer LookupBy(string topic);

/// <summary>
/// Looks up the producer associated with this message via a topic or returns the default producer. The topic lives on the message headers
/// </summary>
/// <param name="topic">The topic we want to find the producer for</param>
/// <returns>A producer</returns>
IAmAMessageProducer LookupByOrDefault(string topic);

/// <summary>
/// An iterable list of all the producers in the registry
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Paramore.Brighter/ProducerRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ public IAmAMessageProducer LookupBy(string topic)
return _messageProducers[topic];
}

/// <summary>
/// Looks up the producer associated with this message via a topic or returns the default producer. The topic lives on the message headers
/// </summary>
/// <param name="topic">The topic we want to find the producer for</param>
/// <returns>A producer</returns>
public IAmAMessageProducer LookupByOrDefault(string topic)
{
if (_messageProducers.ContainsKey(topic)) return LookupBy(topic);

return GetDefaultProducer();
}

/// <summary>
/// An iterable list of all the producers in the registry
/// </summary>
Expand Down

0 comments on commit 3bd423d

Please sign in to comment.