private async Task <IsMasterResult> GetIsMasterResultAsync(
            IConnection connection,
            CommandWireProtocol <BsonDocument> isMasterProtocol,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (_heartbeatStartedEventHandler != null)
            {
                _heartbeatStartedEventHandler(new ServerHeartbeatStartedEvent(connection.ConnectionId, connection.Description.IsMasterResult.TopologyVersion != null));
            }

            try
            {
                var stopwatch      = Stopwatch.StartNew();
                var isMasterResult = await IsMasterHelper.GetResultAsync(connection, isMasterProtocol, cancellationToken).ConfigureAwait(false);

                stopwatch.Stop();

                if (_heartbeatSucceededEventHandler != null)
                {
                    _heartbeatSucceededEventHandler(new ServerHeartbeatSucceededEvent(connection.ConnectionId, stopwatch.Elapsed, connection.Description.IsMasterResult.TopologyVersion != null));
                }

                return(isMasterResult);
            }
            catch (Exception ex)
            {
                if (_heartbeatFailedEventHandler != null)
                {
                    _heartbeatFailedEventHandler(new ServerHeartbeatFailedEvent(connection.ConnectionId, ex, connection.Description.IsMasterResult.TopologyVersion != null));
                }
                throw;
            }
        }
Exemple #2
0
        /// <inheritdoc/>
        public async Task AuthenticateAsync(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(connection, nameof(connection));
            Ensure.IsNotNull(description, nameof(description));

            // If we don't have SaslSupportedMechs as part of the response, that means we didn't piggyback the initial
            // isMaster request and should query the server (provided that the server >= 4.0), merging results into
            // a new ConnectionDescription
            if (!description.IsMasterResult.HasSaslSupportedMechs &&
                Feature.ScramSha256Authentication.IsSupported(description.ServerVersion))
            {
                var command          = CustomizeInitialIsMasterCommand(IsMasterHelper.CreateCommand());
                var isMasterProtocol = IsMasterHelper.CreateProtocol(command);
                var isMasterResult   = await IsMasterHelper.GetResultAsync(connection, isMasterProtocol, cancellationToken).ConfigureAwait(false);

                var mergedIsMasterResult = new IsMasterResult(description.IsMasterResult.Wrapped.Merge(isMasterResult.Wrapped));
                description = new ConnectionDescription(
                    description.ConnectionId,
                    mergedIsMasterResult,
                    description.BuildInfoResult);
            }

            var authenticator = GetOrCreateAuthenticator(connection, description);
            await authenticator.AuthenticateAsync(connection, description, cancellationToken).ConfigureAwait(false);
        }
Exemple #3
0
        public async Task RunAsync()
        {
            await Task.Yield(); // return control immediately

            while (!_cancellationToken.IsCancellationRequested)
            {
                try
                {
                    if (_roundTripTimeConnection == null)
                    {
                        await InitializeConnectionAsync().ConfigureAwait(false); // sets _roundTripTimeConnection
                    }
                    else
                    {
                        var isMasterCommand  = IsMasterHelper.CreateCommand();
                        var isMasterProtocol = IsMasterHelper.CreateProtocol(isMasterCommand);

                        var stopwatch      = Stopwatch.StartNew();
                        var isMasterResult = await IsMasterHelper.GetResultAsync(_roundTripTimeConnection, isMasterProtocol, _cancellationToken).ConfigureAwait(false);

                        stopwatch.Stop();
                        AddSample(stopwatch.Elapsed);
                    }
                }
                catch (Exception)
                {
                    IConnection toDispose;
                    lock (_lock)
                    {
                        toDispose = _roundTripTimeConnection;
                        _roundTripTimeConnection = null;
                    }
                    toDispose?.Dispose();
                }

                await Task.Delay(_heartbeatInterval, _cancellationToken).ConfigureAwait(false);
            }
        }