| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.Text; |
| | | 4 | | using System.Text.Json; |
| | | 5 | | |
| | | 6 | | namespace AsyncResponse.Transports.Kafka; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Publishes <see cref="WorkerJobEnvelope"/> messages to a Kafka topic. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// The correlation id is carried in a message header and doubles as the partition key, so all jobs |
| | | 13 | | /// of one flow stay ordered within their partition; jobs without a correlation id are spread |
| | | 14 | | /// round-robin. The producer is idempotent with <c>acks=all</c>, and transient broker failures are |
| | | 15 | | /// retried with bounded exponential backoff before the exception is returned to the caller. |
| | | 16 | | /// </remarks> |
| | | 17 | | public sealed class KafkaWorkerTransport : IWorkerTransport |
| | | 18 | | { |
| | | 19 | | private readonly KafkaAsyncResponseTransportOptions _options; |
| | | 20 | | private readonly IKafkaProducerClient _producer; |
| | | 21 | | private readonly KafkaTransportTopicSchema _topics; |
| | | 22 | | |
| | | 23 | | /// <summary>Runs the KafkaWorkerTransport operation.</summary> |
| | | 24 | | public KafkaWorkerTransport(IOptions<KafkaAsyncResponseTransportOptions> options) |
| | 2 | 25 | | : this(options, new KafkaProducerClientAdapter(KafkaTransportOptionsValidatorWithValue(options))) |
| | | 26 | | { |
| | 3 | 27 | | } |
| | | 28 | | |
| | 3 | 29 | | internal KafkaWorkerTransport( |
| | 3 | 30 | | IOptions<KafkaAsyncResponseTransportOptions> options, |
| | 3 | 31 | | IKafkaProducerClient producer) |
| | | 32 | | { |
| | 3 | 33 | | _options = options.Value; |
| | 3 | 34 | | KafkaTransportOptionsValidator.ValidateCommon(_options); |
| | 3 | 35 | | _producer = producer; |
| | 3 | 36 | | _topics = new KafkaTransportTopicSchema(_options); |
| | 3 | 37 | | } |
| | | 38 | | |
| | | 39 | | private static KafkaAsyncResponseTransportOptions KafkaTransportOptionsValidatorWithValue( |
| | | 40 | | IOptions<KafkaAsyncResponseTransportOptions> options) |
| | | 41 | | { |
| | 2 | 42 | | KafkaTransportOptionsValidator.ValidateCommon(options.Value); |
| | 3 | 43 | | return options.Value; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary>Publishes the supplied message.</summary> |
| | | 47 | | public async Task PublishAsync(WorkerJobEnvelope job, CancellationToken cancellationToken = default) |
| | | 48 | | { |
| | 3 | 49 | | ArgumentNullException.ThrowIfNull(job); |
| | | 50 | | |
| | 3 | 51 | | using var activity = AsyncResponseDiagnostics.StartActivity( |
| | 3 | 52 | | "asyncresponse.worker.publish", |
| | 3 | 53 | | ActivityKind.Producer, |
| | 3 | 54 | | job.CorrelationId); |
| | 3 | 55 | | activity?.SetTag("asyncresponse.transport", "kafka"); |
| | 3 | 56 | | activity?.SetTag("messaging.system", "kafka"); |
| | 3 | 57 | | activity?.SetTag("messaging.destination.name", _topics.WorkerTopic); |
| | 3 | 58 | | AsyncResponseDiagnostics.SetReplyTarget(activity, job.ReplyTarget); |
| | 3 | 59 | | AsyncResponseDiagnostics.SetWorker(activity, job.Call); |
| | | 60 | | |
| | | 61 | | try |
| | | 62 | | { |
| | 3 | 63 | | var payload = Encoding.UTF8.GetBytes(AsyncResponseJson.Serialize(job)); |
| | 3 | 64 | | var headers = CreateMessageHeaders(job.CorrelationId, _options); |
| | 3 | 65 | | var result = await KafkaTransportRetry.ExecuteAsync( |
| | 3 | 66 | | token => _producer.PublishAsync( |
| | 3 | 67 | | _topics.WorkerTopic, |
| | 3 | 68 | | string.IsNullOrWhiteSpace(job.CorrelationId) ? null : job.CorrelationId, |
| | 3 | 69 | | payload, |
| | 3 | 70 | | headers, |
| | 3 | 71 | | token), |
| | 3 | 72 | | _options.PublishMaxAttempts, |
| | 3 | 73 | | _options.PublishRetryBaseDelay, |
| | 3 | 74 | | _options.PublishRetryMaxDelay, |
| | 3 | 75 | | cancellationToken).ConfigureAwait(false); |
| | | 76 | | |
| | 3 | 77 | | activity?.SetTag("messaging.kafka.destination.partition", result.Partition); |
| | 3 | 78 | | activity?.SetTag("messaging.kafka.message.offset", result.Offset); |
| | 3 | 79 | | } |
| | 2 | 80 | | catch (Exception ex) |
| | | 81 | | { |
| | 2 | 82 | | AsyncResponseDiagnostics.SetError(activity, ex); |
| | 3 | 83 | | throw; |
| | | 84 | | } |
| | 3 | 85 | | } |
| | | 86 | | |
| | | 87 | | internal static IReadOnlyList<KafkaTransportHeader> CreateMessageHeaders( |
| | | 88 | | string? correlationId, |
| | | 89 | | KafkaAsyncResponseTransportOptions options) |
| | | 90 | | { |
| | 3 | 91 | | var correlationHeader = KafkaTransportOptionsValidator.Required( |
| | 3 | 92 | | options.CorrelationIdHeader, |
| | 3 | 93 | | nameof(options.CorrelationIdHeader)); |
| | | 94 | | |
| | 3 | 95 | | return string.IsNullOrWhiteSpace(correlationId) |
| | 3 | 96 | | ? [] |
| | 3 | 97 | | : [KafkaTransportHeader.Utf8(correlationHeader, correlationId)]; |
| | | 98 | | } |
| | | 99 | | } |