/// <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() { if (!this.isInitialized) { lock (this.internalPoolImpl) if (!this.isInitialized) { this.internalPoolImpl.InitPool(); this.isInitialized = true; } } try { return(this.internalPoolImpl.Acquire()); } catch (Exception e) { var message = "Acquire failed. Maybe we're already disposed?"; log.Error(message, e); var result = new PooledSocketResult(); result.Fail(message, e); return(result); } }
/// <summary> /// Acquires a new item from the pool /// </summary> /// <returns>An <see cref="PooledSocket"/> instance which is connected to the memcached server, or <value>null</value> if the pool is dead.</returns> public IPooledSocketResult Acquire() { if (!this._isInitialized) { lock (this._internalPoolImpl) { if (!this._isInitialized) { var startTime = DateTime.Now; this._internalPoolImpl.InitPool(); this._isInitialized = true; if (this._logger.IsEnabled(LogLevel.Trace)) { this._logger.LogInformation($"Cost for initiaizing pool: {(DateTime.Now - startTime).TotalMilliseconds}ms"); } } } } try { return(this._internalPoolImpl.Acquire()); } catch (Exception e) { var message = "Acquire failed. Maybe we're already disposed?"; this._logger.LogError(e, message); var result = new PooledSocketResult(); result.Fail(message, e); return(result); } }
/// <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() { if (!this.isInitialized) { lock (this.internalPoolImpl) if (!this.isInitialized) { var startTime = DateTime.Now; this.internalPoolImpl.InitPool(); this.isInitialized = true; _logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds); } } try { return(this.internalPoolImpl.Acquire()); } catch (Exception e) { var message = "Acquire failed. Maybe we're already disposed?"; _logger.LogError(message, e); var result = new PooledSocketResult(); result.Fail(message, e); return(result); } }
public Enyim.Caching.Memcached.Results.IOperationResult Execute(IOperation op) { IConfigOperation getOp = op as IConfigOperation; byte[] bytes; switch (requestNum) { case 1: bytes = Encoding.UTF8.GetBytes(String.Format("{0}\r\ncluster.0001.use1.cache.amazon.aws.com|10.10.10.1|11211 cluster.0002.use1.cache.amazon.aws.com|10.10.10.2|11211 cluster.0003.use1.cache.amazon.aws.com|10.10.10.3|11211\r\n", this.requestNum)); break; case 2: bytes = Encoding.UTF8.GetBytes(String.Format("{0}\r\ncluster.0002.use1.cache.amazon.aws.com|10.10.10.2|11211 cluster.0003.use1.cache.amazon.aws.com|10.10.10.3|11211\r\n", this.requestNum)); break; default: bytes = Encoding.UTF8.GetBytes(String.Format("{0}\r\ncluster.0001.use1.cache.amazon.aws.com|10.10.10.1|11211\r\n", this.requestNum)); break; } this.requestNum++; var arr = new ArraySegment <byte>(bytes); getOp.ConfigResult = new CacheItem(0, arr); var result = new PooledSocketResult(); result.Success = true; return(result); }
/// <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 async Task <IPooledSocketResult> AcquireAsync() { var result = new PooledSocketResult(); if (!this.isInitialized) { if (!await poolInitSemaphore.WaitAsync(_initPoolTimeout)) { return(result.Fail("Timeout to poolInitSemaphore.Wait", _logger) as PooledSocketResult); } try { if (!this.isInitialized) { var startTime = DateTime.Now; await this.internalPoolImpl.InitPoolAsync(); this.isInitialized = true; _logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds); } } finally { poolInitSemaphore.Release(); } } try { return(await this.internalPoolImpl.AcquireAsync()); } catch (Exception e) { var message = "Acquire failed. Maybe we're already disposed?"; _logger.LogError(message, e); result.Fail(message, e); return(result); } }
public Enyim.Caching.Memcached.Results.IOperationResult Execute(IOperation op) { IConfigOperation getOp = op as IConfigOperation ?? throw new NotImplementedException(op.ToString()); byte[] bytes; switch (requestNum) { case 1: bytes = Encoding.UTF8.GetBytes( $"{requestNum}\r\ncluster.0001.use1.cache.amazon.aws.com|10.10.10.1|11211 cluster.0002.use1.cache.amazon.aws.com|10.10.10.2|11211 cluster.0003.use1.cache.amazon.aws.com|10.10.10.3|11211\r\n"); break; case 2: bytes = Encoding.UTF8.GetBytes( $"{requestNum}\r\ncluster.0002.use1.cache.amazon.aws.com|10.10.10.2|11211 cluster.0003.use1.cache.amazon.aws.com|10.10.10.3|11211\r\n"); break; default: bytes = Encoding.UTF8.GetBytes( $"{requestNum}\r\ncluster.0001.use1.cache.amazon.aws.com|10.10.10.1|11211\r\n"); break; } this.requestNum++; var arr = new ArraySegment <byte>(bytes); getOp.ConfigResult = new CacheItem(0, arr); var result = new PooledSocketResult { Success = true }; return(result); }
/// <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; bool hasDebug = log.IsDebugEnabled; if (hasDebug) { log.Debug("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 (hasDebug) { log.Debug(message); } return(result); } PooledSocket retval = null; if (!this.semaphore.WaitOne(this.queueTimeout)) { message = "Pool is full, timeouting. " + this.endPoint; if (hasDebug) { log.Debug(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 (hasDebug) { log.Debug(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 (hasDebug) { log.Debug(message); } result.Pass(message); result.Value = retval; return(result); } catch (Exception e) { message = "Failed to reset an acquired socket."; log.Error(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 (hasDebug) { log.Debug(message); } try { // okay, create the new item retval = this.CreateSocket(); result.Value = retval; result.Pass(); } catch (Exception e) { message = "Failed to create socket. " + this.endPoint; log.Error(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 (hasDebug) { log.Debug("Done."); } return(result); }
/// <summary> /// Acquires a new item from the pool /// </summary> /// <returns>An <see cref="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(); if (this._logger.IsEnabled(LogLevel.Trace)) { this._logger.LogDebug($"Acquire a socket from pool ({this._endpoint})"); } if (!this.IsAlive || this._isDisposed) { var message = $"Pool is dead or disposed, returning null ({this._endpoint})"; result.Fail(message); if (this._logger.IsEnabled(LogLevel.Trace)) { this._logger.LogWarning(message); } return(result); } // everyone is so busy if (!this._semaphore.WaitOne(this._queueTimeout)) { var message = $"Pool is full, timeouting ({this._endpoint})"; result.Fail(message, new TimeoutException()); if (this._logger.IsEnabled(LogLevel.Trace)) { this._logger.LogWarning(message); } return(result); } // maybe we died while waiting if (!this.IsAlive) { var message = $"Pool is dead, returning null ({this._endpoint})"; result.Fail(message); if (this._logger.IsEnabled(LogLevel.Trace)) { this._logger.LogWarning(message); } return(result); } // do we have free items? if (this._freeItems.TryPop(out PooledSocket socket)) { try { socket.Reset(); result.Pass(); result.Value = socket; if (this._logger.IsEnabled(LogLevel.Trace)) { this._logger.LogDebug($"Socket is ready to use ({socket.InstanceID})"); } return(result); } catch (Exception e) { this.MarkAsDead(); var message = "Failed to reset an acquired socket"; this._logger.LogError(e, message); result.Fail(message, e); return(result); } } // free item pool is empty if (this._logger.IsEnabled(LogLevel.Trace)) { this._logger.LogInformation($"Could not get a socket from the pool => create new ({this._endpoint})"); } try { // okay, create the new item var startTime = DateTime.Now; socket = this.CreateSocket(); result.Value = socket; result.Pass(); if (this._logger.IsEnabled(LogLevel.Trace)) { this._logger.LogWarning($"A new socket is created ({socket.InstanceID}). Cost for creating socket when acquire: {(DateTime.Now - startTime).TotalMilliseconds}ms"); } } catch (Exception e) { var message = $"Failed to create a new socket ({this._endpoint})"; this._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) this._semaphore.Release(); this.MarkAsDead(); result.Fail(message); return(result); } return(result); }
/// <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 async Task <IPooledSocketResult> AcquireAsync() { var result = new PooledSocketResult(); var message = string.Empty; if (_isDebugEnabled) { _logger.LogDebug("Acquiring stream from pool. " + _endPoint); } if (!this.isAlive || this.isDisposed) { message = "Pool is dead or disposed, returning null. " + _endPoint; result.Fail(message); if (_isDebugEnabled) { _logger.LogDebug(message); } return(result); } PooledSocket retval = null; if (!await _semaphore.WaitAsync(this.queueTimeout)) { message = "Pool is full, timeouting. " + _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. " + _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. " + _endPoint; if (_isDebugEnabled) { _logger.LogDebug(message); } try { // okay, create the new item var startTime = DateTime.Now; retval = await this.CreateSocketAsync(); _logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds); result.Value = retval; result.Pass(); } catch (Exception e) { message = "Failed to create socket. " + _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); }