/// <summary> /// Releases all resources allocated by this instance /// </summary> public void Dispose() { // this is not a graceful shutdown // if someone uses a pooled item then 99% that an exception will be thrown // somewhere. But since the dispose is mostly used when everyone else is finished // this should not kill any kittens if (!this.isDisposed) { this.isAlive = false; this.isDisposed = true; PooledSocket ps; while (this.freeItems.TryPop(out ps)) { try { ps.Destroy(); } catch { } } this.ownerNode = null; this.semaphore.Close(); this.semaphore = null; this.freeItems = null; } }
internal InternalPoolImpl(MemcachedNode ownerNode, ISocketPoolConfiguration config) { if (config.MinPoolSize < 0) { throw new InvalidOperationException("minItems must be larger >= 0", null); } if (config.MaxPoolSize < config.MinPoolSize) { throw new InvalidOperationException("maxItems must be larger than minItems", null); } if (config.QueueTimeout < TimeSpan.Zero) { throw new InvalidOperationException("queueTimeout must be >= TimeSpan.Zero", null); } this.ownerNode = ownerNode; this.isAlive = true; this.endPoint = ownerNode.EndPoint; this.queueTimeout = config.QueueTimeout; this.minItems = config.MinPoolSize; this.maxItems = config.MaxPoolSize; this.semaphore = new Semaphore(maxItems, maxItems); this.freeItems = new InterlockedStack <PooledSocket>(); }
internal InternalPoolImpl(MemcachedNode ownerNode, ISocketPoolConfiguration config) { if (config.MinPoolSize < 0) { throw new InvalidOperationException("Min pool size must be larger >= 0", null); } if (config.MaxPoolSize < config.MinPoolSize) { throw new InvalidOperationException("Max pool size must be larger than min pool size", null); } if (config.QueueTimeout < TimeSpan.Zero) { throw new InvalidOperationException("queueTimeout must be >= TimeSpan.Zero", null); } this._ownerNode = ownerNode; this._endpoint = ownerNode.EndPoint; this._queueTimeout = config.QueueTimeout; this._minItems = config.MinPoolSize; this._maxItems = config.MaxPoolSize; this._semaphore = new Semaphore(this._maxItems, this._maxItems); this._freeItems = new InterlockedStack <PooledSocket>(); this._logger = Logger.CreateLogger <InternalPoolImpl>(); }
public ConnectionPool(DefaultPoolControl ctrl) { _state = State.Initializing; _ctrl = ctrl; _poolCounter = new Counter(); _stackOld = new InterlockedStack(); _stackNew = new InterlockedStack(); _waitHandles = new ObjectPoolWaitHandle[3]; _waitHandles[SEMAPHORE_HANDLE] = CreateWaitHandle(SafeNativeMethods.CreateSemaphore(ADP.PtrZero, 0, MAX_Q_SIZE, ADP.PtrZero), false); _waitHandles[ERROR_HANDLE] = CreateWaitHandle(SafeNativeMethods.CreateEvent(ADP.PtrZero, 1, 0, ADP.PtrZero), false); _creationMutex = new Mutex(); _waitHandles[CREATION_HANDLE] = CreateWaitHandle(_creationMutex.Handle, true); _errorWait = ERROR_WAIT_DEFAULT; _cleanupWait = 0; // Set in CreateCleanupTimer _errorTimer = null; // No error yet. _connections = new ArrayList(_ctrl.MaxPool); if (ctrl.TransactionAffinity) { if (SQL.IsPlatformNT5()) { _txPool = CreateResourcePool(); } } _cleanupTimer = CreateCleanupTimer(); _state = State.Running; // PerfCounters - this counter will never be decremented! SQL.IncrementPoolCount(); // Make sure we're at quota by posting a callback to the threadpool. ThreadPool.QueueUserWorkItem(new WaitCallback(PoolCreateRequest)); }
internal InternalPoolImpl( MemcachedNode ownerNode, ISocketPoolConfiguration config, ILogger logger) { if (config.MinPoolSize < 0) { throw new InvalidOperationException("minItems must be larger >= 0", null); } if (config.MaxPoolSize < config.MinPoolSize) { throw new InvalidOperationException("maxItems must be larger than minItems", null); } if (config.QueueTimeout < TimeSpan.Zero) { throw new InvalidOperationException("queueTimeout must be >= TimeSpan.Zero", null); } this.ownerNode = ownerNode; this.isAlive = true; _endPoint = ownerNode.EndPoint; this.queueTimeout = config.QueueTimeout; this.minItems = config.MinPoolSize; this.maxItems = config.MaxPoolSize; _semaphore = new SemaphoreSlim(maxItems, maxItems); _freeItems = new InterlockedStack <PooledSocket>(); _logger = logger; _isDebugEnabled = _logger.IsEnabled(LogLevel.Debug); }
void Dispose(bool disposing) { if (disposing && !isDisposed) { GC.SuppressFinalize(this); } if (!isDisposed) { new Timer((s) => { if (!this.isDisposed) { log.Debug("Disposing InternalPoolImpl"); IPooledSocket ps; while (this.freeItems.TryPop(out ps)) { try { ps.Dispose(); } catch (Exception e) { log.Error(e); } } this.ownerNode = null; this.semaphore.Close(); this.semaphore = null; this.freeItems = null; this.isAlive = false; this.isDisposed = true; } }, null, 1000, Timeout.Infinite); } }