Ejemplo n.º 1
0
        Task PublishInternal <T>(CancellationToken cancellationToken, object values, IPipe <PublishContext <T> > pipe = default)
            where T : class
        {
            Task <ISendEndpoint> sendEndpointTask = GetPublishSendEndpoint <T>();

            if (sendEndpointTask.IsCompletedSuccessfully())
            {
                var sendEndpoint = sendEndpointTask.Result;

                return(pipe.IsNotEmpty()
                    ? sendEndpoint.Send(values, new PublishSendPipeAdapter <T>(pipe), cancellationToken)
                    : sendEndpoint.Send <T>(values, cancellationToken));
            }

            async Task PublishAsync()
            {
                var sendEndpoint = await sendEndpointTask.ConfigureAwait(false);

                if (pipe.IsNotEmpty())
                {
                    await sendEndpoint.Send(values, new PublishSendPipeAdapter <T>(pipe), cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    await sendEndpoint.Send <T>(values, cancellationToken).ConfigureAwait(false);
                }
            }

            return(PublishAsync());
        }
Ejemplo n.º 2
0
        Task RespondInternal <T>(T message, IPipe <SendContext <T> > pipe = default)
            where T : class
        {
            Task <ISendEndpoint> sendEndpointTask = this.GetResponseEndpoint <T>();

            if (sendEndpointTask.IsCompletedSuccessfully())
            {
                var sendEndpoint = sendEndpointTask.Result;

                return(pipe.IsNotEmpty()
                    ? sendEndpoint.Send(message, pipe, CancellationToken)
                    : sendEndpoint.Send(message, CancellationToken));
            }

            async Task RespondAsync()
            {
                var sendEndpoint = await sendEndpointTask.ConfigureAwait(false);

                if (pipe.IsNotEmpty())
                {
                    await sendEndpoint.Send(message, pipe, CancellationToken).ConfigureAwait(false);
                }
                else
                {
                    await sendEndpoint.Send(message, CancellationToken).ConfigureAwait(false);
                }
            }

            return(RespondAsync());
        }
Ejemplo n.º 3
0
            public async Task Send(SendContext <T> context)
            {
                context.Serializer         = _endpoint.Serializer;
                context.DestinationAddress = _endpoint.DestinationAddress;

                if (context.SourceAddress == null)
                {
                    context.SourceAddress = _endpoint.SourceAddress;
                }

                if (_pipe is ISendContextPipe sendContextPipe)
                {
                    await sendContextPipe.Send(context).ConfigureAwait(false);
                }

                if (_endpoint._sendPipe != null)
                {
                    await _endpoint._sendPipe.Send(context).ConfigureAwait(false);
                }

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

                if (!context.ConversationId.HasValue)
                {
                    context.ConversationId = NewId.NextGuid();
                }
            }
Ejemplo n.º 4
0
        public async Task Send(PublishContext <T> context)
        {
            _context = context;

            context.SetScheduledEnqueueTime(_scheduledTime);

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

            if (_sendPipe.IsNotEmpty())
            {
                await _sendPipe.Send(context).ConfigureAwait(false);
            }
        }
        public async Task Send(SendContext <T> context)
        {
            context.SourceAddress = _sourceAddress;

            if (_consumeContext != null)
            {
                context.TransferConsumeContextHeaders(_consumeContext);
            }

            var publishContext = context.GetPayload <PublishContext <T> >();

            var firstTime = Interlocked.CompareExchange(ref _context, publishContext, null) == null;

            await _publishPipe.Send(publishContext).ConfigureAwait(false);

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

            if (firstTime)
            {
                await _observer.PrePublish(publishContext).ConfigureAwait(false);
            }
        }
Ejemplo n.º 6
0
            public async Task Send(SendContext <ScheduleRecurringMessage> context)
            {
                if (_pipe.IsNotEmpty())
                {
                    SendContext <T> proxy = context.CreateProxy(_payload);

                    await _pipe.Send(proxy).ConfigureAwait(false);
                }
            }
Ejemplo n.º 7
0
            public async Task Send(PublishContext <ScheduleRecurringMessage> context)
            {
                if (_pipe.IsNotEmpty())
                {
                    var proxy = new PublishContextProxy <T>(context, _payload);

                    await _pipe.Send(proxy).ConfigureAwait(false);
                }
            }
Ejemplo n.º 8
0
            public Task Send(SendContext <TMessage> context)
            {
                if (_context != null)
                {
                    var instanceContext = _context;
                    context.GetOrAddPayload(() => instanceContext);
                }

                return(_pipe.IsNotEmpty() ? _pipe.Send(context) : TaskUtil.Completed);
            }
Ejemplo n.º 9
0
        public Task Send(SendContext <T> context)
        {
            if (_delay > TimeSpan.Zero)
            {
                var delaySendContext = context.GetPayload <DelaySendContext>();
                delaySendContext.Delay = _delay;
            }

            return(_pipe.IsNotEmpty() ? _pipe.Send(context) : Task.CompletedTask);
        }
Ejemplo n.º 10
0
            public async Task Send(SendContext <T> context)
            {
                var publishContext = context.GetPayload <PublishContext <T> >();

                await _publishPipe.Send(publishContext).ConfigureAwait(false);

                if (_pipe.IsNotEmpty())
                {
                    await _pipe.Send(context).ConfigureAwait(false);
                }
            }
Ejemplo n.º 11
0
        public Task Send(SendContext context)
        {
            context.RequestId     = _context.RequestId;
            context.SourceAddress = _context.ReceiveContext.InputAddress;

            if (_pipe.IsNotEmpty())
            {
                return(_pipe.Send(context));
            }

            return(TaskUtil.Completed);
        }
Ejemplo n.º 12
0
        public async Task Send(SendContext <T> context)
        {
            if (_consumeContext != null)
            {
                context.TransferConsumeContextHeaders(_consumeContext);
            }

            if (_pipe.IsNotEmpty())
            {
                await _pipe.Send(context).ConfigureAwait(false);
            }
        }
Ejemplo n.º 13
0
        IPipe <ReceiveContext> CreateDeadLetterPipe()
        {
            IPipe <ReceiveContext> deadLetterPipe = DeadLetterConfigurator.Build();

            if (deadLetterPipe.IsNotEmpty())
            {
                return(deadLetterPipe);
            }

            DeadLetterConfigurator.UseFilter(new DeadLetterTransportFilter());

            return(DeadLetterConfigurator.Build());
        }
Ejemplo n.º 14
0
        protected async Task RespondInternal <T>(object values, IPipe <SendContext <T> > pipe = default)
            where T : class
        {
            var responseEndpoint = await GetResponseEndpoint <T>().ConfigureAwait(false);

            if (pipe.IsNotEmpty())
            {
                await responseEndpoint.Send(values, pipe, CancellationToken).ConfigureAwait(false);
            }
            else
            {
                await responseEndpoint.Send <T>(values, CancellationToken).ConfigureAwait(false);
            }
        }
Ejemplo n.º 15
0
            public async Task Send(SendContext <TMessage> context)
            {
                await Task.WhenAll(_initializers.Select(x => x.Apply(_context, context))).ConfigureAwait(false);

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

                if (_sendPipe.IsNotEmpty())
                {
                    await _sendPipe.Send(context).ConfigureAwait(false);
                }
            }
Ejemplo n.º 16
0
        IPipe <ExceptionReceiveContext> CreateErrorPipe()
        {
            IPipe <ExceptionReceiveContext> errorPipe = ErrorConfigurator.Build();

            if (errorPipe.IsNotEmpty())
            {
                return(errorPipe);
            }

            ErrorConfigurator.UseFilter(new GenerateFaultFilter());
            ErrorConfigurator.UseFilter(new ErrorTransportFilter());

            return(ErrorConfigurator.Build());
        }
Ejemplo n.º 17
0
        async Task PublishInternal <T>(CancellationToken cancellationToken, T message, IPipe <PublishContext <T> > pipe = default)
            where T : class
        {
            var sendEndpoint = await _endpoint.GetPublishSendEndpoint <T>().ConfigureAwait(false);

            if (pipe.IsNotEmpty())
            {
                await sendEndpoint.Send(message, new PublishPipe <T>(pipe), cancellationToken).ConfigureAwait(false);
            }
            else
            {
                await sendEndpoint.Send(message, cancellationToken).ConfigureAwait(false);
            }
        }
Ejemplo n.º 18
0
        async Task PublishInternal <T>(CancellationToken cancellationToken, object values, IPipe <PublishContext <T> > pipe = default)
            where T : class
        {
            var sendEndpoint = await _endpoint.GetPublishSendEndpoint <T>().ConfigureAwait(false);

            if (pipe.IsNotEmpty())
            {
                await sendEndpoint.Send(values, new PublishSendPipeAdapter <T>(pipe), cancellationToken).ConfigureAwait(false);
            }
            else
            {
                await sendEndpoint.Send <T>(values, cancellationToken).ConfigureAwait(false);
            }
        }
Ejemplo n.º 19
0
        void CreateSendEndpointContext(IAsyncPipeContextAgent <SendEndpointContext> asyncContext, CancellationToken cancellationToken)
        {
            async Task <SendEndpointContext> Create(ConnectionContext context, CancellationToken createCancellationToken)
            {
                var sendEndpointContext = CreateSendEndpointContext(context);

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

                return(sendEndpointContext);
            }

            _supervisor.CreateAgent(asyncContext, Create, cancellationToken);
        }
Ejemplo n.º 20
0
        public async Task Send(SendContext <T> context)
        {
            context.MessageId       = _context.MessageId;
            context.RequestId       = _context.RequestId;
            context.ConversationId  = _context.ConversationId;
            context.CorrelationId   = _context.CorrelationId;
            context.InitiatorId     = _context.InitiatorId;
            context.SourceAddress   = _context.SourceAddress;
            context.ResponseAddress = _context.ResponseAddress;
            context.FaultAddress    = _context.FaultAddress;

            if (_context.ExpirationTime.HasValue)
            {
                context.TimeToLive = _context.ExpirationTime.Value.ToUniversalTime() - DateTime.UtcNow;
            }

            foreach (KeyValuePair <string, object> header in _context.Headers.GetAll())
            {
                context.Headers.Set(header.Key, header.Value);
            }

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

            var forwarderAddress = _context.ReceiveContext.InputAddress ?? _context.DestinationAddress;

            if (forwarderAddress != null)
            {
                context.Headers.Set(MessageHeaders.ForwarderAddress, forwarderAddress.ToString());
            }

            if (JsonMessageSerializer.JsonContentType.Equals(_context.ReceiveContext.ContentType))
            {
                context.Serializer = new ForwardJsonMessageSerializer(_context.ReceiveContext);
            }
            else if (XmlMessageSerializer.XmlContentType.Equals(_context.ReceiveContext.ContentType))
            {
                context.Serializer = new ForwardXmlMessageSerializer(_context.ReceiveContext);
            }
            else
            {
                context.Serializer = new CopyBodySerializer(_context.ReceiveContext);
            }
        }
Ejemplo n.º 21
0
        void CreateSendEndpointContext(IAsyncPipeContextAgent <SendEndpointContext> asyncContext, CancellationToken cancellationToken)
        {
            async Task <SendEndpointContext> Create(ConnectionContext context, CancellationToken createCancellationToken)
            {
                var messageSender = context.CreateMessageSender(_settings.EntityPath);

                var sendEndpointContext = new MessageSendEndpointContext(context, messageSender);

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

                return(sendEndpointContext);
            }

            _supervisor.CreateAgent(asyncContext, Create, cancellationToken);
        }
Ejemplo n.º 22
0
            public async Task Send(SendContext <TMessage> context)
            {
                context.DestinationAddress = _endpoint._destinationAddress;

                context.SourceAddress ??= _endpoint._sourceAddress;

                // ReSharper disable once SuspiciousTypeConversion.Global
                if (_pipe is ISendContextPipe sendContextPipe)
                {
                    await sendContextPipe.Send(context).ConfigureAwait(false);
                }

                await _endpoint._sendPipe.Send(context).ConfigureAwait(false);

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

                context.ConversationId ??= NewId.NextGuid();
            }
Ejemplo n.º 23
0
        public Task Send(SendContext context)
        {
            context.RequestId     = _context.RequestId;
            context.SourceAddress = _context.ReceiveContext.InputAddress;

            if (_context.ExpirationTime.HasValue)
            {
                context.TimeToLive = _context.ExpirationTime.Value - DateTime.UtcNow;
                if (context.TimeToLive.Value <= TimeSpan.Zero)
                {
                    context.TimeToLive = TimeSpan.FromSeconds(1);
                }
            }

            if (_pipe.IsNotEmpty())
            {
                return(_pipe.Send(context));
            }

            return(TaskUtil.Completed);
        }
Ejemplo n.º 24
0
        async Task IPipe <SendContext <TRequest> > .Send(SendContext <TRequest> context)
        {
            await _readyToSend.Task.ConfigureAwait(false);

            context.RequestId       = _requestId;
            context.ResponseAddress = _context.ResponseAddress;

            if (_timeToLive.HasValue)
            {
                context.TimeToLive = _timeToLive.Value;
            }

            IPipe <SendContext <TRequest> > pipe = _pipeConfigurator.Build();

            if (pipe.IsNotEmpty())
            {
                await pipe.Send(context).ConfigureAwait(false);
            }

            _sendContext.TrySetResult(context);
        }
Ejemplo n.º 25
0
        async Task IPipe <SendContext <TRequest> > .Send(SendContext <TRequest> context)
        {
            await _readyToSend.Task.ConfigureAwait(false);

            context.RequestId       = ((RequestHandle)this).RequestId;
            context.ResponseAddress = _context.ResponseAddress;

            context.Headers.Set(MessageHeaders.Request.Accept, _accept);

            if (_timeToLive.HasValue)
            {
                context.TimeToLive ??= _timeToLive.Value;
            }

            IPipe <SendContext <TRequest> > pipe = _pipeConfigurator.Build();

            if (pipe.IsNotEmpty())
            {
                await pipe.Send(context).ConfigureAwait(false);
            }

            _sendContext.TrySetResult(context);
        }
Ejemplo n.º 26
0
        async Task RespondAsyncInternal <T>(object values, IPipe <SendContext <T> > responsePipe = default)
            where T : class
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            var context = await MessageInitializerCache <T> .Initialize(Message, _context.CancellationToken).ConfigureAwait(false);

            IMessageInitializer <T> initializer = MessageInitializerCache <T> .GetInitializer(values.GetType());

            var responseEndpoint = await GetResponseEndpoint <T>().ConfigureAwait(false);

            if (responsePipe.IsNotEmpty())
            {
                await ConsumeTask(initializer.Send(responseEndpoint, context, values, responsePipe)).ConfigureAwait(false);
            }
            else
            {
                await ConsumeTask(initializer.Send(responseEndpoint, context, values)).ConfigureAwait(false);
            }
        }
Ejemplo n.º 27
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.º 28
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();
                }
            }
 public Task Send(SendContext <TMessage> context)
 {
     return(_pipe.IsNotEmpty() ? _pipe.Send(context) : Task.CompletedTask);
 }
Ejemplo n.º 30
0
        public async Task Produce(TKey key, TValue value, IPipe <KafkaSendContext <TKey, TValue> > pipe, CancellationToken cancellationToken)
        {
            LogContext.SetCurrentIfNull(_context.LogContext);

            var context = new KafkaMessageSendContext <TKey, TValue>(key, value, cancellationToken);

            if (_consumeContext != null)
            {
                context.TransferConsumeContextHeaders(_consumeContext);
            }

            context.DestinationAddress = _topicAddress;

            await _context.Send(context).ConfigureAwait(false);

            if (pipe.IsNotEmpty())
            {
                await pipe.Send(context).ConfigureAwait(false);
            }

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

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

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

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

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

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

                var topic = new TopicPartition(_topicAddress.Topic, context.Partition);

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

                context.LogSent();
                activity.AddSendContextHeadersPostSend(context);

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

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

                throw;
            }
            finally
            {
                activity?.Stop();
            }
        }