protected override void HandleBeforeHandshakeCompletesException(Exception ex)
        {
            if (ex is not MongoConnectionException connectionException)
            {
                // non connection exception
                return;
            }

            var(invalidateAndClear, cancelCheck) = ex switch
            {
                MongoAuthenticationException => (invalidateAndClear : true, cancelCheck : false),
                _ => (invalidateAndClear : connectionException.IsNetworkException || connectionException.ContainsTimeoutException,
                      cancelCheck : connectionException.IsNetworkException && !connectionException.ContainsTimeoutException)
            };

            if (invalidateAndClear)
            {
                lock (_monitor.Lock)
                {
                    if (connectionException.Generation != null && connectionException.Generation != ConnectionPool.Generation)
                    {
                        // stale generation number
                        return;
                    }

                    if (cancelCheck)
                    {
                        _monitor.CancelCurrentCheck();
                    }

                    Invalidate($"ChannelException during handshake: {ex}.", clearConnectionPool: true, topologyVersion: null);
                }
            }
        }
Exemple #2
0
        protected override void HandleBeforeHandshakeCompletesException(Exception ex)
        {
            if (ex is MongoAuthenticationException)
            {
                ConnectionPool.Clear();
                return;
            }

            if (ex is MongoConnectionException mongoConnectionException)
            {
                lock (_monitor.Lock)
                {
                    if (mongoConnectionException.Generation != null &&
                        mongoConnectionException.Generation != ConnectionPool.Generation)
                    {
                        return; // stale generation number
                    }

                    if (mongoConnectionException.IsNetworkException &&
                        !mongoConnectionException.ContainsTimeoutException)
                    {
                        _monitor.CancelCurrentCheck();
                    }

                    if (mongoConnectionException.IsNetworkException || mongoConnectionException.ContainsTimeoutException)
                    {
                        Invalidate($"ChannelException during handshake: {ex}.", clearConnectionPool: true, topologyVersion: null);
                    }
                }
            }
        }