Esempio n. 1
0
            public async Task <IConnectionHandle> AcquireConnectionAsync(CancellationToken cancellationToken)
            {
                try
                {
                    StartCheckingOut();

                    var stopwatch = Stopwatch.StartNew();
                    _poolQueueWaitResult = await _pool._maxConnectionsQueue.WaitSignaledAsync(_timeout, cancellationToken).ConfigureAwait(false);

                    if (_poolQueueWaitResult == SemaphoreSlimSignalable.SemaphoreWaitResult.Entered)
                    {
                        PooledConnection pooledConnection = null;
                        var timeout = EnsureTimeout(stopwatch);

                        using (var connectionCreator = new ConnectionCreator(_pool, timeout))
                        {
                            pooledConnection = await connectionCreator.CreateOpenedOrReuseAsync(cancellationToken).ConfigureAwait(false);
                        }

                        return(EndCheckingOut(pooledConnection, stopwatch));
                    }

                    stopwatch.Stop();
                    throw CreateException(stopwatch);
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                    throw;
                }
            }
Esempio n. 2
0
        private void ReleaseConnection(PooledConnection connection)
        {
            if (_checkingInConnectionEventHandler != null)
            {
                _checkingInConnectionEventHandler(new ConnectionPoolCheckingInConnectionEvent(connection.ConnectionId, EventContext.OperationId));
            }

            if (_checkedInConnectionEventHandler != null)
            {
                _checkedInConnectionEventHandler(new ConnectionPoolCheckedInConnectionEvent(connection.ConnectionId, TimeSpan.Zero, EventContext.OperationId));
            }

            _checkOutReasonCounter.Decrement(connection.CheckOutReason);

            if (!connection.IsExpired && !_poolState.IsDisposed)
            {
                _connectionHolder.Return(connection);
            }
            else
            {
                _serviceStates.DecrementConnectionCount(connection.Description?.ServiceId);

                _connectionHolder.RemoveConnection(connection);
            }

            if (!_poolState.IsDisposed)
            {
                _maxConnectionsQueue.Release();
            }
        }
 public AcquiredConnection(ExclusiveConnectionPool connectionPool, PooledConnection pooledConnection)
     : base(pooledConnection)
 {
     _connectionPool   = connectionPool;
     _pooledConnection = pooledConnection;
     _pooledConnection.IncrementReferenceCount();
 }
Esempio n. 4
0
            public PooledConnection Acquire()
            {
                PooledConnection result = null;

                lock (_lock)
                {
                    while (_connections.Count > 0 && result == null)
                    {
                        var lastIndex  = _connections.Count - 1;
                        var connection = _connections[lastIndex];
                        _connections.RemoveAt(lastIndex);
                        if (connection.IsExpired)
                        {
                            RemoveConnection(connection);
                        }
                        else
                        {
                            result = connection;
                        }
                    }

                    SignalOrReset();
                }
                return(result);
            }
Esempio n. 5
0
            // private methods
            private IConnectionHandle AcquireOrCreateConnection()
            {
                PooledConnection connection = _pool._connectionHolder.Acquire();

                if (connection == null)
                {
                    var addingConnectionEventHandler = _pool._addingConnectionEventHandler;
                    if (addingConnectionEventHandler != null)
                    {
                        addingConnectionEventHandler(new ConnectionPoolAddingConnectionEvent(_pool._serverId, EventContext.OperationId));
                    }

                    var stopwatch = Stopwatch.StartNew();
                    connection = _pool.CreateNewConnection();
                    stopwatch.Stop();

                    var addedConnectionEventHandler = _pool._addedConnectionEventHandler;
                    if (addedConnectionEventHandler != null)
                    {
                        addedConnectionEventHandler(new ConnectionPoolAddedConnectionEvent(connection.ConnectionId, stopwatch.Elapsed, EventContext.OperationId));
                    }
                }

                var reference = new ReferenceCounted <PooledConnection>(connection, _pool.ReleaseConnection);

                return(new AcquiredConnection(_pool, reference));
            }
