private PooledSocket CreateSocket() { PooledSocket retval = new PooledSocket(endPoint, config.ConnectionTimeout, config.ReceiveTimeout, ReleaseSocket); retval.Reset(); return(retval); }
/// <summary> /// Acquires a new item from the pool /// </summary> /// <returns>An <see cref="T:PooledSocket"/> instance which is connected to the memcached server, or <value>null</value> if the pool is dead.</returns> public PooledSocket Acquire() { bool hasDebug = log.IsDebugEnabled; if (hasDebug) { log.Debug("Acquiring stream from pool. " + this.endPoint); } if (!this.isAlive || this.isDisposed) { if (hasDebug) { log.Debug("Pool is dead or disposed, returning null. " + this.endPoint); } return(null); } PooledSocket retval = null; if (!this.semaphore.WaitOne(this.queueTimeout)) { if (hasDebug) { log.Debug("Pool is full, timeouting. " + this.endPoint); } // everyone is so busy throw new TimeoutException(); } // maybe we died while waiting if (!this.isAlive) { if (hasDebug) { log.Debug("Pool is dead, returning null. " + this.endPoint); } return(null); } // do we have free items? if (this.freeItems.TryPop(out retval)) { #region [ get it from the pool ] try { retval.Reset(); if (hasDebug) { log.Debug("Socket was reset. " + retval.InstanceId); } return(retval); } catch (Exception e) { log.Error("Failed to reset an acquired socket.", e); this.MarkAsDead(); return(null); } #endregion } // free item pool is empty if (hasDebug) { log.Debug("Could not get a socket from the pool, Creating a new item. " + this.endPoint); } try { // okay, create the new item retval = this.CreateSocket(); } catch (Exception e) { log.Error("Failed to create socket. " + this.endPoint, e); // eventhough this item failed the failure policy may keep the pool alive // so we need to make sure to release the semaphore, so new connections can be // acquired or created (otherwise dead conenctions would "fill up" the pool // while the FP pretends that the pool is healthy) semaphore.Release(); this.MarkAsDead(); return(null); } if (hasDebug) { log.Debug("Done."); } return(retval); }
/// <summary> /// Acquires a new item from the pool /// </summary> /// <returns>An <see cref="T:PooledSocket"/> instance which is connected to the memcached server, or <value>null</value> if the pool is dead.</returns> public IPooledSocketResult Acquire() { var result = new PooledSocketResult(); var message = string.Empty; if (_isDebugEnabled) { _logger.LogDebug("Acquiring stream from pool. " + this.endPoint); } if (!this.isAlive || this.isDisposed) { message = "Pool is dead or disposed, returning null. " + this.endPoint; result.Fail(message); if (_isDebugEnabled) { _logger.LogDebug(message); } return(result); } PooledSocket retval = null; if (!this.semaphore.WaitOne(this.queueTimeout)) { message = "Pool is full, timeouting. " + this.endPoint; if (_isDebugEnabled) { _logger.LogDebug(message); } result.Fail(message, new TimeoutException()); // everyone is so busy return(result); } // maybe we died while waiting if (!this.isAlive) { message = "Pool is dead, returning null. " + this.endPoint; if (_isDebugEnabled) { _logger.LogDebug(message); } result.Fail(message); return(result); } // do we have free items? if (this.freeItems.TryPop(out retval)) { #region [ get it from the pool ] try { retval.Reset(); message = "Socket was reset. " + retval.InstanceId; if (_isDebugEnabled) { _logger.LogDebug(message); } result.Pass(message); result.Value = retval; return(result); } catch (Exception e) { message = "Failed to reset an acquired socket."; _logger.LogError(message, e); this.MarkAsDead(); result.Fail(message, e); return(result); } #endregion } // free item pool is empty message = "Could not get a socket from the pool, Creating a new item. " + this.endPoint; if (_isDebugEnabled) { _logger.LogDebug(message); } try { // okay, create the new item var startTime = DateTime.Now; retval = this.CreateSocket(); _logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds); result.Value = retval; result.Pass(); } catch (Exception e) { message = "Failed to create socket. " + this.endPoint; _logger.LogError(message, e); // eventhough this item failed the failure policy may keep the pool alive // so we need to make sure to release the semaphore, so new connections can be // acquired or created (otherwise dead conenctions would "fill up" the pool // while the FP pretends that the pool is healthy) semaphore.Release(); this.MarkAsDead(); result.Fail(message); return(result); } if (_isDebugEnabled) { _logger.LogDebug("Done."); } return(result); }
/// <summary> /// Acquires a new item from the sockIOPool /// </summary> /// <returns>An <see cref="T:PooledSocket"/> instance which is connected to the memcached server, or <value>null</value> if the sockIOPool is dead.</returns> public PooledSocket Acquire() { bool hasDebug = log.IsDebugEnabled; if (hasDebug) { log.Debug("Acquiring stream from pool. " + this.endPoint); } if (!this.isAlive || this.isDisposed) { if (hasDebug) { log.Debug("Pool is dead or disposed, returning null. " + this.endPoint); } return(null); } PooledSocket retval = null; if (!this.semaphore.WaitOne(this.config.QueueTimeout)) { if (hasDebug) { log.Debug("Pool is full, timeouting. " + this.endPoint); } // everyone is so busy throw new TimeoutException(); } // maybe we died while waiting if (!this.isAlive) { if (hasDebug) { log.Debug("Pool is dead, returning null. " + this.endPoint); } return(null); } // do we have free items? if (this.freeItems.Dequeue(out retval)) { #region [ get it from the sockIOPool ] try { retval.Reset(); if (hasDebug) { log.Debug("Socket was reset. " + retval.InstanceId); } return(retval); } catch (Exception e) { log.Error("Failed to reset an acquired socket.", e); this.MarkAsDead(); return(null); } #endregion } // free item sockIOPool is empty if (hasDebug) { log.Debug("Could not get a socket from the pool, Creating a new item. " + this.endPoint); } try { // okay, create the new item retval = this.CreateSocket(); } catch (Exception e) { log.Error("Failed to create socket. " + this.endPoint, e); this.MarkAsDead(); return(null); } if (hasDebug) { log.Debug("Done."); } return(retval); }
/// <summary> /// Acquires a new item from the pool /// </summary> /// <returns>An <see cref="T:PooledSocket"/> instance which is connected to the memcached server, or <value>null</value> if the pool is dead.</returns> public PooledSocket Acquire() { if (log.IsDebugEnabled) { log.Debug("Acquiring stream from pool."); } if (!this.IsAlive) { if (log.IsDebugEnabled) { log.Debug("Pool is dead, returning null."); } return(null); } // every release signals the event, so even if the pool becomes full in the meantime // the WaitOne will succeed, and more items will be in the pool than allowed, // so reset the event when an item is inserted this.itemReleasedEvent.Reset(); PooledSocket retval = null; // do we have free items? if (this.freeItems.Dequeue(out retval)) { try { retval.Reset(); if (log.IsDebugEnabled) { log.Debug("Socket was reset. " + retval.InstanceId); } Interlocked.Increment(ref this.workingCount); return(retval); } catch (Exception e) { log.Error("Failed to reset an acquired socket.", e); this.MarkAsDead(); return(null); } } else { // free item pool is empty if (log.IsDebugEnabled) { log.Debug("Could not get a socket from the pool."); } // we are not allowed to create more, so wait for an item to be released back into the pool if (this.workingCount >= this.maxItems) { if (log.IsDebugEnabled) { log.Debug("Pool is full, wait for a release."); } // wait on the event if (!itemReleasedEvent.WaitOne(this.config.ConnectionTimeout, false)) { if (log.IsDebugEnabled) { log.Debug("Pool is still full, timeouting."); } // everyone is working throw new TimeoutException(); } } if (log.IsDebugEnabled) { log.Debug("Creating a new item."); } try { // okay, create the new item retval = this.CreateSocket(); Interlocked.Increment(ref this.workingCount); } catch (Exception e) { log.Error("Failed to create socket.", e); this.MarkAsDead(); return(null); } } if (log.IsDebugEnabled) { log.Debug("Done."); } return(retval); }