Example #1
0
        private async Task <MyCatSession> CreateSessionAsync(IOBehavior ioBehavior, CancellationToken cancellationToken)
        {
            var connectTimeout = m_connectionSettings.ConnectionTimeout == 0 ? Timeout.InfiniteTimeSpan : TimeSpan.FromSeconds(checked ((int)m_connectionSettings.ConnectionTimeout));

            using (var timeoutSource = new CancellationTokenSource(connectTimeout))
                using (var linkedSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutSource.Token))
                {
                    try
                    {
                        // get existing session from the pool if possible
                        if (m_connectionSettings.Pooling)
                        {
                            var pool = ConnectionPool.GetPool(m_connectionSettings);
                            // this returns an open session
                            return(await pool.GetSessionAsync(ioBehavior, linkedSource.Token).ConfigureAwait(false));
                        }
                        else
                        {
                            var session = new MyCatSession();
                            await session.ConnectAsync(m_connectionSettings, ioBehavior, linkedSource.Token).ConfigureAwait(false);

                            return(session);
                        }
                    }
                    catch (OperationCanceledException ex) when(timeoutSource.IsCancellationRequested)
                    {
                        throw new MyCatException("Connect Timeout expired.", ex);
                    }
                }
        }
Example #2
0
        private static async Task ClearPoolAsync(MyCatConnection connection, IOBehavior ioBehavior, CancellationToken cancellationToken)
        {
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }

            var pool = ConnectionPool.GetPool(connection.m_connectionSettings);

            if (pool != null)
            {
                await pool.ClearAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
            }
        }