Esempio n. 1
0
            public async Task Send(ProducerContext context)
            {
                if (_messages == null)
                {
                    throw new ArgumentNullException(nameof(_messages));
                }

                LogContext.SetCurrentIfNull(_context.LogContext);

                EventHubMessageSendContext <T>[] contexts = _messages
                                                            .Select(x => new EventHubMessageSendContext <T>(x, _cancellationToken)
                {
                    Serializer = context.Serializer
                })
                                                            .ToArray();

                if (contexts.Length == 0)
                {
                    return;
                }

                NewId[] ids = NewId.Next(contexts.Length);

                async Task SendInner(EventHubMessageSendContext <T> c, int idx)
                {
                    c.DestinationAddress = _context.EndpointAddress;

                    await _context.SendPipe.Send(c).ConfigureAwait(false);

                    if (_pipe.IsNotEmpty())
                    {
                        await _pipe.Send(c).ConfigureAwait(false);
                    }

                    c.SourceAddress ??= _context.HostAddress;
                    c.ConversationId ??= ids[idx].ToGuid();
                }

                await Task.WhenAll(contexts.Select(SendInner)).ConfigureAwait(false);

                EventHubMessageSendContext <T> sendContext = contexts[0];
                var options = new CreateBatchOptions
                {
                    PartitionId  = sendContext.PartitionId,
                    PartitionKey = sendContext.PartitionKey
                };

                StartedActivity?activity = LogContext.IfEnabled(OperationName.Transport.Send)?.StartSendActivity(sendContext,
                                                                                                                 (nameof(EventHubMessageSendContext <T> .PartitionId), options.PartitionId),
                                                                                                                 (nameof(EventHubMessageSendContext <T> .PartitionKey), options.PartitionKey));

                try
                {
                    var eventDataBatch = await context.CreateBatch(options, context.CancellationToken).ConfigureAwait(false);

                    if (_context.SendObservers.Count > 0)
                    {
                        await Task.WhenAll(contexts.Select(c => _context.SendObservers.PreSend(c))).ConfigureAwait(false);
                    }

                    async Task FlushAsync(EventDataBatch batch)
                    {
                        await context.Produce(batch, context.CancellationToken).ConfigureAwait(false);

                        batch.Dispose();
                    }

                    for (var i = 0; i < contexts.Length; i++)
                    {
                        EventHubMessageSendContext <T> c = contexts[i];

                        var eventData = new EventData(c.Body);

                        eventData.Properties.Set(c.Headers);

                        while (!eventDataBatch.TryAdd(eventData) && eventDataBatch.Count > 0)
                        {
                            await FlushAsync(eventDataBatch);

                            eventDataBatch = await context.CreateBatch(options, context.CancellationToken).ConfigureAwait(false);
                        }
                    }

                    if (eventDataBatch.Count > 0)
                    {
                        await FlushAsync(eventDataBatch);
                    }

                    sendContext.LogSent();
                    activity.AddSendContextHeadersPostSend(sendContext);

                    if (_context.SendObservers.Count > 0)
                    {
                        await Task.WhenAll(contexts.Select(c => _context.SendObservers.PostSend(c))).ConfigureAwait(false);
                    }
                }
                catch (Exception exception)
                {
                    sendContext.LogFaulted(exception);

                    if (_context.SendObservers.Count > 0)
                    {
                        await Task.WhenAll(contexts.Select(c => _context.SendObservers.SendFault(c, exception))).ConfigureAwait(false);
                    }

                    throw;
                }
                finally
                {
                    activity?.Stop();
                }
            }
