Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        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;
            }
        }
Exemplo n.º 3
0
        // private methods
        private CommandWireProtocol <BsonDocument> InitializeIsMasterProtocol(IConnection connection)
        {
            BsonDocument isMasterCommand;
            var          commandResponseHandling = CommandResponseHandling.Return;

            if (connection.Description.IsMasterResult.TopologyVersion != null)
            {
                connection.SetReadTimeout(_serverMonitorSettings.ConnectTimeout + _serverMonitorSettings.HeartbeatInterval);
                commandResponseHandling = CommandResponseHandling.ExhaustAllowed;

                var veryLargeHeartbeatInterval = TimeSpan.FromDays(1); // the server doesn't support Infinite value, so we set just a big enough value
                var maxAwaitTime = _serverMonitorSettings.HeartbeatInterval == Timeout.InfiniteTimeSpan ? veryLargeHeartbeatInterval : _serverMonitorSettings.HeartbeatInterval;
                isMasterCommand = IsMasterHelper.CreateCommand(connection.Description.IsMasterResult.TopologyVersion, maxAwaitTime);
            }
            else
            {
                isMasterCommand = IsMasterHelper.CreateCommand();
            }

            return(IsMasterHelper.CreateProtocol(isMasterCommand, _serverApi, commandResponseHandling));
        }
Exemplo n.º 4
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);
            }
        }