Esempio n. 6
0
        private void ReleaseConnection(PooledConnection connection)
        {
            if (_checkingInConnectionEventHandler != null)
            {
                _checkingInConnectionEventHandler(new ConnectionPoolCheckingInConnectionEvent(connection.ConnectionId, EventContext.OperationId));
            }

            if (_checkedInConnectionEventHandler != null)
            {
                _checkedInConnectionEventHandler(new ConnectionPoolCheckedInConnectionEvent(connection.ConnectionId, TimeSpan.Zero, EventContext.OperationId));
            }

            if (!connection.IsExpired && _state.Value != State.Disposed)
            {
                _connectionHolder.Return(connection);
            }
            else
            {
                _connectionHolder.RemoveConnection(connection);
            }

            if (_state.Value != State.Disposed)
            {
                _poolQueue.Release();
            }
        }
Esempio n. 7
0
 public void Return(PooledConnection connection)
 {
     lock (_lock)
     {
         _connections.Add(connection);
     }
 }
Esempio n. 8
0
 public void UntrackInUseConnection(PooledConnection connection)
 {
     lock (_lockInUse)
     {
         _connectionsInUse.Remove(connection);
     }
 }
Esempio n. 9
0
 public void TrackInUseConnection(PooledConnection connection)
 {
     lock (_lockInUse)
     {
         _connectionsInUse.Add(connection);
     }
 }
 public void Return(PooledConnection connection)
 {
     lock (_lock)
     {
         _connections.Add(connection);
         SignalOrReset();
     }
 }
Esempio n. 11
0
        private PooledConnection CreateNewConnection()
        {
            var connection       = _connectionFactory.CreateConnection(_serverId, _endPoint);
            var pooledConnection = new PooledConnection(this, connection);

            _connectionCreatedEventHandler?.Invoke(new ConnectionCreatedEvent(connection.ConnectionId, connection.Settings, EventContext.OperationId));
            return(pooledConnection);
        }
        private PooledConnection CreateConnection()
        {
            var connection       = _connectionFactory.CreateConnection(_serverId, _endPoint); // will be initialized by caller outside of the lock
            var pooledConnection = new PooledConnection(connection);

            _connections.Add(pooledConnection);
            return(pooledConnection);
        }
 public ConnectionCreator(ExclusiveConnectionPool pool, TimeSpan connectingTimeout)
 {
     _pool = pool;
     _connectingTimeout    = connectingTimeout;
     _connectingWaitStatus = SemaphoreSlimSignalable.SemaphoreWaitResult.None;
     _connection           = null;
     _disposeConnection    = true;
     _stopwatch            = null;
 }
            public void Return(PooledConnection connection)
            {
                if (connection.IsExpired)
                {
                    RemoveConnection(connection);
                    return;
                }

                _connections.Enqueue(connection);
            }
Esempio n. 15
0
            public void Return(PooledConnection connection)
            {
                UntrackInUseConnection(connection);

                lock (_lock)
                {
                    _connections.Add(connection);
                    SignalOrReset();
                }
            }
            private void StartCreating(CancellationToken cancellationToken)
            {
                var addingConnectionEvent = new ConnectionPoolAddingConnectionEvent(_pool._serverId, EventContext.OperationId);

                _pool._addingConnectionEventHandler?.Invoke(addingConnectionEvent);

                cancellationToken.ThrowIfCancellationRequested();

                _stopwatch  = Stopwatch.StartNew();
                _connection = _pool.CreateNewConnection();
            }
Esempio n. 17
0
            public void Return(PooledConnection connection)
            {
                if (connection.IsExpired)
                {
                    RemoveConnection(connection);
                    return;
                }

                lock (_lock)
                {
                    _connections.Add(connection);
                }
            }