Esempio n. 2
0
            public async Task Send(ProducerContext context)
            {
                LogContext.SetCurrentIfNull(_context.LogContext);

                var sendContext = new EventHubMessageSendContext <T>(_message, _cancellationToken)
                {
                    Serializer         = context.Serializer,
                    DestinationAddress = _context.EndpointAddress
                };

                await _context.SendPipe.Send(sendContext).ConfigureAwait(false);

                if (_pipe.IsNotEmpty())
                {
                    await _pipe.Send(sendContext).ConfigureAwait(false);
                }

                sendContext.SourceAddress ??= _context.HostAddress;
                sendContext.ConversationId ??= NewId.NextGuid();

                var options = new SendEventOptions
                {
                    PartitionId  = sendContext.PartitionId,
                    PartitionKey = sendContext.PartitionKey
                };

                StartedActivity?activity = LogContext.IfEnabled(OperationName.Transport.Send)?.StartSendActivity(sendContext,
                                                                                                                 (nameof(sendContext.PartitionId), options.PartitionId), (nameof(sendContext.PartitionKey), options.PartitionKey));

                try
                {
                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.PreSend(sendContext).ConfigureAwait(false);
                    }

                    var eventData = new EventData(sendContext.Body);

                    eventData.Properties.Set(sendContext.Headers);

                    await context.Produce(new[] { eventData }, options, sendContext.CancellationToken).ConfigureAwait(false);

                    sendContext.LogSent();
                    activity.AddSendContextHeadersPostSend(sendContext);

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.PostSend(sendContext).ConfigureAwait(false);
                    }
                }
                catch (Exception exception)
                {
                    sendContext.LogFaulted(exception);

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.SendFault(sendContext, exception).ConfigureAwait(false);
                    }

                    throw;
                }
                finally
                {
                    activity?.Stop();
                }
            }
Esempio n. 3
0
            public async Task Send(ProducerContext <TKey, TValue> context)
            {
                LogContext.SetCurrentIfNull(_context.LogContext);

                var sendContext = new KafkaMessageSendContext <TKey, TValue>(_key, _value, _cancellationToken)
                {
                    DestinationAddress = _context.TopicAddress
                };

                await _context.SendPipe.Send(sendContext).ConfigureAwait(false);

                if (_pipe.IsNotEmpty())
                {
                    await _pipe.Send(sendContext).ConfigureAwait(false);
                }

                sendContext.SourceAddress ??= _context.HostAddress;
                sendContext.ConversationId ??= NewId.NextGuid();

                StartedActivity?activity = LogContext.IfEnabled(OperationName.Transport.Send)?.StartSendActivity(sendContext,
                                                                                                                 (nameof(sendContext.Partition), sendContext.Partition.ToString()));

                try
                {
                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.PreSend(sendContext).ConfigureAwait(false);
                    }

                    var message = new Message <TKey, TValue>
                    {
                        Key   = sendContext.Key,
                        Value = sendContext.Message
                    };

                    if (sendContext.SentTime.HasValue)
                    {
                        message.Timestamp = new Timestamp(sendContext.SentTime.Value);
                    }

                    message.Headers = context.HeadersSerializer.Serialize(sendContext);

                    var topic = new TopicPartition(_context.TopicAddress.Topic, sendContext.Partition);

                    await context.Produce(topic, message, context.CancellationToken).ConfigureAwait(false);

                    sendContext.LogSent();
                    activity.AddSendContextHeadersPostSend(sendContext);

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.PostSend(sendContext).ConfigureAwait(false);
                    }
                }
                catch (Exception exception)
                {
                    sendContext.LogFaulted(exception);

                    if (_context.SendObservers.Count > 0)
                    {
                        await _context.SendObservers.SendFault(sendContext, exception).ConfigureAwait(false);
                    }

                    throw;
                }
                finally
                {
                    activity?.Stop();
                }
            }
Esempio n. 4
0
 public Task Produce(EventDataBatch eventDataBatch, CancellationToken cancellationToken)
 {
     return(_context.Produce(eventDataBatch, cancellationToken));
 }
Esempio n. 5
0
 public async Task Produce(TopicPartition partition, Message <TKey, TValue> message, CancellationToken cancellationToken)
 {
     await _context.Produce(partition, message, cancellationToken);
 }