private void Free(string serviceName, JsonRpcConnection connection) { using (_shutdownLock.DisposableRead()) { if (!_enableConnectionPool || _shutdown) { // pool is not being used or // manager is already shutdown connection.Dispose(); return; } // queue must exist var queue = _pools[serviceName]; if (queue.Count >= _maxPoolConnections) { // let the connection actually go away connection.Dispose(); return; } // pool the connection queue.Enqueue(connection); } }
internal void Free(ConcurrentQueue <JsonRpcConnection> pool, JsonRpcConnection connection) { using (_shutdownLock.DisposableRead()) { // There is a race between checking the current pool capacity i nthe condition and // and queueing connections to the pool in the else branch. // The amount of pooled connections may thus exceed the capacity at times, // or some connections might not end up returned into the pool and reused. if (_isDisposed || pool.Count >= _capacityPerService) { connection.Dispose(); } else { pool.Enqueue(connection); } } }