Skip to content
This repository
file 151 lines (129 sloc) 3.526 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
using System;
using System.Collections.Generic;
using System.Threading;

namespace FluentCassandra.Connections
{
public class PooledConnectionProvider : NormalConnectionProvider
{
private readonly object _lock = new object();

private readonly Queue<IConnection> _freeConnections = new Queue<IConnection>();
private readonly HashSet<IConnection> _usedConnections = new HashSet<IConnection>();
private readonly Timer _maintenanceTimer;

/// <summary>
///
/// </summary>
/// <param name="builder"></param>
public PooledConnectionProvider(IConnectionBuilder builder)
: base(builder)
{
MinPoolSize = builder.MinPoolSize;
MaxPoolSize = builder.MaxPoolSize;
ConnectionLifetime = builder.ConnectionLifetime;

_maintenanceTimer = new Timer(o => Cleanup(), null, 30000L, 30000L);
}

/// <summary>
///
/// </summary>
public int MinPoolSize { get; private set; }

/// <summary>
///
/// </summary>
public int MaxPoolSize { get; private set; }

/// <summary>
///
/// </summary>
public TimeSpan ConnectionLifetime { get; private set; }

/// <summary>
///
/// </summary>
/// <returns></returns>
public override IConnection CreateConnection()
{
IConnection conn = null;

lock (_lock)
{
if (_freeConnections.Count > 0)
{
conn = _freeConnections.Dequeue();
_usedConnections.Add(conn);
}
else if (_freeConnections.Count + _usedConnections.Count >= MaxPoolSize)
{
if (!Monitor.Wait(_lock, TimeSpan.FromSeconds(30)))
throw new TimeoutException("No connection could be made, timed out trying to acquirer a connection from the connection pool.");

return CreateConnection();
}
else
{
conn = base.CreateConnection();
_usedConnections.Add(conn);
}
}

return conn;
}

public override void ErrorOccurred(IConnection connection, Exception exc = null)
{
lock (_lock)
{
_usedConnections.RemoveWhere(x => x.Server == connection.Server);
Servers.ErrorOccurred(connection.Server, exc);

var currentFreeConnections = _freeConnections.ToArray();
_freeConnections.Clear();

foreach (var conn in currentFreeConnections)
if (conn.Server != connection.Server)
_freeConnections.Enqueue(conn);
}
}

/// <summary>
///
/// </summary>
/// <param name="connection"></param>
/// <returns></returns>
public override bool Close(IConnection connection)
{
lock (_lock)
{
_usedConnections.Remove(connection);

if (IsAlive(connection))
_freeConnections.Enqueue(connection);
}

return true;
}

/// <summary>
/// Cleans up this instance.
/// </summary>
public void Cleanup()
{
CheckFreeConnectionsAlive();
}

/// <summary>
/// Determines whether the connection is alive.
/// </summary>
/// <param name="connection">The connection.</param>
/// <returns>True if alive; otherwise false.</returns>
private bool IsAlive(IConnection connection)
{
if (ConnectionLifetime > TimeSpan.Zero && connection.Created.Add(ConnectionLifetime) < DateTime.UtcNow)
return false;

return connection.IsOpen;
}

/// <summary>
/// The check free connections alive.
/// </summary>
private void CheckFreeConnectionsAlive()
{
lock (_lock)
{
var freeConnections = _freeConnections.ToArray();
_freeConnections.Clear();

foreach (var free in freeConnections)
{
if (IsAlive(free))
_freeConnections.Enqueue(free);
else
base.Close(free);
}
}
}
}
}
Something went wrong with that request. Please try again.