| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.ServiceModel; |
| | | 6 | | using System.ServiceModel.Channels; |
| | | 7 | | using System.Text.RegularExpressions; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using Confluent.Kafka; |
| | | 11 | | using CoreWCF.Runtime; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.ServiceModel.Channels |
| | | 14 | | { |
| | | 15 | | internal class KafkaOutputChannel : ChannelBase, IOutputChannel |
| | | 16 | | { |
| | | 17 | | private readonly MessageEncoder _encoder; |
| | | 18 | | private readonly KafkaChannelFactory _parent; |
| | | 19 | | private readonly string _topic; |
| | | 20 | | private readonly ProducerBuilder<byte[], byte[]> _producerBuilder; |
| | 1 | 21 | | private static readonly Regex s_topicNameRegex = |
| | 1 | 22 | | new(@"^[a-zA-Z0-9\.\-_]{1,255}$", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100)); |
| | | 23 | | private IProducer<byte[], byte[]> _producer; |
| | | 24 | | |
| | | 25 | | public KafkaOutputChannel(KafkaChannelFactory factory, EndpointAddress address, Uri via, MessageEncoder encoder, |
| | 38 | 26 | | KafkaTransportBindingElement transportBindingElement) : base(factory) |
| | | 27 | | { |
| | 38 | 28 | | _parent = factory; |
| | 38 | 29 | | RemoteAddress = address ?? throw new ArgumentNullException(nameof(address)); |
| | 38 | 30 | | Via = via ?? throw new ArgumentNullException(nameof(via)); |
| | 38 | 31 | | _encoder = encoder ?? throw new ArgumentNullException(nameof(encoder)); |
| | | 32 | | |
| | 38 | 33 | | if (!string.Equals(address.Uri.Scheme, KafkaConstants.Scheme, StringComparison.OrdinalIgnoreCase)) |
| | | 34 | | { |
| | 0 | 35 | | throw new InvalidOperationException(); |
| | | 36 | | } |
| | | 37 | | |
| | 38 | 38 | | string bootstrapServer = address.Uri.Authority; |
| | 38 | 39 | | _topic = address.Uri.PathAndQuery.Substring(1, address.Uri.PathAndQuery.Length - 1); |
| | 38 | 40 | | if (string.IsNullOrEmpty(_topic) || !s_topicNameRegex.IsMatch(_topic)) |
| | | 41 | | { |
| | 0 | 42 | | throw new NotSupportedException(string.Format(SR.InvalidTopicName, _topic)); |
| | | 43 | | } |
| | 38 | 44 | | ProducerConfig producerConfig = transportBindingElement.Config; |
| | 38 | 45 | | producerConfig.BootstrapServers = bootstrapServer; |
| | | 46 | | |
| | 38 | 47 | | _producerBuilder = new ProducerBuilder<byte[], byte[]>(producerConfig); |
| | 38 | 48 | | } |
| | | 49 | | |
| | | 50 | | protected override void OnOpen(TimeSpan timeout) |
| | | 51 | | { |
| | 21 | 52 | | _producer = _producerBuilder.Build(); |
| | 21 | 53 | | } |
| | | 54 | | |
| | | 55 | | private Task OnOpenAsync() |
| | | 56 | | { |
| | 17 | 57 | | _producer = _producerBuilder.Build(); |
| | 17 | 58 | | return Task.CompletedTask; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state) |
| | | 62 | | { |
| | 17 | 63 | | return OnOpenAsync().ToApm(callback, state); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | protected override void OnEndOpen(IAsyncResult result) |
| | | 67 | | { |
| | 17 | 68 | | result.ToApmEnd(); |
| | 17 | 69 | | } |
| | | 70 | | |
| | | 71 | | protected override void OnAbort() |
| | | 72 | | { |
| | 0 | 73 | | OnClose(DefaultCloseTimeout); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | protected override void OnClose(TimeSpan timeout) |
| | | 77 | | { |
| | 12 | 78 | | CancellationTokenSource cts = new(timeout); |
| | | 79 | | try |
| | | 80 | | { |
| | 12 | 81 | | _producer.Flush(cts.Token); |
| | 12 | 82 | | } |
| | | 83 | | finally |
| | | 84 | | { |
| | 12 | 85 | | cts.Dispose(); |
| | 12 | 86 | | _producer.Dispose(); |
| | 12 | 87 | | } |
| | 12 | 88 | | } |
| | | 89 | | |
| | | 90 | | private Task OnCloseAsync(TimeSpan timeout) |
| | | 91 | | { |
| | 1 | 92 | | OnClose(timeout); |
| | 1 | 93 | | return Task.CompletedTask; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state) |
| | | 97 | | { |
| | 1 | 98 | | return OnCloseAsync(timeout).ToApm(callback, state); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | protected override void OnEndClose(IAsyncResult result) |
| | | 102 | | { |
| | 1 | 103 | | result.ToApmEnd(); |
| | 1 | 104 | | } |
| | | 105 | | |
| | | 106 | | private ArraySegment<byte> EncodeMessage(Message message) |
| | | 107 | | { |
| | | 108 | | try |
| | | 109 | | { |
| | 472 | 110 | | RemoteAddress.ApplyTo(message); |
| | 472 | 111 | | return _encoder.WriteMessage(message, int.MaxValue, _parent.BufferManager); |
| | | 112 | | } |
| | | 113 | | finally |
| | | 114 | | { |
| | 472 | 115 | | message.Close(); |
| | 472 | 116 | | } |
| | 472 | 117 | | } |
| | | 118 | | |
| | | 119 | | private async Task SendAsync(Message message, TimeSpan timeout) |
| | | 120 | | { |
| | 472 | 121 | | Message<byte[], byte[]> kafkaMessage = new(); |
| | 472 | 122 | | ApplyKafkaMessageProperty(message, kafkaMessage); |
| | 472 | 123 | | ArraySegment<byte> messageBuffer = EncodeMessage(message); |
| | 472 | 124 | | CancellationTokenSource cts = new(timeout); |
| | | 125 | | try |
| | | 126 | | { |
| | 472 | 127 | | kafkaMessage.Value = new Span<byte>(messageBuffer.Array, messageBuffer.Offset, messageBuffer.Count).ToAr |
| | 472 | 128 | | await _producer.ProduceAsync(_topic, kafkaMessage, cts.Token); |
| | 470 | 129 | | } |
| | | 130 | | catch (KafkaException kafkaException) |
| | | 131 | | { |
| | 1 | 132 | | throw KafkaChannelHelpers.ConvertKafkaException(kafkaException); |
| | | 133 | | } |
| | 1 | 134 | | catch (OperationCanceledException) |
| | | 135 | | { |
| | 1 | 136 | | throw new TimeoutException(string.Format(SR.KafkaSendTimeoutExceeded, timeout)); |
| | | 137 | | } |
| | | 138 | | finally |
| | | 139 | | { |
| | 472 | 140 | | cts.Dispose(); |
| | 472 | 141 | | _parent.BufferManager.ReturnBuffer(messageBuffer.Array); |
| | | 142 | | } |
| | 470 | 143 | | } |
| | | 144 | | |
| | | 145 | | private static void ApplyKafkaMessageProperty(Message message, Message<byte[], byte[]> kafkaMessage) |
| | | 146 | | { |
| | 472 | 147 | | KafkaMessageProperty kafkaMessageProperty = |
| | 472 | 148 | | message.Properties.TryGetValue(KafkaMessageProperty.Name, out object value) && |
| | 472 | 149 | | value is KafkaMessageProperty property |
| | 472 | 150 | | ? property |
| | 472 | 151 | | : new KafkaMessageProperty(); |
| | 472 | 152 | | kafkaMessage.Headers = new(); |
| | 948 | 153 | | foreach (KafkaMessageHeader kafkaMessageHeader in kafkaMessageProperty.Headers) |
| | | 154 | | { |
| | 2 | 155 | | kafkaMessage.Headers.Add(kafkaMessageHeader.Key, kafkaMessageHeader.Value); |
| | | 156 | | } |
| | | 157 | | |
| | 472 | 158 | | kafkaMessage.Key = kafkaMessageProperty.PartitionKey; |
| | 472 | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | public void Send(Message message) => Send(message, DefaultSendTimeout); |
| | | 162 | | |
| | | 163 | | public void Send(Message message, TimeSpan timeout) |
| | | 164 | | { |
| | 455 | 165 | | SendAsync(message, timeout).GetAwaiter().GetResult(); |
| | 455 | 166 | | } |
| | | 167 | | |
| | | 168 | | public IAsyncResult BeginSend(Message message, AsyncCallback callback, object state) |
| | | 169 | | { |
| | 17 | 170 | | return SendAsync(message, DefaultSendTimeout).ToApm(callback, state); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | public IAsyncResult BeginSend(Message message, TimeSpan timeout, AsyncCallback callback, object state) |
| | | 174 | | { |
| | 17 | 175 | | return BeginSend(message, callback, state); |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | public void EndSend(IAsyncResult result) |
| | | 179 | | { |
| | 17 | 180 | | result.ToApmEnd(); |
| | 15 | 181 | | } |
| | | 182 | | |
| | 472 | 183 | | public EndpointAddress RemoteAddress { get; } |
| | | 184 | | |
| | 0 | 185 | | public Uri Via { get; } |
| | | 186 | | |
| | | 187 | | public override T GetProperty<T>() |
| | | 188 | | { |
| | 0 | 189 | | if (typeof(T) == typeof(IOutputChannel)) |
| | | 190 | | { |
| | 0 | 191 | | return (T)(object)this; |
| | | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | T messageEncoderProperty = _encoder.GetProperty<T>(); |
| | 0 | 195 | | if (messageEncoderProperty != null) |
| | | 196 | | { |
| | 0 | 197 | | return messageEncoderProperty; |
| | | 198 | | } |
| | | 199 | | |
| | 0 | 200 | | return base.GetProperty<T>(); |
| | | 201 | | } |
| | | 202 | | } |
| | | 203 | | } |
| | | 204 | | |