Exemplo n.º 1
0
        private async Task SetChannelHostAsync()
        {
            if (ConsumerOptions.UseTransientChannels ?? true)
            {
                var autoAck = ConsumerOptions.AutoAck ?? false;
                _logger.LogTrace(LogMessages.Consumers.GettingTransientChannelHost, ConsumerOptions.ConsumerName);
                _chanHost = await ChannelPool
                            .GetTransientChannelAsync(!autoAck)
                            .ConfigureAwait(false);
            }
            else if (ConsumerOptions.AutoAck ?? false)
            {
                _logger.LogTrace(LogMessages.Consumers.GettingChannelHost, ConsumerOptions.ConsumerName);
                _chanHost = await ChannelPool
                            .GetChannelAsync()
                            .ConfigureAwait(false);
            }
            else
            {
                _logger.LogTrace(LogMessages.Consumers.GettingAckChannelHost, ConsumerOptions.ConsumerName);
                _chanHost = await ChannelPool
                            .GetAckChannelAsync()
                            .ConfigureAwait(false);
            }

            _logger.LogDebug(
                LogMessages.Consumers.ChannelEstablished,
                ConsumerOptions.ConsumerName,
                _chanHost?.ChannelId ?? 0ul);
        }
Exemplo n.º 2
0
        public async ValueTask ReturnChannelAsync(IChannelHost chanHost, bool flagChannel = false)
        {
            if (Shutdown)
            {
                throw new InvalidOperationException(ExceptionMessages.ChannelPoolValidationMessage);
            }

            _flaggedChannels[chanHost.ChannelId] = flagChannel;

            _logger.LogDebug(LogMessages.ChannelPools.ReturningChannel, chanHost.ChannelId, flagChannel);

            if (chanHost.Ackable)
            {
                await _ackChannels
                .Writer
                .WriteAsync(chanHost)
                .ConfigureAwait(false);
            }
            else
            {
                await _channels
                .Writer
                .WriteAsync(chanHost)
                .ConfigureAwait(false);
            }
        }
