public void EnterWaitQueue()
            {
                _enteredWaitQueue = _pool._waitQueue.Wait(0); // don't wait...
                if (!_enteredWaitQueue)
                {
                    throw MongoWaitQueueFullException.ForConnectionPool(_pool._endPoint);
                }

                _stopwatch = Stopwatch.StartNew();
            }
Exemple #2
0
        private void EnterServerSelectionWaitQueue()
        {
            lock (_serverSelectionWaitQueueLock)
            {
                if (_serverSelectionWaitQueueSize >= _settings.MaxServerSelectionWaitQueueSize)
                {
                    throw MongoWaitQueueFullException.ForServerSelection();
                }

                if (++_serverSelectionWaitQueueSize == 1)
                {
                    _rapidHeartbeatTimer.Change(TimeSpan.Zero, __minHeartbeatInterval);
                }
            }
        }
Exemple #3
0
            // public methods
            public void CheckingOutConnection()
            {
                var handler = _pool._checkingOutConnectionEventHandler;

                if (handler != null)
                {
                    handler(new ConnectionPoolCheckingOutConnectionEvent(_pool._serverId, EventContext.OperationId));
                }

                _enteredWaitQueue = _pool._waitQueue.Wait(0); // don't wait...
                if (!_enteredWaitQueue)
                {
                    throw MongoWaitQueueFullException.ForConnectionPool(_pool._endPoint);
                }

                _stopwatch = Stopwatch.StartNew();
            }
Exemple #4
0
            // private methods
            private void AcquireWaitQueueSlot()
            {
                // enter the wait-queue, deprecated feature
                int freeSlots;

                do
                {
                    freeSlots = _pool._waitQueueFreeSlots;

                    if (freeSlots == 0)
                    {
                        throw MongoWaitQueueFullException.ForConnectionPool(_pool._endPoint);
                    }
                }while (Interlocked.CompareExchange(ref _pool._waitQueueFreeSlots, freeSlots - 1, freeSlots) != freeSlots);

                _enteredWaitQueue = true;
            }
Exemple #5
0
        // public methods
        public async Task <IConnectionHandle> AcquireConnectionAsync(CancellationToken cancellationToken)
        {
            ThrowIfNotOpen();

            bool enteredWaitQueue = false;
            bool enteredPool      = false;

            try
            {
                if (_checkingOutConnectionEventHandler != null)
                {
                    _checkingOutConnectionEventHandler(new ConnectionPoolCheckingOutConnectionEvent(_serverId, EventContext.OperationId));
                }

                enteredWaitQueue = _waitQueue.Wait(0); // don't wait...
                if (!enteredWaitQueue)
                {
                    throw MongoWaitQueueFullException.ForConnectionPool(_endPoint);
                }

                var stopwatch = new Stopwatch();
                enteredPool = await _poolQueue.WaitAsync(_settings.WaitQueueTimeout, cancellationToken).ConfigureAwait(false);

                if (enteredPool)
                {
                    var acquired = AcquireConnection();
                    stopwatch.Stop();
                    if (_checkedOutConnectionEventHandler != null)
                    {
                        _checkedOutConnectionEventHandler(new ConnectionPoolCheckedOutConnectionEvent(acquired.ConnectionId, stopwatch.Elapsed, EventContext.OperationId));
                    }
                    return(acquired);
                }

                stopwatch.Stop();
                var message = string.Format("Timed out waiting for a connection after {0}ms.", stopwatch.ElapsedMilliseconds);
                throw new TimeoutException(message);
            }
            catch (Exception ex)
            {
                if (enteredPool)
                {
                    try
                    {
                        _poolQueue.Release();
                    }
                    catch
                    {
                        // TODO: log this, but don't throw... it's a bug if we get here
                    }
                }

                if (_checkingOutConnectionFailedEventHandler != null)
                {
                    _checkingOutConnectionFailedEventHandler(new ConnectionPoolCheckingOutConnectionFailedEvent(_serverId, ex, EventContext.OperationId));
                }
                throw;
            }
            finally
            {
                if (enteredWaitQueue)
                {
                    try
                    {
                        _waitQueue.Release();
                    }
                    catch
                    {
                        // TODO: log this, but don't throw... it's a bug if we get here
                    }
                }
            }
        }