MongoDefaults.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.Text;
  17. using System.Threading;
  18. using MongoDB.Bson;
  19. using MongoDB.Bson.IO;
  20. namespace MongoDB.Driver
  21. {
  22. /// <summary>
  23. /// Default values for various Mongo settings.
  24. /// </summary>
  25. public static class MongoDefaults
  26. {
  27. // private static fields
  28. private static bool __assignIdOnInsert = true;
  29. private static string __authenticationMechanism = null;
  30. private static TimeSpan __connectTimeout = TimeSpan.FromSeconds(30);
  31. private static TimeSpan __localThreshold = TimeSpan.FromMilliseconds(15);
  32. private static TimeSpan __maxConnectionIdleTime = TimeSpan.FromMinutes(10);
  33. private static TimeSpan __maxConnectionLifeTime = TimeSpan.FromMinutes(30);
  34. private static int __maxBatchCount = 1000;
  35. private static int __maxConnectionPoolSize = 100;
  36. private static int __maxMessageLength = 16000000; // 16MB (not 16 MiB!)
  37. private static int __minConnectionPoolSize = 0;
  38. private static TimeSpan __operationTimeout = TimeSpan.FromSeconds(30);
  39. private static UTF8Encoding __readEncoding = Utf8Encodings.Strict;
  40. private static TimeSpan __serverSelectionTimeout = TimeSpan.FromSeconds(30);
  41. private static TimeSpan __socketTimeout = TimeSpan.Zero; // use operating system default (presumably infinite)
  42. private static int __tcpReceiveBufferSize = 64 * 1024; // 64KiB (note: larger than 2MiB fails on Mac using Mono)
  43. private static int __tcpSendBufferSize = 64 * 1024; // 64KiB (TODO: what is the optimum value for the buffers?)
  44. private static double __waitQueueMultiple = 5.0; // default wait queue multiple is 5.0
  45. private static int __waitQueueSize = 0; // use multiple by default
  46. private static TimeSpan __waitQueueTimeout = TimeSpan.FromMinutes(2); // default wait queue timeout is 2 minutes
  47. private static UTF8Encoding __writeEncoding = Utf8Encodings.Strict;
  48. private static int __maxDocumentSize = 4 * 1024 * 1024; // 4 MiB. Original MongoDB max document size
  49. // public static properties
  50. /// <summary>
  51. /// Gets or sets whether the driver should assign a value to empty Ids on Insert.
  52. /// </summary>
  53. public static bool AssignIdOnInsert
  54. {
  55. get { return __assignIdOnInsert; }
  56. set { __assignIdOnInsert = value; }
  57. }
  58. /// <summary>
  59. /// Gets or sets the default authentication mechanism.
  60. /// </summary>
  61. public static string AuthenticationMechanism
  62. {
  63. get { return __authenticationMechanism; }
  64. set { __authenticationMechanism = value; }
  65. }
  66. /// <summary>
  67. /// Gets the actual wait queue size (either WaitQueueSize or WaitQueueMultiple x MaxConnectionPoolSize).
  68. /// </summary>
  69. public static int ComputedWaitQueueSize
  70. {
  71. get
  72. {
  73. if (__waitQueueMultiple == 0.0)
  74. {
  75. return __waitQueueSize;
  76. }
  77. else
  78. {
  79. return (int)(__waitQueueMultiple * __maxConnectionPoolSize);
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Gets or sets the connect timeout.
  85. /// </summary>
  86. public static TimeSpan ConnectTimeout
  87. {
  88. get { return __connectTimeout; }
  89. set { __connectTimeout = value; }
  90. }
  91. /// <summary>
  92. /// Gets or sets the representation to use for Guids (this is an alias for BsonDefaults.GuidRepresentation).
  93. /// </summary>
  94. public static GuidRepresentation GuidRepresentation
  95. {
  96. get { return BsonDefaults.GuidRepresentation; }
  97. set { BsonDefaults.GuidRepresentation = value; }
  98. }
  99. /// <summary>
  100. /// Gets or sets the default local threshold.
  101. /// </summary>
  102. public static TimeSpan LocalThreshold
  103. {
  104. get { return __localThreshold; }
  105. set { __localThreshold = value; }
  106. }
  107. /// <summary>
  108. /// Gets or sets the maximum batch count.
  109. /// </summary>
  110. public static int MaxBatchCount
  111. {
  112. get { return __maxBatchCount; }
  113. set { __maxBatchCount = value; }
  114. }
  115. /// <summary>
  116. /// Gets or sets the max connection idle time.
  117. /// </summary>
  118. public static TimeSpan MaxConnectionIdleTime
  119. {
  120. get { return __maxConnectionIdleTime; }
  121. set { __maxConnectionIdleTime = value; }
  122. }
  123. /// <summary>
  124. /// Gets or sets the max connection life time.
  125. /// </summary>
  126. public static TimeSpan MaxConnectionLifeTime
  127. {
  128. get { return __maxConnectionLifeTime; }
  129. set { __maxConnectionLifeTime = value; }
  130. }
  131. /// <summary>
  132. /// Gets or sets the max connection pool size.
  133. /// </summary>
  134. public static int MaxConnectionPoolSize
  135. {
  136. get { return __maxConnectionPoolSize; }
  137. set { __maxConnectionPoolSize = value; }
  138. }
  139. /// <summary>
  140. /// Gets or sets the max document size
  141. /// </summary>
  142. public static int MaxDocumentSize
  143. {
  144. get { return __maxDocumentSize; }
  145. set { __maxDocumentSize = value; }
  146. }
  147. /// <summary>
  148. /// Gets or sets the max message length.
  149. /// </summary>
  150. public static int MaxMessageLength
  151. {
  152. get { return __maxMessageLength; }
  153. set { __maxMessageLength = value; }
  154. }
  155. /// <summary>
  156. /// Gets or sets the min connection pool size.
  157. /// </summary>
  158. public static int MinConnectionPoolSize
  159. {
  160. get { return __minConnectionPoolSize; }
  161. set { __minConnectionPoolSize = value; }
  162. }
  163. /// <summary>
  164. /// Gets or sets the operation timeout.
  165. /// </summary>
  166. public static TimeSpan OperationTimeout
  167. {
  168. get { return __operationTimeout; }
  169. set { __operationTimeout = value; }
  170. }
  171. /// <summary>
  172. /// Gets or sets the Read Encoding.
  173. /// </summary>
  174. public static UTF8Encoding ReadEncoding
  175. {
  176. get { return __readEncoding; }
  177. set
  178. {
  179. if (value == null)
  180. {
  181. throw new ArgumentNullException("value");
  182. }
  183. __readEncoding = value;
  184. }
  185. }
  186. /// <summary>
  187. /// Gets or sets the server selection timeout.
  188. /// </summary>
  189. public static TimeSpan ServerSelectionTimeout
  190. {
  191. get { return __serverSelectionTimeout; }
  192. set { __serverSelectionTimeout = value; }
  193. }
  194. /// <summary>
  195. /// Gets or sets the socket timeout.
  196. /// </summary>
  197. public static TimeSpan SocketTimeout
  198. {
  199. get { return __socketTimeout; }
  200. set { __socketTimeout = value; }
  201. }
  202. /// <summary>
  203. /// Gets or sets the TCP receive buffer size.
  204. /// </summary>
  205. public static int TcpReceiveBufferSize
  206. {
  207. get { return __tcpReceiveBufferSize; }
  208. set { __tcpReceiveBufferSize = value; }
  209. }
  210. /// <summary>
  211. /// Gets or sets the TCP send buffer size.
  212. /// </summary>
  213. public static int TcpSendBufferSize
  214. {
  215. get { return __tcpSendBufferSize; }
  216. set { __tcpSendBufferSize = value; }
  217. }
  218. /// <summary>
  219. /// Gets or sets the wait queue multiple (the actual wait queue size will be WaitQueueMultiple x MaxConnectionPoolSize, see also WaitQueueSize).
  220. /// </summary>
  221. public static double WaitQueueMultiple
  222. {
  223. get { return __waitQueueMultiple; }
  224. set
  225. {
  226. __waitQueueMultiple = value;
  227. __waitQueueSize = 0;
  228. }
  229. }
  230. /// <summary>
  231. /// Gets or sets the wait queue size (see also WaitQueueMultiple).
  232. /// </summary>
  233. public static int WaitQueueSize
  234. {
  235. get { return __waitQueueSize; }
  236. set
  237. {
  238. __waitQueueMultiple = 0.0;
  239. __waitQueueSize = value;
  240. }
  241. }
  242. /// <summary>
  243. /// Gets or sets the wait queue timeout.
  244. /// </summary>
  245. public static TimeSpan WaitQueueTimeout
  246. {
  247. get { return __waitQueueTimeout; }
  248. set { __waitQueueTimeout = value; }
  249. }
  250. /// <summary>
  251. /// Gets or sets the Write Encoding.
  252. /// </summary>
  253. public static UTF8Encoding WriteEncoding
  254. {
  255. get { return __writeEncoding; }
  256. set
  257. {
  258. if (value == null)
  259. {
  260. throw new ArgumentNullException("value");
  261. }
  262. __writeEncoding = value;
  263. }
  264. }
  265. }
  266. }