Skip to content
This repository
Browse code

updated connections to have a more standarding naming connvetion with…

… other databases
  • Loading branch information...
commit 046825e7a1a490938f0d0e011189b9257170920e 1 parent 65aca98
Nick Berardi authored
BIN  FluentCassandra.sln.docstates
Binary file not shown
3  src/CassandraContext.cs
@@ -37,7 +37,7 @@ public CassandraContext(string keyspace, Server server, string username = null,
37 37
 		/// <param name="port"></param>
38 38
 		/// <param name="timeout"></param>
39 39
 		/// <param name="provider"></param>
40  
-		public CassandraContext(string keyspace, string host, int port = 9160, int timeout = 0, string username = null, string password = null)
  40
+		public CassandraContext(string keyspace, string host, int port = Server.DefaultPort, int timeout = Server.DefaultTimeout, string username = null, string password = null)
41 41
 			: this(new ConnectionBuilder(keyspace, host, port, timeout, username: username, password: password)) { }
42 42
 
43 43
 		/// <summary>
@@ -187,6 +187,7 @@ public TResult ExecuteOperation<TResult>(ContextOperation<TResult> action, bool?
187 187
 		public void Dispose()
188 188
 		{
189 189
 			Dispose(true);
  190
+			GC.SuppressFinalize(this);
190 191
 		}
191 192
 
192 193
 		/// <summary>
1  src/CassandraSession.cs
@@ -186,6 +186,7 @@ public void Login(string username, string password)
186 186
 		public void Dispose()
187 187
 		{
188 188
 			Dispose(true);
  189
+			GC.SuppressFinalize(this);
189 190
 		}
190 191
 
191 192
 		/// <summary>
3  src/Connections/Connection.cs
@@ -27,7 +27,7 @@ internal Connection(Server server)
27 27
 			Created = DateTime.Now;
28 28
 			Server = server;
29 29
 
30  
-			TTransport socket = new TSocket(server.Host, server.Port, server.Timeout);
  30
+			TTransport socket = new TSocket(server.Host, server.Port, server.Timeout * 1000);
31 31
 
32 32
 			_transport = new TFramedTransport(socket);
33 33
 			_protocol = new TBinaryProtocol(_transport);
@@ -109,6 +109,7 @@ public override string ToString()
109 109
 		public void Dispose()
110 110
 		{
111 111
 			Dispose(true);
  112
+			GC.SuppressFinalize(this);
112 113
 		}
113 114
 
114 115
 		/// <summary>
127  src/Connections/ConnectionBuilder.cs
@@ -14,15 +14,15 @@ public class ConnectionBuilder
14 14
 		/// <param name="host"></param>
15 15
 		/// <param name="port"></param>
16 16
 		/// <param name="timeout"></param>
17  
-		/// <param name="provider"></param>
18  
-		public ConnectionBuilder(string keyspace, string host, int port = 9160, int timeout = 0, bool pooled = false, int poolSize = 25, int lifetime = 0, string username = null, string password = null)
  17
+		public ConnectionBuilder(string keyspace, string host, int port = Server.DefaultPort, int connectionTimeout = Server.DefaultTimeout, bool pooling = false, int minPoolSize = 0, int maxPoolSize = 100, int connectionLifetime = 0, string username = null, string password = null)
19 18
 		{
20 19
 			Keyspace = keyspace;
21 20
 			Servers = new List<Server>() { new Server(host, port) };
22  
-			Timeout = timeout;
23  
-			Pooled = pooled;
24  
-			PoolSize = poolSize;
25  
-			Lifetime = lifetime;
  21
+			ConnectionTimeout = connectionTimeout;
  22
+			Pooling = pooling;
  23
+			MinPoolSize = minPoolSize;
  24
+			MaxPoolSize = maxPoolSize;
  25
+			ConnectionLifetime = connectionLifetime;
26 26
 			ConnectionString = GetConnectionString();
27 27
 			ReadConsistency = ConsistencyLevel.QUORUM;
28 28
 			WriteConsistency = ConsistencyLevel.QUORUM;
@@ -68,23 +68,23 @@ private void InitializeConnectionString(string connectionString)
68 68
 
69 69
 			#endregion
70 70
 
71  
-			#region Timeout
  71
+			#region ConnectionTimeout
72 72
 
73  
-			if (!pairs.ContainsKey("Timeout"))
  73
+			if (!pairs.ContainsKey("Connection Timeout"))
74 74
 			{
75  
-				Timeout = 0;
  75
+				ConnectionTimeout = 0;
76 76
 			}
77 77
 			else
78 78
 			{
79  
-				int timeout;
  79
+				int connectionTimeout;
80 80
 
81  
-				if (!Int32.TryParse(pairs["Timeout"], out timeout))
82  
-					throw new CassandraException("Timeout is not valid.");
  81
+				if (!Int32.TryParse(pairs["Connection Timeout"], out connectionTimeout))
  82
+					throw new CassandraException("Connection Timeout is not valid.");
83 83
 
84  
-				if (timeout < 0)
85  
-					timeout = 0;
  84
+				if (connectionTimeout < 0)
  85
+					connectionTimeout = 0;
86 86
 				
87  
-				Timeout = timeout;
  87
+				ConnectionTimeout = connectionTimeout;
88 88
 			}
89 89
 
90 90
 			#endregion
@@ -109,9 +109,9 @@ private void InitializeConnectionString(string connectionString)
109 109
 					{
110 110
 						int port;
111 111
 						if (Int32.TryParse(serverParts[1], out port))
112  
-							Servers.Add(new Server(host: host, port: port, timeout: Timeout));
  112
+							Servers.Add(new Server(host: host, port: port, timeout: ConnectionTimeout));
113 113
 						else
114  
-							Servers.Add(new Server(host: host, timeout: Timeout));
  114
+							Servers.Add(new Server(host: host, timeout: ConnectionTimeout));
115 115
 					}
116 116
 					else
117 117
 						Servers.Add(new Server(host));
@@ -120,59 +120,80 @@ private void InitializeConnectionString(string connectionString)
120 120
 
121 121
 			#endregion
122 122
 
123  
-			#region Pooled
  123
+			#region Pooling
124 124
 
125  
-			if (!pairs.ContainsKey("Pooled"))
  125
+			if (!pairs.ContainsKey("Pooling"))
126 126
 			{
127  
-				Pooled = false;
  127
+				Pooling = false;
128 128
 			}
129 129
 			else
130 130
 			{
131  
-				bool pooled;
  131
+				bool pooling;
132 132
 
133  
-				if (!Boolean.TryParse(pairs["Pooled"], out pooled))
134  
-					pooled = false;
  133
+				if (!Boolean.TryParse(pairs["Pooling"], out pooling))
  134
+					pooling = false;
135 135
 
136  
-				Pooled = pooled;
  136
+				Pooling = pooling;
137 137
 			}
138 138
 
139 139
 			#endregion
140 140
 
141  
-			#region PoolSize
  141
+			#region MinPoolSize
142 142
 
143  
-			if (!pairs.ContainsKey("PoolSize"))
  143
+			if (!pairs.ContainsKey("Min Pool Size"))
144 144
 			{
145  
-				PoolSize = 25;
  145
+				MinPoolSize = 0;
146 146
 			}
147 147
 			else
148 148
 			{
149  
-				int poolSize;
  149
+				int minPoolSize;
150 150
 
151  
-				if (!Int32.TryParse(pairs["PoolSize"], out poolSize))
152  
-					poolSize = 25;
  151
+				if (!Int32.TryParse(pairs["Min Pool Size"], out minPoolSize))
  152
+					minPoolSize = 0;
153 153
 
154  
-				if (poolSize < 0)
155  
-					poolSize = 25;
  154
+				if (minPoolSize < 0)
  155
+					minPoolSize = 0;
156 156
 
157  
-				PoolSize = poolSize;
  157
+				MinPoolSize = minPoolSize;
158 158
 			}
159 159
 
160 160
 			#endregion
161 161
 
162  
-			#region Lifetime
  162
+			#region MaxPoolSize
163 163
 
164  
-			if (!pairs.ContainsKey("Lifetime"))
  164
+			if (!pairs.ContainsKey("Max Pool Size"))
165 165
 			{
166  
-				Lifetime = 0;
  166
+				MaxPoolSize = 100;
  167
+			}
  168
+			else
  169
+			{
  170
+				int maxPoolSize;
  171
+
  172
+				if (!Int32.TryParse(pairs["Max Pool Size"], out maxPoolSize))
  173
+					maxPoolSize = 100;
  174
+
  175
+				if (maxPoolSize < 0)
  176
+					maxPoolSize = 100;
  177
+
  178
+				MaxPoolSize = maxPoolSize;
  179
+			}
  180
+
  181
+			#endregion
  182
+
  183
+			#region ConnectionLifetime
  184
+
  185
+			if (!pairs.ContainsKey("Connection Lifetime"))
  186
+			{
  187
+				ConnectionLifetime = 0;
167 188
 			}
168 189
 			else
169 190
 			{
170 191
 				int lifetime;
171 192
 
172  
-				if (!Int32.TryParse(pairs["Lifetime"], out lifetime))
  193
+				if (!Int32.TryParse(pairs["Connection Lifetime"], out lifetime))
173 194
 					lifetime = 0;
174 195
 
175  
-				Lifetime = lifetime;
  196
+				ConnectionLifetime = lifetime;
176 197
 			}
177 198
 
178 199
 			#endregion
@@ -239,10 +260,11 @@ private string GetConnectionString()
239 260
 
240 261
 			b.AppendFormat(format, "Keyspace", Keyspace);
241 262
 			b.AppendFormat(format, "Server", String.Join(",", Servers));
242  
-			b.AppendFormat(format, "Timeout", Timeout);
243  
-			b.AppendFormat(format, "Pooled", Pooled);
244  
-			b.AppendFormat(format, "PoolSize", PoolSize);
245  
-			b.AppendFormat(format, "Lifetime", Lifetime);
  263
+			b.AppendFormat(format, "Connection Timeout", ConnectionTimeout);
  264
+			b.AppendFormat(format, "Pooling", Pooling);
  265
+			b.AppendFormat(format, "Min Pool Size", MinPoolSize);
  266
+			b.AppendFormat(format, "Max Pool Size", MaxPoolSize);
  267
+			b.AppendFormat(format, "Connection Lifetime", ConnectionLifetime);
246 268
 			b.AppendFormat(format, "Read", ReadConsistency);
247 269
 			b.AppendFormat(format, "Write", WriteConsistency);
248 270
 			b.AppendFormat(format, "Username", Username);
@@ -257,24 +279,29 @@ private string GetConnectionString()
257 279
 		public string Keyspace { get; private set; }
258 280
 
259 281
 		/// <summary>
260  
-		/// 
  282
+		/// The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.
261 283
 		/// </summary>
262  
-		public int Timeout { get; private set; }
  284
+		public int ConnectionTimeout { get; private set; }
263 285
 
264 286
 		/// <summary>
265  
-		/// 
  287
+		/// When true, the Connection object is drawn from the appropriate pool, or if necessary, is created and added to the appropriate pool. Recognized values are true, false, yes, and no.
266 288
 		/// </summary>
267  
-		public int PoolSize { get; private set; }
  289
+		public bool Pooling { get; private set; }
268 290
 
269 291
 		/// <summary>
270  
-		/// 
  292
+		/// (Not Currently Implimented) The minimum number of connections allowed in the pool.
271 293
 		/// </summary>
272  
-		public int Lifetime { get; private set; }
  294
+		public int MinPoolSize { get; private set; }
273 295
 
274 296
 		/// <summary>
275  
-		/// 
  297
+		/// The maximum number of connections allowed in the pool.
  298
+		/// </summary>
  299
+		public int MaxPoolSize { get; private set; }
  300
+
  301
+		/// <summary>
  302
+		/// When a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by Connection Lifetime. This is useful in clustered configurations to force load balancing between a running server and a server just brought online. A value of zero (0) causes pooled connections to have the maximum connection timeout.
276 303
 		/// </summary>
277  
-		public bool Pooled { get; private set; }
  304
+		public int ConnectionLifetime { get; private set; }
278 305
 
279 306
 		/// <summary>
280 307
 		/// 
2  src/Connections/ConnectionProviderFactory.cs
@@ -26,7 +26,7 @@ public static IConnectionProvider Get(ConnectionBuilder connectionBuilder)
26 26
 
27 27
 		private static IConnectionProvider CreateProvider(ConnectionBuilder builder)
28 28
 		{
29  
-			if (builder.Pooled)
  29
+			if (builder.Pooling)
30 30
 				return new PooledConnectionProvider(builder);
31 31
 			else
32 32
 				return new NormalConnectionProvider(builder);
6  src/Connections/NormalConnectionProvider.cs
@@ -13,16 +13,16 @@ public class NormalConnectionProvider : ConnectionProvider
13 13
 		public NormalConnectionProvider(ConnectionBuilder builder)
14 14
 			: base(builder)
15 15
 		{
16  
-			if (builder.Servers.Count > 1 && builder.Timeout == 0)
  16
+			if (builder.Servers.Count > 1 && builder.ConnectionTimeout == 0)
17 17
 				throw new CassandraException("You must specify a timeout when using multiple servers.");
18 18
 
19  
-			Timeout = builder.Timeout;
  19
+			ConnectionTimeout = builder.ConnectionTimeout;
20 20
 		}
21 21
 
22 22
 		/// <summary>
23 23
 		/// 
24 24
 		/// </summary>
25  
-		public int Timeout { get; private set; }
  25
+		public int ConnectionTimeout { get; private set; }
26 26
 
27 27
 		/// <summary>
28 28
 		/// 
18  src/Connections/PooledConnectionProvider.cs
@@ -19,8 +19,9 @@ public class PooledConnectionProvider : NormalConnectionProvider
19 19
 		public PooledConnectionProvider(ConnectionBuilder builder)
20 20
 			: base(builder)
21 21
 		{
22  
-			PoolSize = builder.PoolSize;
23  
-			Lifetime = builder.Lifetime;
  22
+			MinPoolSize = builder.MinPoolSize;
  23
+			MaxPoolSize = builder.MaxPoolSize;
  24
+			ConnectionLifetime = builder.ConnectionLifetime;
24 25
 
25 26
 			_maintenanceTimer = new Timer(o => Cleanup(), null, 30000L, 30000L);
26 27
 		}
@@ -28,12 +29,17 @@ public PooledConnectionProvider(ConnectionBuilder builder)
28 29
 		/// <summary>
29 30
 		/// 
30 31
 		/// </summary>
31  
-		public int PoolSize { get; private set; }
  32
+		public int MinPoolSize { get; private set; }
32 33
 
33 34
 		/// <summary>
34 35
 		/// 
35 36
 		/// </summary>
36  
-		public int Lifetime { get; private set; }
  37
+		public int MaxPoolSize { get; private set; }
  38
+
  39
+		/// <summary>
  40
+		/// 
  41
+		/// </summary>
  42
+		public int ConnectionLifetime { get; private set; }
37 43
 
38 44
 		/// <summary>
39 45
 		/// 
@@ -50,7 +56,7 @@ public override IConnection CreateConnection()
50 56
 					conn = _freeConnections.Dequeue();
51 57
 					_usedConnections.Add(conn);
52 58
 				}
53  
-				else if (_freeConnections.Count + _usedConnections.Count >= PoolSize)
  59
+				else if (_freeConnections.Count + _usedConnections.Count >= MaxPoolSize)
54 60
 				{
55 61
 					if (!Monitor.Wait(_lock, TimeSpan.FromSeconds(30)))
56 62
 						throw new CassandraException("No connection could be made, timed out trying to aquire a connection from the connection pool.");
@@ -100,7 +106,7 @@ public void Cleanup()
100 106
 		/// <returns>True if alive; otherwise false.</returns>
101 107
 		private bool IsAlive(IConnection connection)
102 108
 		{
103  
-			if (Lifetime > 0 && connection.Created.AddMilliseconds(Lifetime) < DateTime.Now)
  109
+			if (ConnectionLifetime > 0 && connection.Created.AddSeconds(ConnectionLifetime) < DateTime.Now)
104 110
 				return false;
105 111
 
106 112
 			return connection.IsOpen;
2  src/Connections/Server.cs
@@ -22,7 +22,7 @@ public Server(string host = "127.0.0.1", int port = DefaultPort, int timeout = D
22 22
 
23 23
 		public override string ToString()
24 24
 		{
25  
-			return String.Concat(Host, ":", Port, "-", Timeout);
  25
+			return String.Concat(Host, ":", Port, ",", Timeout, " secs");
26 26
 		}
27 27
 	}
28 28
 }
1  src/FluentCassandra.csproj
@@ -265,6 +265,7 @@
265 265
       <SubType>Designer</SubType>
266 266
     </None>
267 267
   </ItemGroup>
  268
+  <ItemGroup />
268 269
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
269 270
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
270 271
        Other similar extension points exist, see Microsoft.Common.targets.
9  test/FluentCassandra.Sandbox/Program.cs
... ...
@@ -1,14 +1,7 @@
1 1
 using System;
2  
-using System.Collections.Generic;
3 2
 using System.Linq;
4  
-using System.Text;
5  
-using Thrift.Transport;
6  
-using Thrift.Protocol;
7  
-
8  
-using FluentCassandra.Configuration;
9  
-using FluentCassandra.Types;
10 3
 using System.Threading;
11  
-using Apache.Cassandra;
  4
+using FluentCassandra.Types;
12 5
 
13 6
 namespace FluentCassandra.Sandbox
14 7
 {

0 notes on commit 046825e

Please sign in to comment.
Something went wrong with that request. Please try again.