| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | using NATS.Client.Core; |
| | | 3 | | using NATS.Net; |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Text.Json; |
| | | 6 | | |
| | | 7 | | namespace AsyncResponse.Transports.NATS; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Publishes <see cref="WorkerJobEnvelope"/> messages to a NATS JetStream subject. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// JetStream provides durable queueing and explicit acknowledgement. The correlation id travels as a |
| | | 14 | | /// message header so the consuming side can correlate without parsing the body, and transient NATS |
| | | 15 | | /// failures are retried with bounded exponential backoff before the exception is returned to the |
| | | 16 | | /// caller. |
| | | 17 | | /// </remarks> |
| | | 18 | | public sealed class NatsWorkerTransport : IWorkerTransport |
| | | 19 | | { |
| | | 20 | | private readonly NatsAsyncResponseTransportOptions _options; |
| | | 21 | | private readonly INatsJetStreamTransport _jetStream; |
| | | 22 | | private readonly NatsTransportSubjectSchema _schema; |
| | 216 | 23 | | private readonly SemaphoreSlim _ensureStreamGate = new(1, 1); |
| | | 24 | | private bool _streamEnsured; |
| | | 25 | | |
| | | 26 | | /// <summary>Runs the NatsWorkerTransport operation.</summary> |
| | | 27 | | public NatsWorkerTransport( |
| | | 28 | | IOptions<NatsAsyncResponseTransportOptions> options, |
| | | 29 | | INatsConnection connection) |
| | 196 | 30 | | : this(options, new NatsJetStreamTransportAdapter(connection.CreateJetStreamContext(), null, options.Value.Strea |
| | | 31 | | { |
| | 196 | 32 | | } |
| | | 33 | | |
| | 216 | 34 | | internal NatsWorkerTransport( |
| | 216 | 35 | | IOptions<NatsAsyncResponseTransportOptions> options, |
| | 216 | 36 | | INatsJetStreamTransport jetStream) |
| | | 37 | | { |
| | 216 | 38 | | _options = options.Value; |
| | 216 | 39 | | NatsTransportOptionsValidator.ValidateCommon(_options); |
| | 216 | 40 | | _jetStream = jetStream; |
| | 216 | 41 | | _schema = new NatsTransportSubjectSchema(_options); |
| | 216 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary>Publishes the supplied message.</summary> |
| | | 45 | | public async Task PublishAsync(WorkerJobEnvelope job, CancellationToken cancellationToken = default) |
| | | 46 | | { |
| | 438 | 47 | | ArgumentNullException.ThrowIfNull(job); |
| | | 48 | | |
| | 436 | 49 | | using var activity = AsyncResponseDiagnostics.StartActivity( |
| | 436 | 50 | | "asyncresponse.worker.publish", |
| | 436 | 51 | | ActivityKind.Producer, |
| | 436 | 52 | | job.CorrelationId); |
| | 436 | 53 | | activity?.SetTag("asyncresponse.transport", "nats"); |
| | 436 | 54 | | activity?.SetTag("messaging.system", "nats"); |
| | 436 | 55 | | activity?.SetTag("messaging.destination.name", _schema.WorkerSubject); |
| | 436 | 56 | | AsyncResponseDiagnostics.SetReplyTarget(activity, job.ReplyTarget); |
| | 436 | 57 | | AsyncResponseDiagnostics.SetWorker(activity, job.Call); |
| | | 58 | | |
| | | 59 | | try |
| | | 60 | | { |
| | 436 | 61 | | if (_options.CreateStreams) |
| | 436 | 62 | | await EnsureWorkerStreamOnceAsync(cancellationToken).ConfigureAwait(false); |
| | | 63 | | |
| | | 64 | | // Stable id outside the retry loop so a retried publish is deduplicated by JetStream |
| | | 65 | | // (Nats-Msg-Id within the stream's duplicate window) rather than enqueuing the same |
| | | 66 | | // worker job twice when a PubAck is lost after the broker already persisted the message. |
| | 432 | 67 | | var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) |
| | 432 | 68 | | { |
| | 432 | 69 | | ["Nats-Msg-Id"] = Guid.NewGuid().ToString("N") |
| | 432 | 70 | | }; |
| | 432 | 71 | | if (!string.IsNullOrWhiteSpace(job.CorrelationId)) |
| | 112 | 72 | | headers[_options.CorrelationIdHeader] = job.CorrelationId!; |
| | | 73 | | |
| | 432 | 74 | | var payload = AsyncResponseJson.Serialize(job); |
| | 432 | 75 | | var sequence = await NatsTransportRetry.ExecuteAsync( |
| | 436 | 76 | | token => _jetStream.PublishAsync(_schema.WorkerSubject, payload, headers, token), |
| | 432 | 77 | | _options.PublishMaxAttempts, |
| | 432 | 78 | | _options.PublishRetryBaseDelay, |
| | 432 | 79 | | _options.PublishRetryMaxDelay, |
| | 432 | 80 | | cancellationToken).ConfigureAwait(false); |
| | | 81 | | |
| | 430 | 82 | | activity?.SetTag("messaging.message.id", sequence); |
| | 430 | 83 | | } |
| | 6 | 84 | | catch (Exception ex) |
| | | 85 | | { |
| | 6 | 86 | | AsyncResponseDiagnostics.SetError(activity, ex); |
| | 6 | 87 | | throw; |
| | | 88 | | } |
| | 430 | 89 | | } |
| | | 90 | | |
| | | 91 | | private async Task EnsureWorkerStreamOnceAsync(CancellationToken cancellationToken) |
| | | 92 | | { |
| | 436 | 93 | | if (_streamEnsured) |
| | 223 | 94 | | return; |
| | | 95 | | |
| | 213 | 96 | | await _ensureStreamGate.WaitAsync(cancellationToken).ConfigureAwait(false); |
| | | 97 | | try |
| | | 98 | | { |
| | 213 | 99 | | if (!_streamEnsured) |
| | | 100 | | { |
| | | 101 | | // Retried on the SAME terms as the publish below: provisioning is a JetStream API |
| | | 102 | | // request like any other, and it runs when that request is likeliest to time out — |
| | | 103 | | // the first publish after startup, or after any hiccup, since the flag only |
| | | 104 | | // latches on success. |
| | 213 | 105 | | await NatsTransportRetry.ExecuteAsync( |
| | 213 | 106 | | async token => |
| | 213 | 107 | | { |
| | 224 | 108 | | await _jetStream.EnsureStreamAsync( |
| | 224 | 109 | | _schema.WorkerStream, |
| | 224 | 110 | | _schema.WorkerSubject, |
| | 224 | 111 | | _options.StreamMaxMessages, |
| | 224 | 112 | | token).ConfigureAwait(false); |
| | 209 | 113 | | return true; |
| | 209 | 114 | | }, |
| | 213 | 115 | | _options.PublishMaxAttempts, |
| | 213 | 116 | | _options.PublishRetryBaseDelay, |
| | 213 | 117 | | _options.PublishRetryMaxDelay, |
| | 213 | 118 | | cancellationToken).ConfigureAwait(false); |
| | 209 | 119 | | _streamEnsured = true; |
| | | 120 | | } |
| | 209 | 121 | | } |
| | | 122 | | finally |
| | | 123 | | { |
| | 213 | 124 | | _ensureStreamGate.Release(); |
| | | 125 | | } |
| | 432 | 126 | | } |
| | | 127 | | } |