| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | using StackExchange.Redis; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Text.Json; |
| | | 5 | | |
| | | 6 | | namespace AsyncResponse.Transports.Redis; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Publishes <see cref="WorkerJobEnvelope"/> messages to a Redis stream. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// Redis Streams provide durable queueing and consumer-group acknowledgement. Publishing uses XADD |
| | | 13 | | /// with optional approximate trimming, and transient Redis failures are retried with bounded |
| | | 14 | | /// exponential backoff before the exception is returned to the caller. |
| | | 15 | | /// </remarks> |
| | | 16 | | public sealed class RedisWorkerTransport : IWorkerTransport |
| | | 17 | | { |
| | | 18 | | private readonly RedisAsyncResponseTransportOptions _options; |
| | | 19 | | private readonly IRedisStreamDatabase _database; |
| | | 20 | | private readonly RedisTransportKeySchema _keys; |
| | | 21 | | |
| | | 22 | | /// <summary>Runs the RedisWorkerTransport operation.</summary> |
| | | 23 | | public RedisWorkerTransport( |
| | | 24 | | IOptions<RedisAsyncResponseTransportOptions> options, |
| | | 25 | | IConnectionMultiplexer multiplexer) |
| | 3 | 26 | | : this( |
| | 3 | 27 | | options, |
| | 3 | 28 | | new RedisStreamDatabaseAdapter( |
| | 3 | 29 | | multiplexer.GetDatabase(), |
| | 3 | 30 | | RedisTransportOptionsValidatorWithValue(options).OperationTimeout)) |
| | | 31 | | { |
| | 3 | 32 | | } |
| | | 33 | | |
| | 3 | 34 | | internal RedisWorkerTransport( |
| | 3 | 35 | | IOptions<RedisAsyncResponseTransportOptions> options, |
| | 3 | 36 | | IRedisStreamDatabase database) |
| | | 37 | | { |
| | 3 | 38 | | _options = options.Value; |
| | 3 | 39 | | RedisTransportOptionsValidator.ValidateCommon(_options); |
| | 3 | 40 | | ValidatePublishOptions(_options); |
| | 3 | 41 | | _database = database; |
| | 3 | 42 | | _keys = new RedisTransportKeySchema(_options); |
| | 3 | 43 | | } |
| | | 44 | | |
| | | 45 | | private static RedisAsyncResponseTransportOptions RedisTransportOptionsValidatorWithValue( |
| | | 46 | | IOptions<RedisAsyncResponseTransportOptions> options) |
| | | 47 | | { |
| | 3 | 48 | | RedisTransportOptionsValidator.ValidateCommon(options.Value); |
| | 3 | 49 | | ValidatePublishOptions(options.Value); |
| | 3 | 50 | | return options.Value; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | private static void ValidatePublishOptions(RedisAsyncResponseTransportOptions options) |
| | | 54 | | { |
| | 3 | 55 | | _ = new RedisTransportKeySchema(options).WorkerStream; |
| | 3 | 56 | | _ = RedisTransportOptionsValidator.Required(options.PayloadField, nameof(options.PayloadField)); |
| | 3 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary>Publishes the supplied message.</summary> |
| | | 60 | | public async Task PublishAsync(WorkerJobEnvelope job, CancellationToken cancellationToken = default) |
| | | 61 | | { |
| | 3 | 62 | | ArgumentNullException.ThrowIfNull(job); |
| | | 63 | | |
| | 3 | 64 | | using var activity = AsyncResponseDiagnostics.StartActivity( |
| | 3 | 65 | | "asyncresponse.worker.publish", |
| | 3 | 66 | | ActivityKind.Producer, |
| | 3 | 67 | | job.CorrelationId); |
| | 3 | 68 | | activity?.SetTag("asyncresponse.transport", "redis"); |
| | 3 | 69 | | activity?.SetTag("messaging.system", "redis"); |
| | 3 | 70 | | activity?.SetTag("messaging.destination.name", _keys.WorkerStream.ToString()); |
| | 3 | 71 | | AsyncResponseDiagnostics.SetReplyTarget(activity, job.ReplyTarget); |
| | 3 | 72 | | AsyncResponseDiagnostics.SetWorker(activity, job.Call); |
| | | 73 | | |
| | | 74 | | try |
| | | 75 | | { |
| | 3 | 76 | | var fields = CreateMessageFields( |
| | 3 | 77 | | AsyncResponseJson.Serialize(job), |
| | 3 | 78 | | job.CorrelationId, |
| | 3 | 79 | | _options); |
| | 3 | 80 | | var messageId = await RedisTransportRetry.ExecuteAsync( |
| | 3 | 81 | | token => _database.StreamAddAsync( |
| | 3 | 82 | | _keys.WorkerStream, |
| | 3 | 83 | | fields, |
| | 3 | 84 | | _options.StreamMaxLength, |
| | 3 | 85 | | _options.UseApproximateStreamTrimming, |
| | 3 | 86 | | token), |
| | 3 | 87 | | _options.PublishMaxAttempts, |
| | 3 | 88 | | _options.PublishRetryBaseDelay, |
| | 3 | 89 | | _options.PublishRetryMaxDelay, |
| | 3 | 90 | | cancellationToken).ConfigureAwait(false); |
| | | 91 | | |
| | 3 | 92 | | activity?.SetTag("messaging.message.id", messageId.ToString()); |
| | 3 | 93 | | } |
| | 2 | 94 | | catch (Exception ex) |
| | | 95 | | { |
| | 2 | 96 | | AsyncResponseDiagnostics.SetError(activity, ex); |
| | 3 | 97 | | throw; |
| | | 98 | | } |
| | 3 | 99 | | } |
| | | 100 | | |
| | | 101 | | internal static NameValueEntry[] CreateMessageFields( |
| | | 102 | | string payloadJson, |
| | | 103 | | string? correlationId, |
| | | 104 | | RedisAsyncResponseTransportOptions options) |
| | | 105 | | { |
| | 3 | 106 | | var payloadField = RedisTransportOptionsValidator.Required(options.PayloadField, nameof(options.PayloadField)); |
| | 3 | 107 | | var correlationField = RedisTransportOptionsValidator.Required(options.CorrelationIdField, nameof(options.Correl |
| | | 108 | | |
| | 3 | 109 | | return string.IsNullOrWhiteSpace(correlationId) |
| | 3 | 110 | | ? [new NameValueEntry(payloadField, payloadJson)] |
| | 3 | 111 | | : |
| | 3 | 112 | | [ |
| | 3 | 113 | | new NameValueEntry(payloadField, payloadJson), |
| | 3 | 114 | | new NameValueEntry(correlationField, correlationId) |
| | 3 | 115 | | ]; |
| | | 116 | | } |
| | | 117 | | } |