ClusterKey.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Copyright 2010-2016 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using MongoDB.Driver.Core.Configuration;
  19. using MongoDB.Shared;
  20. namespace MongoDB.Driver
  21. {
  22. internal class ClusterKey
  23. {
  24. #region static
  25. // static fields
  26. private static readonly int __defaultReceiveBufferSize;
  27. private static readonly int __defaultSendBufferSize;
  28. // static constructor
  29. static ClusterKey()
  30. {
  31. var defaultTcpStreamSettings = new TcpStreamSettings();
  32. __defaultReceiveBufferSize = defaultTcpStreamSettings.ReceiveBufferSize;
  33. __defaultSendBufferSize = defaultTcpStreamSettings.SendBufferSize;
  34. }
  35. #endregion
  36. // fields
  37. private readonly string _applicationName;
  38. private readonly Action<ClusterBuilder> _clusterConfigurator;
  39. private readonly ConnectionMode _connectionMode;
  40. private readonly TimeSpan _connectTimeout;
  41. private readonly IReadOnlyList<MongoCredential> _credentials;
  42. private readonly int _hashCode;
  43. private readonly TimeSpan _heartbeatInterval;
  44. private readonly TimeSpan _heartbeatTimeout;
  45. private readonly bool _ipv6;
  46. private readonly TimeSpan _localThreshold;
  47. private readonly TimeSpan _maxConnectionIdleTime;
  48. private readonly TimeSpan _maxConnectionLifeTime;
  49. private readonly int _maxConnectionPoolSize;
  50. private readonly int _minConnectionPoolSize;
  51. private readonly int _receiveBufferSize;
  52. private readonly string _replicaSetName;
  53. private readonly int _sendBufferSize;
  54. private readonly IReadOnlyList<MongoServerAddress> _servers;
  55. private readonly TimeSpan _serverSelectionTimeout;
  56. private readonly TimeSpan _socketTimeout;
  57. private readonly SslSettings _sslSettings;
  58. private readonly bool _useSsl;
  59. private readonly bool _verifySslCertificate;
  60. private readonly int _waitQueueSize;
  61. private readonly TimeSpan _waitQueueTimeout;
  62. // constructors
  63. public ClusterKey(
  64. string applicationName,
  65. Action<ClusterBuilder> clusterConfigurator,
  66. ConnectionMode connectionMode,
  67. TimeSpan connectTimeout,
  68. IReadOnlyList<MongoCredential> credentials,
  69. TimeSpan heartbeatInterval,
  70. TimeSpan heartbeatTimeout,
  71. bool ipv6,
  72. TimeSpan localThreshold,
  73. TimeSpan maxConnectionIdleTime,
  74. TimeSpan maxConnectionLifeTime,
  75. int maxConnectionPoolSize,
  76. int minConnectionPoolSize,
  77. string replicaSetName,
  78. IReadOnlyList<MongoServerAddress> servers,
  79. TimeSpan serverSelectionTimeout,
  80. TimeSpan socketTimeout,
  81. SslSettings sslSettings,
  82. bool useSsl,
  83. bool verifySslCertificate,
  84. int waitQueueSize,
  85. TimeSpan waitQueueTimeout)
  86. {
  87. _applicationName = applicationName;
  88. _clusterConfigurator = clusterConfigurator;
  89. _connectionMode = connectionMode;
  90. _connectTimeout = connectTimeout;
  91. _credentials = credentials;
  92. _heartbeatInterval = heartbeatInterval;
  93. _heartbeatTimeout = heartbeatTimeout;
  94. _ipv6 = ipv6;
  95. _localThreshold = localThreshold;
  96. _maxConnectionIdleTime = maxConnectionIdleTime;
  97. _maxConnectionLifeTime = maxConnectionLifeTime;
  98. _maxConnectionPoolSize = maxConnectionPoolSize;
  99. _minConnectionPoolSize = minConnectionPoolSize;
  100. _receiveBufferSize = __defaultReceiveBufferSize; // TODO: add ReceiveBufferSize to MongoServerSettings?
  101. _replicaSetName = replicaSetName;
  102. _sendBufferSize = __defaultSendBufferSize; // TODO: add SendBufferSize to MongoServerSettings?
  103. _servers = servers;
  104. _serverSelectionTimeout = serverSelectionTimeout;
  105. _socketTimeout = socketTimeout;
  106. _sslSettings = sslSettings;
  107. _useSsl = useSsl;
  108. _verifySslCertificate = verifySslCertificate;
  109. _waitQueueSize = waitQueueSize;
  110. _waitQueueTimeout = waitQueueTimeout;
  111. _hashCode = CalculateHashCode();
  112. }
  113. // properties
  114. public string ApplicationName { get { return _applicationName; } }
  115. public Action<ClusterBuilder> ClusterConfigurator { get { return _clusterConfigurator; } }
  116. public ConnectionMode ConnectionMode { get { return _connectionMode; } }
  117. public TimeSpan ConnectTimeout { get { return _connectTimeout; } }
  118. public IReadOnlyList<MongoCredential> Credentials { get { return _credentials; } }
  119. public TimeSpan HeartbeatInterval { get { return _heartbeatInterval; } }
  120. public TimeSpan HeartbeatTimeout { get { return _heartbeatTimeout; } }
  121. public bool IPv6 { get { return _ipv6; } }
  122. public TimeSpan LocalThreshold { get { return _localThreshold; } }
  123. public TimeSpan MaxConnectionIdleTime { get { return _maxConnectionIdleTime; } }
  124. public TimeSpan MaxConnectionLifeTime { get { return _maxConnectionLifeTime; } }
  125. public int MaxConnectionPoolSize { get { return _maxConnectionPoolSize; } }
  126. public int MinConnectionPoolSize { get { return _minConnectionPoolSize; } }
  127. public int ReceiveBufferSize { get { return _receiveBufferSize; } }
  128. public string ReplicaSetName { get { return _replicaSetName; } }
  129. public int SendBufferSize { get { return _sendBufferSize; } }
  130. public IReadOnlyList<MongoServerAddress> Servers { get { return _servers; } }
  131. public TimeSpan ServerSelectionTimeout { get { return _serverSelectionTimeout; } }
  132. public TimeSpan SocketTimeout { get { return _socketTimeout; } }
  133. public SslSettings SslSettings { get { return _sslSettings; } }
  134. public bool UseSsl { get { return _useSsl; } }
  135. public bool VerifySslCertificate { get { return _verifySslCertificate; } }
  136. public int WaitQueueSize { get { return _waitQueueSize; } }
  137. public TimeSpan WaitQueueTimeout { get { return _waitQueueTimeout; } }
  138. // methods
  139. private int CalculateHashCode()
  140. {
  141. // keep calculation simple (leave out fields that are rarely used)
  142. return new Hasher()
  143. .HashElements(_credentials)
  144. .HashElements(_servers)
  145. .GetHashCode();
  146. }
  147. public override bool Equals(object obj)
  148. {
  149. if (obj == null || obj.GetType() != typeof(ClusterKey))
  150. {
  151. return false;
  152. }
  153. var rhs = (ClusterKey)obj;
  154. return
  155. _hashCode == rhs._hashCode && // fail fast
  156. _applicationName == rhs._applicationName &&
  157. object.ReferenceEquals(_clusterConfigurator, rhs._clusterConfigurator) &&
  158. _connectionMode == rhs._connectionMode &&
  159. _connectTimeout == rhs._connectTimeout &&
  160. _credentials.SequenceEqual(rhs._credentials) &&
  161. _heartbeatInterval == rhs._heartbeatInterval &&
  162. _heartbeatTimeout == rhs._heartbeatTimeout &&
  163. _ipv6 == rhs._ipv6 &&
  164. _localThreshold == rhs._localThreshold &&
  165. _maxConnectionIdleTime == rhs._maxConnectionIdleTime &&
  166. _maxConnectionLifeTime == rhs._maxConnectionLifeTime &&
  167. _maxConnectionPoolSize == rhs._maxConnectionPoolSize &&
  168. _minConnectionPoolSize == rhs._minConnectionPoolSize &&
  169. _receiveBufferSize == rhs._receiveBufferSize &&
  170. _replicaSetName == rhs._replicaSetName &&
  171. _sendBufferSize == rhs._sendBufferSize &&
  172. _servers.SequenceEqual(rhs._servers) &&
  173. _serverSelectionTimeout == rhs._serverSelectionTimeout &&
  174. _socketTimeout == rhs._socketTimeout &&
  175. object.Equals(_sslSettings, rhs._sslSettings) &&
  176. _useSsl == rhs._useSsl &&
  177. _verifySslCertificate == rhs._verifySslCertificate &&
  178. _waitQueueSize == rhs._waitQueueSize &&
  179. _waitQueueTimeout == rhs._waitQueueTimeout;
  180. }
  181. public override int GetHashCode()
  182. {
  183. return _hashCode;
  184. }
  185. }
  186. }