Esempio n. 18
0
            private IConnectionHandle EndCheckingOut(PooledConnection pooledConnection, Stopwatch stopwatch)
            {
                var reference        = new ReferenceCounted <PooledConnection>(pooledConnection, _pool.ReleaseConnection);
                var connectionHandle = new AcquiredConnection(_pool, reference);

                stopwatch.Stop();
                var checkedOutConnectionEvent = new ConnectionPoolCheckedOutConnectionEvent(connectionHandle.ConnectionId, stopwatch.Elapsed, EventContext.OperationId);

                _pool._checkedOutConnectionEventHandler?.Invoke(checkedOutConnectionEvent);

                // no need to release the semaphore
                _poolQueueWaitResult = SemaphoreSlimSignalable.SemaphoreWaitResult.None;

                return(connectionHandle);
            }
            public async Task <IConnectionHandle> EnteredPoolAsync(bool enteredPool, CancellationToken cancellationToken)
            {
                _enteredPool = enteredPool;
                PooledConnection connection = null;

                if (enteredPool)
                {
                    var timeSpentInWaitQueue = _stopwatch.Elapsed;
                    using (var connectionCreator = new ConnectionCreator(_pool, _pool._settings.WaitQueueTimeout - timeSpentInWaitQueue))
                    {
                        connection = await connectionCreator.CreateOpenedOrReuseAsync(cancellationToken).ConfigureAwait(false);
                    }
                }

                return(FinalizePoolEnterance(connection));
            }
            public IConnectionHandle EnteredPool(bool enteredPool, CancellationToken cancellationToken)
            {
                _enteredPool = enteredPool;
                PooledConnection connection = null;

                if (enteredPool)
                {
                    var timeSpentInWaitQueue = _stopwatch.Elapsed;
                    using (var connectionCreator = new ConnectionCreator(_pool, _pool._settings.WaitQueueTimeout - timeSpentInWaitQueue))
                    {
                        connection = connectionCreator.CreateOpenedOrReuse(cancellationToken);
                    }
                }

                return(FinalizePoolEnterance(connection));
            }
Esempio n. 21
0
            private void RemoveConnection(PooledConnection connection)
            {
                if (_removingConnectionEventHandler != null)
                {
                    _removingConnectionEventHandler(new ConnectionPoolRemovingConnectionEvent(connection.ConnectionId, EventContext.OperationId));
                }

                var stopwatch = Stopwatch.StartNew();

                connection.Dispose();
                stopwatch.Stop();

                if (_removedConnectionEventHandler != null)
                {
                    _removedConnectionEventHandler(new ConnectionPoolRemovedConnectionEvent(connection.ConnectionId, stopwatch.Elapsed, EventContext.OperationId));
                }
            }
Esempio n. 22
0
            private void RemoveConnection(PooledConnection connection)
            {
                if (_listener != null)
                {
                    _listener.ConnectionPoolBeforeRemovingAConnection(new ConnectionPoolBeforeRemovingAConnectionEvent(connection.ConnectionId));
                }

                var stopwatch = Stopwatch.StartNew();

                connection.Dispose();
                stopwatch.Stop();

                if (_listener != null)
                {
                    _listener.ConnectionPoolAfterRemovingAConnection(new ConnectionPoolAfterRemovingAConnectionEvent(connection.ConnectionId, stopwatch.Elapsed));
                }
            }
Esempio n. 23
0
            public void RemoveConnection(PooledConnection connection)
            {
                if (_removingConnectionEventHandler != null)
                {
                    _removingConnectionEventHandler(new ConnectionPoolRemovingConnectionEvent(connection.ConnectionId, EventContext.OperationId));
                }

                var stopwatch = Stopwatch.StartNew();

                UntrackInUseConnection(connection); // no op if connection is not in use
                connection.Dispose();
                stopwatch.Stop();

                if (_removedConnectionEventHandler != null)
                {
                    _removedConnectionEventHandler(new ConnectionPoolRemovedConnectionEvent(connection.ConnectionId, stopwatch.Elapsed, EventContext.OperationId));
                }
            }
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (!Disposed)
         {
             _pooledConnection.DecrementReferenceCount();
             if (_pooledConnection.ReferenceCount == 0)
             {
                 _connectionPool.ReleaseConnection(_pooledConnection);
             }
         }
         Disposed          = true;
         _pooledConnection = null;
         _connectionPool   = null;
     }
     // don't call base.Dispose here because we don't want the underlying
     // connection to get disposed...
 }
            private AcquiredConnection FinalizePoolEnterance(PooledConnection pooledConnection)
            {
                if (pooledConnection != null)
                {
                    var reference        = new ReferenceCounted <PooledConnection>(pooledConnection, _pool.ReleaseConnection);
                    var connectionHandle = new AcquiredConnection(_pool, reference);

                    var checkedOutConnectionEvent = new ConnectionPoolCheckedOutConnectionEvent(connectionHandle.ConnectionId, _stopwatch.Elapsed, EventContext.OperationId);
                    _pool._checkedOutConnectionEventHandler?.Invoke(checkedOutConnectionEvent);

                    return(connectionHandle);
                }
                else
                {
                    _stopwatch.Stop();

                    var message = $"Timed out waiting for a connection after {_stopwatch.ElapsedMilliseconds}ms.";
                    throw new TimeoutException(message);
                }
            }
        private IConnectionHandle AcquireConnection()
        {
            PooledConnection connection = _connectionHolder.Acquire();

            if (connection == null)
            {
                if (_listener != null)
                {
                    _listener.ConnectionPoolBeforeAddingAConnection(_serverId);
                }
                var stopwatch = Stopwatch.StartNew();
                connection = CreateNewConnection();
                stopwatch.Stop();
                if (_listener != null)
                {
                    _listener.ConnectionPoolAfterAddingAConnection(connection.ConnectionId, stopwatch.Elapsed);
                }
            }

            return(new AcquiredConnection(this, connection));
        }
