Ejemplo 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();
                }
            }
Ejemplo n.º 2
0
 public ValueTask <EventDataBatch> CreateBatch(CreateBatchOptions options, CancellationToken cancellationToken)
 {
     return(_context.CreateBatch(options, cancellationToken));
 }