Ejemplo n.º 1
0
        public async Task GetChannelFromChannelPoolAsync(int x)
        {
            for (int i = 0; i < x; i++)
            {
                var channel = await ChannelPool
                              .GetChannelAsync()
                              .ConfigureAwait(false);

                await ChannelPool
                .ReturnChannelAsync(channel, false)
                .ConfigureAwait(false);
            }
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public async Task <ReadOnlyMemory <byte>?> GetAsync(string queueName)
        {
            IChannelHost chanHost;

            try
            {
                chanHost = await ChannelPool
                           .GetChannelAsync()
                           .ConfigureAwait(false);
            }
            catch { return(default); }
Ejemplo n.º 4
0
        private async Task SetChannelHostAsync()
        {
            if (ConsumingChannelHost != null && !(ConsumerSettings.UseTransientChannels ?? true)) // Return Non-Transient Channels
            {
                await ChannelPool
                .ReturnChannelAsync(ConsumingChannelHost, !Shutdown)
                .ConfigureAwait(false);
            }

            try
            {
                if (ConsumerSettings.UseTransientChannels ?? true)
                {
                    var autoAck = ConsumerSettings.AutoAck ?? false;
                    _logger.LogTrace(LogMessages.Consumer.GettingTransientChannelHost, ConsumerSettings.ConsumerName);
                    ConsumingChannelHost = await ChannelPool
                                           .GetTransientChannelAsync(!autoAck)
                                           .ConfigureAwait(false);
                }
                else if (ConsumerSettings.AutoAck ?? false)
                {
                    _logger.LogTrace(LogMessages.Consumer.GettingChannelHost, ConsumerSettings.ConsumerName);
                    ConsumingChannelHost = await ChannelPool
                                           .GetChannelAsync()
                                           .ConfigureAwait(false);
                }
                else
                {
                    _logger.LogTrace(LogMessages.Consumer.GettingAckChannelHost, ConsumerSettings.ConsumerName);
                    ConsumingChannelHost = await ChannelPool
                                           .GetAckChannelAsync()
                                           .ConfigureAwait(false);
                }
            }
            catch { ConsumingChannelHost = null; }

            if (ConsumingChannelHost != null)
            {
                _logger.LogInformation(
                    LogMessages.Consumer.ChannelEstablished,
                    ConsumerSettings.ConsumerName,
                    ConsumingChannelHost?.ChannelId ?? 0ul);
            }
            else
            {
                _logger.LogError(
                    LogMessages.Consumer.ChannelNotEstablished,
                    ConsumerSettings.ConsumerName);
            }
        }
Ejemplo n.º 5
0
        public async Task OverLoopThroughChannelPoolAsync()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri         = new Uri("amqp://*****:*****@localhost:5672/");
            options.PoolOptions.MaxConnections = 5;
            options.PoolOptions.MaxChannels    = 25;
            var       successCount = 0;
            const int loopCount    = 100_000;
            var       chanPool     = new ChannelPool(options);

            var sw = Stopwatch.StartNew();

            for (int i = 0; i < loopCount; i++)
            {
                var channel = await chanPool
                              .GetChannelAsync()
                              .ConfigureAwait(false);

                if (channel != null)
                {
                    successCount++;
                    await chanPool
                    .ReturnChannelAsync(channel, false)
                    .ConfigureAwait(false);
                }
            }

            for (int i = 0; i < loopCount; i++)
            {
                var channel = await chanPool
                              .GetAckChannelAsync()
                              .ConfigureAwait(false);

                if (channel != null)
                {
                    successCount++;
                    await chanPool
                    .ReturnChannelAsync(channel, false)
                    .ConfigureAwait(false);
                }
            }

            sw.Stop();
            _fixture.Output.WriteLine($"OverLoop Iteration Time: {sw.ElapsedMilliseconds} ms");

            Assert.True(successCount == 2 * loopCount);
        }
        // Note: we could have parameterless overloads of Create and CreateAsync,
        // documented to just use the default endpoint, settings and credentials.
        // Pros:
        // - Might be more reassuring on first use
        // - Allows method group conversions
        // Con: overloads!

        /// <summary>
        /// Asynchronously creates a <see cref="ErrorStatsServiceClient"/>, applying defaults for all unspecified settings,
        /// and creating a channel connecting to the given endpoint with application default credentials where
        /// necessary.
        /// </summary>
        /// <param name="endpoint">Optional <see cref="ServiceEndpoint"/>.</param>
        /// <param name="settings">Optional <see cref="ErrorStatsServiceSettings"/>.</param>
        /// <returns>The task representing the created <see cref="ErrorStatsServiceClient"/>.</returns>
        public static async Task <ErrorStatsServiceClient> CreateAsync(ServiceEndpoint endpoint = null, ErrorStatsServiceSettings settings = null)
        {
            Channel channel = await s_channelPool.GetChannelAsync(endpoint ?? DefaultEndpoint).ConfigureAwait(false);

            return(Create(channel, settings));
        }