Esempio n. 27
0
            public PooledConnection Acquire()
            {
                PooledConnection result = null;

                lock (_lock)
                {
                    while (_connections.Count > 0 && result == null)
                    {
                        var lastIndex  = _connections.Count - 1;
                        var connection = _connections[lastIndex];
                        _connections.RemoveAt(lastIndex);
                        if (connection.IsExpired)
                        {
                            RemoveConnection(connection);
                        }
                        else
                        {
                            result = connection;
                        }
                    }

                    SignalOrReset();
                }

                if (result != null)
                {
                    TrackInUseConnection(result);

                    // This connection can be expired and not disposed by Prune. Dispose if needed
                    if (result.IsExpired)
                    {
                        RemoveConnection(result);
                        result = null;
                    }
                }

                return(result);
            }
Esempio n. 28
0
        private IConnectionHandle AcquireConnection()
        {
            PooledConnection connection = _connectionHolder.Acquire();

            if (connection == null)
            {
                if (_listener != null)
                {
                    _listener.ConnectionPoolBeforeAddingAConnection(new ConnectionPoolBeforeAddingAConnectionEvent(_serverId));
                }
                var stopwatch = Stopwatch.StartNew();
                connection = CreateNewConnection();
                stopwatch.Stop();
                if (_listener != null)
                {
                    _listener.ConnectionPoolAfterAddingAConnection(new ConnectionPoolAfterAddingAConnectionEvent(connection.ConnectionId, stopwatch.Elapsed));
                }
            }

            var reference = new ReferenceCounted <PooledConnection>(connection, x => ReleaseConnection(x));

            return(new AcquiredConnection(this, reference));
        }
Esempio n. 29
0
        private void ReleaseConnection(PooledConnection connection)
        {
            if (_state.Value == State.Disposed)
            {
                connection.Dispose();
                return;
            }

            if (_checkingInConnectionEventHandler != null)
            {
                _checkingInConnectionEventHandler(new ConnectionPoolCheckingInConnectionEvent(connection.ConnectionId, EventContext.OperationId));
            }

            var stopwatch = Stopwatch.StartNew();

            _connectionHolder.Return(connection);
            _poolQueue.Release();
            stopwatch.Stop();

            if (_checkedInConnectionEventHandler != null)
            {
                _checkedInConnectionEventHandler(new ConnectionPoolCheckedInConnectionEvent(connection.ConnectionId, stopwatch.Elapsed, EventContext.OperationId));
            }
        }
Esempio n. 30
0
        private void ReleaseConnection(PooledConnection connection)
        {
            if (_state.Value == State.Disposed)
            {
                connection.Dispose();
                return;
            }

            if (_listener != null)
            {
                _listener.ConnectionPoolBeforeCheckingInAConnection(new ConnectionPoolBeforeCheckingInAConnectionEvent(connection.ConnectionId));
            }

            var stopwatch = Stopwatch.StartNew();

            _connectionHolder.Return(connection);
            _poolQueue.Release();
            stopwatch.Stop();

            if (_listener != null)
            {
                _listener.ConnectionPoolAfterCheckingInAConnection(new ConnectionPoolAfterCheckingInAConnectionEvent(connection.ConnectionId, stopwatch.Elapsed));
            }
        }