Exemplo n.º 3
0
        private static IBasicProperties BuildProperties(Letter letter, IChannelHost channelHost, bool withHeaders)
        {
            var props = channelHost.GetChannel().CreateBasicProperties();

            props.DeliveryMode = letter.Envelope.RoutingOptions.DeliveryMode;
            props.ContentType  = letter.Envelope.RoutingOptions.MessageType;
            props.Priority     = letter.Envelope.RoutingOptions.PriorityLevel;

            if (!props.IsHeadersPresent())
            {
                props.Headers = new Dictionary <string, object>();
            }

            if (withHeaders && letter.LetterMetadata != null)
            {
                foreach (var kvp in letter.LetterMetadata?.CustomFields)
                {
                    if (kvp.Key.StartsWith(Utils.Constants.HeaderPrefix, StringComparison.OrdinalIgnoreCase))
                    {
                        props.Headers[kvp.Key] = kvp.Value;
                    }
                }
            }

            // Non-optional Header.
            props.Headers[Utils.Constants.HeaderForObjectType] = Utils.Constants.HeaderValueForLetter;

            return(props);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Intended to bring feature parity and include properties when publishing byte[], which you get for free when publishing with IMessage objects.
        /// </summary>
        /// <param name="headers"></param>
        /// <param name="channelHost"></param>
        /// <param name="messageId"></param>
        /// <param name="priority"></param>
        /// <param name="deliveryMode"></param>
        /// <returns></returns>
        private static IBasicProperties BuildProperties(
            IDictionary <string, object> headers,
            IChannelHost channelHost,
            string messageId  = null,
            byte?priority     = 0,
            byte?deliveryMode = 2)
        {
            var props = channelHost.GetChannel().CreateBasicProperties();

            props.DeliveryMode = deliveryMode ?? 2; // Default Persisted
            props.Priority     = priority ?? 0;     // Default Priority
            props.MessageId    = messageId ?? Guid.NewGuid().ToString();

            if (!props.IsHeadersPresent())
            {
                props.Headers = new Dictionary <string, object>();
            }

            if (headers?.Count > 0)
            {
                foreach (var kvp in headers)
                {
                    if (kvp.Key.StartsWith(Constants.HeaderPrefix, StringComparison.OrdinalIgnoreCase))
                    {
                        props.Headers[kvp.Key] = kvp.Value;
                    }
                }
            }

            // Non-optional Header.
            props.Headers[Constants.HeaderForObjectType] = Constants.HeaderValueForMessage;

            return(props);
        }
Exemplo n.º 5
0
        public static IBasicProperties CreateBasicProperties(
            this IMessage message,
            IChannelHost channelHost,
            bool withOptionalHeaders,
            IMetadata metadata)
        {
            var props = channelHost.GetChannel().CreateBasicProperties();

            props.DeliveryMode = message.Envelope.RoutingOptions.DeliveryMode;
            props.ContentType  = message.Envelope.RoutingOptions.MessageType;
            props.Priority     = message.Envelope.RoutingOptions.PriorityLevel;
            props.MessageId    = message.MessageId ?? Guid.NewGuid().ToString();

            if (!props.IsHeadersPresent())
            {
                props.Headers = new Dictionary <string, object>();
            }

            if (withOptionalHeaders && metadata != null)
            {
                foreach (var kvp in metadata?.CustomFields)
                {
                    if (kvp.Key.StartsWith(Constants.HeaderPrefix, StringComparison.OrdinalIgnoreCase))
                    {
                        props.Headers[kvp.Key] = kvp.Value;
                    }
                }
            }

            return(props);
        }
Exemplo n.º 6
0
        private async Task <IChannelHost> CreateChannelAsync(ulong channelId, bool ackable)
        {
            IChannelHost    chanHost = null;
            IConnectionHost connHost = null;

            while (true)
            {
                _logger.LogDebug(LogMessages.ChannelPool.CreateChannel, channelId);

                var sleep = false;

                try
                {
                    connHost = await _connectionPool
                               .GetConnectionAsync()
                               .ConfigureAwait(false);

                    if (!await connHost.HealthyAsync().ConfigureAwait(false))
                    {
                        _logger.LogDebug(LogMessages.ChannelPool.CreateChannelFailedConnection, channelId);
                        sleep = true;
                    }
                }
                catch
                {
                    _logger.LogDebug(LogMessages.ChannelPool.CreateChannelFailedConnection, channelId);
                    sleep = true;
                }

                if (!sleep)
                {
                    try
                    { chanHost = new ChannelHost(channelId, connHost, ackable); }
                    catch
                    {
                        _logger.LogDebug(LogMessages.ChannelPool.CreateChannelFailedConstruction, channelId);
                        sleep = true;
                    }
                }

                if (sleep)
                {
                    _logger.LogDebug(LogMessages.ChannelPool.CreateChannelSleep, channelId);

                    await Task
                    .Delay(Config.PoolSettings.SleepOnErrorInterval)
                    .ConfigureAwait(false);

                    continue; // Continue here forever (till reconnection is established).
                }

                break;
            }

            _flaggedChannels[chanHost.ChannelId] = false;

            _logger.LogDebug(LogMessages.ChannelPool.CreateChannelSuccess, channelId);
            return(chanHost);
        }
Exemplo n.º 7
0
        public IBasicProperties BuildProperties(IChannelHost channelHost, bool withOptionalHeaders)
        {
            MessageId ??= Guid.NewGuid().ToString();

            var props = this.CreateBasicProperties(channelHost, withOptionalHeaders, LetterMetadata);

            props.MessageId = MessageId;

            // Non-optional Header.
            props.Headers[Constants.HeaderForObjectType] = Constants.HeaderValueForLetter;

            return(props);
        }
Exemplo n.º 8
0
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposedValue)
            {
                if (disposing)
                {
                    _dataFlowExecLock.Dispose();
                    _conLock.Dispose();
                }

                _dataBuffer    = null;
                _chanHost      = null;
                _disposedValue = true;
            }
        }
Exemplo n.º 9
0
        private async Task <IChannelHost> CreateChannelAsync(ulong channelId, bool ackable)
        {
            IChannelHost    chanHost = null;
            IConnectionHost connHost = null;

            while (true)
            {
                _logger.LogTrace(LogMessages.ChannelPools.CreateChannel, channelId);

                // Get ConnectionHost
                try
                { connHost = await _connectionPool.GetConnectionAsync().ConfigureAwait(false); }
                catch
                {
                    _logger.LogTrace(LogMessages.ChannelPools.CreateChannelFailedConnection, channelId);
                    await ReturnConnectionWithOptionalSleep(connHost, channelId, Options.PoolOptions.SleepOnErrorInterval).ConfigureAwait(false);

                    continue;
                }

                // Create a Channel Host
                try
                {
                    chanHost = new ChannelHost(channelId, connHost, ackable);
                    await ReturnConnectionWithOptionalSleep(connHost, channelId, 0).ConfigureAwait(false);

                    _flaggedChannels[chanHost.ChannelId] = false;
                    _logger.LogDebug(LogMessages.ChannelPools.CreateChannelSuccess, channelId);

                    return(chanHost);
                }
                catch
                {
                    _logger.LogTrace(LogMessages.ChannelPools.CreateChannelFailedConstruction, channelId);
                    await ReturnConnectionWithOptionalSleep(connHost, channelId, Options.PoolOptions.SleepOnErrorInterval).ConfigureAwait(false);
                }
            }
        }