/driver-core/src/main/com/mongodb/connection/SocketSettings.java

https://github.com/mebigfatguy/mongo-java-driver · Java · 291 lines · 141 code · 31 blank · 119 comment · 20 complexity · 21cb1d1be9b40898758bce6d80decbcf MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb.connection;
  17. import com.mongodb.ConnectionString;
  18. import com.mongodb.annotations.Immutable;
  19. import java.util.concurrent.TimeUnit;
  20. import static com.mongodb.assertions.Assertions.notNull;
  21. import static java.util.concurrent.TimeUnit.MILLISECONDS;
  22. /**
  23. * An immutable class representing socket settings used for connections to a MongoDB server.
  24. *
  25. * @since 3.0
  26. */
  27. @Immutable
  28. public class SocketSettings {
  29. private final long connectTimeoutMS;
  30. private final long readTimeoutMS;
  31. private final boolean keepAlive;
  32. private final int receiveBufferSize;
  33. private final int sendBufferSize;
  34. /**
  35. * Gets a builder for an instance of {@code SocketSettings}.
  36. * @return the builder
  37. */
  38. public static Builder builder() {
  39. return new Builder();
  40. }
  41. /**
  42. * Creates a builder instance.
  43. *
  44. * @param socketSettings existing SocketSettings to default the builder settings on.
  45. * @return a builder
  46. * @since 3.7
  47. */
  48. public static Builder builder(final SocketSettings socketSettings) {
  49. return builder().applySettings(socketSettings);
  50. }
  51. /**
  52. * A builder for an instance of {@code SocketSettings}.
  53. */
  54. public static final class Builder {
  55. private long connectTimeoutMS = 10000;
  56. private long readTimeoutMS;
  57. private boolean keepAlive = true;
  58. private int receiveBufferSize;
  59. private int sendBufferSize;
  60. private Builder() {
  61. }
  62. /**
  63. * Applies the socketSettings to the builder
  64. *
  65. * <p>Note: Overwrites all existing settings</p>
  66. *
  67. * @param socketSettings the socketSettings
  68. * @return this
  69. * @since 3.7
  70. */
  71. public Builder applySettings(final SocketSettings socketSettings) {
  72. notNull("socketSettings", socketSettings);
  73. connectTimeoutMS = socketSettings.connectTimeoutMS;
  74. readTimeoutMS = socketSettings.readTimeoutMS;
  75. keepAlive = socketSettings.keepAlive;
  76. receiveBufferSize = socketSettings.receiveBufferSize;
  77. sendBufferSize = socketSettings.sendBufferSize;
  78. return this;
  79. }
  80. /**
  81. * Sets the socket connect timeout.
  82. *
  83. * @param connectTimeout the connect timeout
  84. * @param timeUnit the time unit
  85. * @return this
  86. */
  87. public Builder connectTimeout(final int connectTimeout, final TimeUnit timeUnit) {
  88. this.connectTimeoutMS = MILLISECONDS.convert(connectTimeout, timeUnit);
  89. return this;
  90. }
  91. /**
  92. * Sets the socket read timeout.
  93. *
  94. * @param readTimeout the read timeout
  95. * @param timeUnit the time unit
  96. * @return this
  97. */
  98. public Builder readTimeout(final int readTimeout, final TimeUnit timeUnit) {
  99. this.readTimeoutMS = MILLISECONDS.convert(readTimeout, timeUnit);
  100. return this;
  101. }
  102. /**
  103. * Sets keep-alive.
  104. *
  105. * @param keepAlive false if keep-alive should be disabled
  106. * @return this
  107. * @deprecated configuring keep-alive has been deprecated. It now defaults to true and disabling it is not recommended.
  108. * @see <a href="https://docs.mongodb.com/manual/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments">
  109. * Does TCP keep-alive time affect MongoDB Deployments?</a>
  110. */
  111. @Deprecated
  112. public Builder keepAlive(final boolean keepAlive) {
  113. this.keepAlive = keepAlive;
  114. return this;
  115. }
  116. /**
  117. * Sets the receive buffer size.
  118. *
  119. * @param receiveBufferSize the receive buffer size
  120. * @return this
  121. */
  122. public Builder receiveBufferSize(final int receiveBufferSize) {
  123. this.receiveBufferSize = receiveBufferSize;
  124. return this;
  125. }
  126. /**
  127. * Sets the send buffer size.
  128. *
  129. * @param sendBufferSize the send buffer size
  130. * @return this
  131. */
  132. public Builder sendBufferSize(final int sendBufferSize) {
  133. this.sendBufferSize = sendBufferSize;
  134. return this;
  135. }
  136. /**
  137. * Takes the settings from the given {@code ConnectionString} and applies them to the builder
  138. *
  139. * @param connectionString the connection string containing details of how to connect to MongoDB
  140. * @return this
  141. * @see com.mongodb.ConnectionString#getConnectTimeout()
  142. * @see com.mongodb.ConnectionString#getSocketTimeout()
  143. */
  144. public Builder applyConnectionString(final ConnectionString connectionString) {
  145. Integer connectTimeout = connectionString.getConnectTimeout();
  146. if (connectTimeout != null) {
  147. this.connectTimeout(connectTimeout, MILLISECONDS);
  148. }
  149. Integer socketTimeout = connectionString.getSocketTimeout();
  150. if (socketTimeout != null) {
  151. this.readTimeout(socketTimeout, MILLISECONDS);
  152. }
  153. return this;
  154. }
  155. /**
  156. * Build an instance of {@code SocketSettings}.
  157. * @return the socket settings for this builder
  158. */
  159. public SocketSettings build() {
  160. return new SocketSettings(this);
  161. }
  162. }
  163. /**
  164. * Gets the timeout for socket connect. Defaults to 10 seconds.
  165. *
  166. * @param timeUnit the time unit to get the timeout in
  167. * @return the connect timeout in the requested time unit.
  168. */
  169. public int getConnectTimeout(final TimeUnit timeUnit) {
  170. return (int) timeUnit.convert(connectTimeoutMS, MILLISECONDS);
  171. }
  172. /**
  173. * Gets the timeout for socket reads. Defaults to 0, which indicates no timeout
  174. *
  175. * @param timeUnit the time unit to get the timeout in
  176. * @return the read timeout in the requested time unit, or 0 if there is no timeout
  177. */
  178. public int getReadTimeout(final TimeUnit timeUnit) {
  179. return (int) timeUnit.convert(readTimeoutMS, MILLISECONDS);
  180. }
  181. /**
  182. * Gets whether keep-alive is enabled. Defaults to true.
  183. *
  184. * @return true if keep-alive is enabled.
  185. * @deprecated configuring keep-alive has been deprecated. It now defaults to true and disabling it is not recommended.
  186. * @see <a href="https://docs.mongodb.com/manual/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments">
  187. * Does TCP keep-alive time affect MongoDB Deployments?</a>
  188. */
  189. @Deprecated
  190. public boolean isKeepAlive() {
  191. return keepAlive;
  192. }
  193. /**
  194. * Gets the receive buffer size. Defaults to the operating system default.
  195. * @return the receive buffer size
  196. */
  197. public int getReceiveBufferSize() {
  198. return receiveBufferSize;
  199. }
  200. /**
  201. * Gets the send buffer size. Defaults to the operating system default.
  202. *
  203. * @return the send buffer size
  204. */
  205. public int getSendBufferSize() {
  206. return sendBufferSize;
  207. }
  208. @Override
  209. public boolean equals(final Object o) {
  210. if (this == o) {
  211. return true;
  212. }
  213. if (o == null || getClass() != o.getClass()) {
  214. return false;
  215. }
  216. SocketSettings that = (SocketSettings) o;
  217. if (connectTimeoutMS != that.connectTimeoutMS) {
  218. return false;
  219. }
  220. if (keepAlive != that.keepAlive) {
  221. return false;
  222. }
  223. if (readTimeoutMS != that.readTimeoutMS) {
  224. return false;
  225. }
  226. if (receiveBufferSize != that.receiveBufferSize) {
  227. return false;
  228. }
  229. if (sendBufferSize != that.sendBufferSize) {
  230. return false;
  231. }
  232. return true;
  233. }
  234. @Override
  235. public int hashCode() {
  236. int result = (int) (connectTimeoutMS ^ (connectTimeoutMS >>> 32));
  237. result = 31 * result + (int) (readTimeoutMS ^ (readTimeoutMS >>> 32));
  238. result = 31 * result + (keepAlive ? 1 : 0);
  239. result = 31 * result + receiveBufferSize;
  240. result = 31 * result + sendBufferSize;
  241. return result;
  242. }
  243. @Override
  244. public String toString() {
  245. return "SocketSettings{"
  246. + "connectTimeoutMS=" + connectTimeoutMS
  247. + ", readTimeoutMS=" + readTimeoutMS
  248. + ", keepAlive=" + keepAlive
  249. + ", receiveBufferSize=" + receiveBufferSize
  250. + ", sendBufferSize=" + sendBufferSize
  251. + '}';
  252. }
  253. SocketSettings(final Builder builder) {
  254. connectTimeoutMS = builder.connectTimeoutMS;
  255. readTimeoutMS = builder.readTimeoutMS;
  256. keepAlive = builder.keepAlive;
  257. receiveBufferSize = builder.receiveBufferSize;
  258. sendBufferSize = builder.sendBufferSize;
  259. }
  260. }