ClusterKey.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Copyright 2010-present 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 string _sdamLogFilename;
  54. private readonly int _sendBufferSize;
  55. private readonly IReadOnlyList<MongoServerAddress> _servers;
  56. private readonly TimeSpan _serverSelectionTimeout;
  57. private readonly TimeSpan _socketTimeout;
  58. private readonly SslSettings _sslSettings;
  59. private readonly bool _useSsl;
  60. private readonly bool _verifySslCertificate;
  61. private readonly int _waitQueueSize;
  62. private readonly TimeSpan _waitQueueTimeout;
  63. // constructors
  64. public ClusterKey(
  65. string applicationName,
  66. Action<ClusterBuilder> clusterConfigurator,
  67. ConnectionMode connectionMode,
  68. TimeSpan connectTimeout,
  69. IReadOnlyList<MongoCredential> credentials,
  70. TimeSpan heartbeatInterval,
  71. TimeSpan heartbeatTimeout,
  72. bool ipv6,
  73. TimeSpan localThreshold,
  74. TimeSpan maxConnectionIdleTime,
  75. TimeSpan maxConnectionLifeTime,
  76. int maxConnectionPoolSize,
  77. int minConnectionPoolSize,
  78. string replicaSetName,
  79. string sdamLogFilename,
  80. IReadOnlyList<MongoServerAddress> servers,
  81. TimeSpan serverSelectionTimeout,
  82. TimeSpan socketTimeout,
  83. SslSettings sslSettings,
  84. bool useSsl,
  85. bool verifySslCertificate,
  86. int waitQueueSize,
  87. TimeSpan waitQueueTimeout)
  88. {
  89. _applicationName = applicationName;
  90. _clusterConfigurator = clusterConfigurator;
  91. _connectionMode = connectionMode;
  92. _connectTimeout = connectTimeout;
  93. _credentials = credentials;
  94. _heartbeatInterval = heartbeatInterval;
  95. _heartbeatTimeout = heartbeatTimeout;
  96. _ipv6 = ipv6;
  97. _localThreshold = localThreshold;
  98. _maxConnectionIdleTime = maxConnectionIdleTime;
  99. _maxConnectionLifeTime = maxConnectionLifeTime;
  100. _maxConnectionPoolSize = maxConnectionPoolSize;
  101. _minConnectionPoolSize = minConnectionPoolSize;
  102. _receiveBufferSize = __defaultReceiveBufferSize; // TODO: add ReceiveBufferSize to MongoServerSettings?
  103. _replicaSetName = replicaSetName;
  104. _sdamLogFilename = sdamLogFilename;
  105. _sendBufferSize = __defaultSendBufferSize; // TODO: add SendBufferSize to MongoServerSettings?
  106. _servers = servers;
  107. _serverSelectionTimeout = serverSelectionTimeout;
  108. _socketTimeout = socketTimeout;
  109. _sslSettings = sslSettings;
  110. _useSsl = useSsl;
  111. _verifySslCertificate = verifySslCertificate;
  112. _waitQueueSize = waitQueueSize;
  113. _waitQueueTimeout = waitQueueTimeout;
  114. _hashCode = CalculateHashCode();
  115. }
  116. // properties
  117. public string ApplicationName { get { return _applicationName; } }
  118. public Action<ClusterBuilder> ClusterConfigurator { get { return _clusterConfigurator; } }
  119. public ConnectionMode ConnectionMode { get { return _connectionMode; } }
  120. public TimeSpan ConnectTimeout { get { return _connectTimeout; } }
  121. public IReadOnlyList<MongoCredential> Credentials { get { return _credentials; } }
  122. public TimeSpan HeartbeatInterval { get { return _heartbeatInterval; } }
  123. public TimeSpan HeartbeatTimeout { get { return _heartbeatTimeout; } }
  124. public bool IPv6 { get { return _ipv6; } }
  125. public TimeSpan LocalThreshold { get { return _localThreshold; } }
  126. public TimeSpan MaxConnectionIdleTime { get { return _maxConnectionIdleTime; } }
  127. public TimeSpan MaxConnectionLifeTime { get { return _maxConnectionLifeTime; } }
  128. public int MaxConnectionPoolSize { get { return _maxConnectionPoolSize; } }
  129. public int MinConnectionPoolSize { get { return _minConnectionPoolSize; } }
  130. public int ReceiveBufferSize { get { return _receiveBufferSize; } }
  131. public string ReplicaSetName { get { return _replicaSetName; } }
  132. public string SdamLogFilename { get { return _sdamLogFilename; }}
  133. public int SendBufferSize { get { return _sendBufferSize; } }
  134. public IReadOnlyList<MongoServerAddress> Servers { get { return _servers; } }
  135. public TimeSpan ServerSelectionTimeout { get { return _serverSelectionTimeout; } }
  136. public TimeSpan SocketTimeout { get { return _socketTimeout; } }
  137. public SslSettings SslSettings { get { return _sslSettings; } }
  138. public bool UseSsl { get { return _useSsl; } }
  139. public bool VerifySslCertificate { get { return _verifySslCertificate; } }
  140. public int WaitQueueSize { get { return _waitQueueSize; } }
  141. public TimeSpan WaitQueueTimeout { get { return _waitQueueTimeout; } }
  142. // methods
  143. private int CalculateHashCode()
  144. {
  145. // keep calculation simple (leave out fields that are rarely used)
  146. return new Hasher()
  147. .HashElements(_credentials)
  148. .HashElements(_servers)
  149. .GetHashCode();
  150. }
  151. public override bool Equals(object obj)
  152. {
  153. if (obj == null || obj.GetType() != typeof(ClusterKey))
  154. {
  155. return false;
  156. }
  157. var rhs = (ClusterKey)obj;
  158. return
  159. _hashCode == rhs._hashCode && // fail fast
  160. _applicationName == rhs._applicationName &&
  161. object.ReferenceEquals(_clusterConfigurator, rhs._clusterConfigurator) &&
  162. _connectionMode == rhs._connectionMode &&
  163. _connectTimeout == rhs._connectTimeout &&
  164. _credentials.SequenceEqual(rhs._credentials) &&
  165. _heartbeatInterval == rhs._heartbeatInterval &&
  166. _heartbeatTimeout == rhs._heartbeatTimeout &&
  167. _ipv6 == rhs._ipv6 &&
  168. _localThreshold == rhs._localThreshold &&
  169. _maxConnectionIdleTime == rhs._maxConnectionIdleTime &&
  170. _maxConnectionLifeTime == rhs._maxConnectionLifeTime &&
  171. _maxConnectionPoolSize == rhs._maxConnectionPoolSize &&
  172. _minConnectionPoolSize == rhs._minConnectionPoolSize &&
  173. _receiveBufferSize == rhs._receiveBufferSize &&
  174. _replicaSetName == rhs._replicaSetName &&
  175. _sdamLogFilename == rhs._sdamLogFilename &&
  176. _sendBufferSize == rhs._sendBufferSize &&
  177. _servers.SequenceEqual(rhs._servers) &&
  178. _serverSelectionTimeout == rhs._serverSelectionTimeout &&
  179. _socketTimeout == rhs._socketTimeout &&
  180. object.Equals(_sslSettings, rhs._sslSettings) &&
  181. _useSsl == rhs._useSsl &&
  182. _verifySslCertificate == rhs._verifySslCertificate &&
  183. _waitQueueSize == rhs._waitQueueSize &&
  184. _waitQueueTimeout == rhs._waitQueueTimeout;
  185. }
  186. public override int GetHashCode()
  187. {
  188. return _hashCode;
  189. }
  190. }
  191. }