public TItem TakeConnection(TimeSpan timeout) { TItem item = null; List <TItem> itemsToClose = null; lock (ThisLock) { if (closed) { return(null); } bool closeItem; while (true) { item = IdleConnections.Take(out closeItem); if (item == null) { break; } if (!closeItem) { busyConnections.Add(item); break; } if (itemsToClose == null) { itemsToClose = new List <TItem>(); } itemsToClose.Add(item); } } // cleanup any stale items accrued from IdleConnections if (itemsToClose != null) { // and only allocate half the timeout passed in for our g----ful shutdowns TimeoutHelper timeoutHelper = new TimeoutHelper(TimeoutHelper.Divide(timeout, 2)); for (int i = 0; i < itemsToClose.Count; i++) { CloseIdleConnection(itemsToClose[i], timeoutHelper.RemainingTime()); } } if (TD.ConnectionPoolMissIsEnabled()) { if (item == null && busyConnections != null) { TD.ConnectionPoolMiss(key != null ? key.ToString() : string.Empty, busyConnections.Count); } } return(item); }
// must call under lock (ThisLock) since we are calling IdleConnections.Take() List <TItem> SnapshotIdleConnections() { List <TItem> itemsToClose = new List <TItem>(); bool dummy; for (;;) { TItem item = IdleConnections.Take(out dummy); if (item == null) { break; } itemsToClose.Add(item); } return(itemsToClose); }
public void ReturnConnection(TItem connection, bool connectionIsStillGood, TimeSpan timeout) { bool closeConnection = false; bool abortConnection = false; lock (ThisLock) { if (!closed) { if (busyConnections.Remove(connection) && connectionIsStillGood) { if (!IdleConnections.Return(connection)) { closeConnection = true; } } else { abortConnection = true; } } else { abortConnection = true; } } if (closeConnection) { CloseIdleConnection(connection, timeout); } else if (abortConnection) { this.AbortItem(connection); OnConnectionAborted(); } }
public void AddConnection(TItem connection, TimeSpan timeout) { bool closeConnection = false; lock (ThisLock) { if (!closed) { if (!IdleConnections.Add(connection)) { closeConnection = true; } } else { closeConnection = true; } } if (closeConnection) { CloseIdleConnection(connection, timeout); } }