/// <summary> /// 当连接使用时发生socket异常,则标记该连接不可用,在连接池中移除 /// </summary> /// <param name="connection">The connection.</param> /// <param name="exception">The exception.</param> public void ReportError(ThriftSConnection connection, Exception exception) { if (connection == null) { return; } connection.IsErrorOccurred = true; lock (this.lockObj) { this.usedConnectionSet.RemoveWhere(x => x.ConnectionSetting == connection.ConnectionSetting); var currentFreeConnections = this.freeConnectionQueue.ToArray(); this.freeConnectionQueue.Clear(); foreach (var conn in currentFreeConnections) { if (conn.ConnectionSetting != connection.ConnectionSetting) { this.freeConnectionQueue.Enqueue(conn); } } } }
/// <summary> /// Releases the specified connection. /// </summary> /// <param name="connection">The connection.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool Release(ThriftSConnection connection) { // 异常连接不需要再放回池里 if (connection == null || connection.IsErrorOccurred) { return(false); } lock (this.lockObj) { var fcount = this.freeConnectionQueue.Count; var ucount = this.usedConnectionSet.Count; this.usedConnectionSet.Remove(connection); if (this.IsAlive(connection)) { this.freeConnectionQueue.Enqueue(connection); } Trace.WriteLine(string.Format( "release connection, server: {0}, free: {1} -> {2}, used: {3} -> {4}", this.ConnectionSetting, fcount, this.freeConnectionQueue.Count, ucount, this.usedConnectionSet.Count)); } return(true); }
/// <summary> /// Determines whether the connection is alive. /// </summary> /// <param name="connection">The connection.</param> /// <returns>True if alive; otherwise false.</returns> private bool IsAlive(ThriftSConnection connection) { if (this.KeepaliveTime > TimeSpan.Zero && connection.LastActiveTime.Add(this.KeepaliveTime) < DateTime.UtcNow) { return(false); } return(connection.IsOpen); }
/// <summary> /// Borrows this instance. /// </summary> /// <returns>ThriftSConnection.</returns> public ThriftSConnection Borrow() { ThriftSConnection conn = null; lock (this.lockObj) { var stopwatch = new Stopwatch(); var fcount = this.freeConnectionQueue.Count; var ucount = this.usedConnectionSet.Count; var isnew = false; stopwatch.Start(); if (this.freeConnectionQueue.Count > 0) { conn = this.freeConnectionQueue.Dequeue(); conn.Open(); this.usedConnectionSet.Add(conn); } else if (this.freeConnectionQueue.Count + this.usedConnectionSet.Count >= this.MaxPoolSize) { if (!Monitor.Wait(this.lockObj, TimeSpan.FromSeconds(30))) { throw new TimeoutException( "No connection could be made, timed out trying to acquire a connection from the connection pool."); } return(this.Borrow()); } else { conn = new ThriftSConnection(this.ConnectionSetting); conn.Open(); this.usedConnectionSet.Add(conn); isnew = true; } stopwatch.Stop(); Trace.WriteLine(string.Format( "borrow connection. server: {0}, free: {1} -> {2}, used: {3} -> {4}, isnew: {5}, took: {6}ms.", this.ConnectionSetting, fcount, this.freeConnectionQueue.Count, ucount, this.usedConnectionSet.Count, isnew, stopwatch.ElapsedMilliseconds)); } return(conn); }