Exemplo n.º 1
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.º 2
0
        private async Task <IChannelHost> CreateChannelAsync(ulong channelId, bool ackable)
        {
            IChannelHost    chanHost;
            IConnectionHost connHost = null;

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

                var sleep = false;

                // Get ConnectionHost
                try
                { connHost = await _connectionPool.GetConnectionAsync().ConfigureAwait(false); }
                catch
                {
                    _logger.LogTrace(LogMessages.ChannelPool.CreateChannelFailedConnection, channelId);
                    sleep = true;
                }

                // Create a Channel Host
                if (!sleep)
                {
                    try
                    {
                        chanHost = new ChannelHost(channelId, connHost, ackable);
                        await _connectionPool.ReturnConnectionAsync(connHost); // Return Connection (or lose them.)

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

                        return(chanHost);
                    }
                    catch
                    {
                        _logger.LogTrace(LogMessages.ChannelPool.CreateChannelFailedConstruction, channelId);
                        sleep = true;
                    }
                }

                // Sleep on failure.
                if (sleep)
                {
                    if (connHost != null)
                    {
                        await _connectionPool.ReturnConnectionAsync(connHost);
                    }                                                          // Return Connection (or lose them.)

                    _logger.LogDebug(LogMessages.ChannelPool.CreateChannelSleep, channelId);

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