2019-04-24 18:04:30 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Tapeti.Config;
|
2019-08-13 20:30:04 +02:00
|
|
|
|
using Tapeti.Default;
|
2019-04-24 18:04:30 +02:00
|
|
|
|
|
|
|
|
|
namespace Tapeti.Transient
|
|
|
|
|
{
|
2019-08-13 20:30:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Manages active requests and responses. For internal use.
|
|
|
|
|
/// </summary>
|
2019-08-15 11:26:55 +02:00
|
|
|
|
internal class TransientRouter
|
2019-04-24 18:04:30 +02:00
|
|
|
|
{
|
2019-04-25 15:19:51 +02:00
|
|
|
|
private readonly int defaultTimeoutMs;
|
2019-04-24 18:04:30 +02:00
|
|
|
|
private readonly ConcurrentDictionary<Guid, TaskCompletionSource<object>> map = new ConcurrentDictionary<Guid, TaskCompletionSource<object>>();
|
|
|
|
|
|
2019-08-13 20:30:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The generated name of the dynamic queue to which responses should be sent.
|
|
|
|
|
/// </summary>
|
2019-04-24 18:04:30 +02:00
|
|
|
|
public string TransientResponseQueueName { get; set; }
|
|
|
|
|
|
2019-08-13 20:30:04 +02:00
|
|
|
|
|
2021-05-29 21:51:58 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// </summary>
|
2019-04-25 15:19:51 +02:00
|
|
|
|
public TransientRouter(TimeSpan defaultTimeout)
|
2019-04-24 18:04:30 +02:00
|
|
|
|
{
|
2019-04-25 15:19:51 +02:00
|
|
|
|
defaultTimeoutMs = (int)defaultTimeout.TotalMilliseconds;
|
2019-04-24 18:04:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 20:30:04 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes incoming messages to complete the corresponding request task.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
public void HandleMessage(IMessageContext context)
|
2019-04-24 18:04:30 +02:00
|
|
|
|
{
|
|
|
|
|
if (context.Properties.CorrelationId == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!Guid.TryParse(context.Properties.CorrelationId, out var continuationID))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (map.TryRemove(continuationID, out var tcs))
|
2021-09-09 15:11:44 +02:00
|
|
|
|
tcs.TrySetResult(context.Message);
|
2019-04-24 18:04:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 20:30:04 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a request and waits for the response. Do not call directly, instead use ITransientPublisher.RequestResponse.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="publisher"></param>
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
/// <returns></returns>
|
2019-04-25 15:19:51 +02:00
|
|
|
|
public async Task<object> RequestResponse(IPublisher publisher, object request)
|
2019-04-24 18:04:30 +02:00
|
|
|
|
{
|
|
|
|
|
var correlation = Guid.NewGuid();
|
|
|
|
|
var tcs = map.GetOrAdd(correlation, c => new TaskCompletionSource<object>());
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-08-13 20:30:04 +02:00
|
|
|
|
var properties = new MessageProperties
|
2019-04-24 18:04:30 +02:00
|
|
|
|
{
|
|
|
|
|
CorrelationId = correlation.ToString(),
|
|
|
|
|
ReplyTo = TransientResponseQueueName,
|
|
|
|
|
Persistent = false
|
|
|
|
|
};
|
|
|
|
|
|
2019-04-25 15:19:51 +02:00
|
|
|
|
await ((IInternalPublisher)publisher).Publish(request, properties, false);
|
2019-04-24 18:04:30 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
// Simple cleanup of the task and map dictionary.
|
|
|
|
|
if (map.TryRemove(correlation, out tcs))
|
|
|
|
|
tcs.TrySetResult(null);
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 15:19:51 +02:00
|
|
|
|
using (new Timer(TimeoutResponse, tcs, defaultTimeoutMs, -1))
|
2019-04-24 18:04:30 +02:00
|
|
|
|
{
|
|
|
|
|
return await tcs.Task;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 20:30:04 +02:00
|
|
|
|
|
2019-04-24 18:04:30 +02:00
|
|
|
|
private void TimeoutResponse(object tcs)
|
|
|
|
|
{
|
2021-09-09 15:11:44 +02:00
|
|
|
|
((TaskCompletionSource<object>)tcs).TrySetException(new TimeoutException("Transient RequestResponse timed out at (ms) " + defaultTimeoutMs));
|
2019-04-24 18:04:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|