Skip to content

Commit 238d4a1

Browse files
committed
Make socket keepAlive default to true
JAVA-2531
1 parent b3ed0ad commit 238d4a1

File tree

13 files changed

+89
-40
lines changed

13 files changed

+89
-40
lines changed

docs/reference/content/driver-async/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ There are two higher level MongoDB Asynchronous Java Drivers available, that use
1818
* [MongoDB Reactive Streams Java Driver](http://mongodb.github.io/mongo-java-driver-reactivestreams/) A Reactive Streams implementation for the JVM.
1919
{{% /note %}}
2020

21-
### What's New in 3.4
21+
### What's New in 3.5
2222

2323
The [What's New]({{< relref "whats-new.md" >}}) guide explains the major new features of the driver.
2424

docs/reference/content/driver-async/tutorials/connect-to-mongodb.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ deployment (e.g. standalone, replica set, or a sharded cluster) and use it acros
2424
- To dispose of an instance, call `MongoClient.close()` to clean up resources.
2525
{{% /note %}}
2626

27+
{{% note %}}
28+
The 3.5 release deprecated socket keep-alive settings, also socket keep-alive checks are now on by default.
29+
It is *strongly recommended* that system keep-alive settings should be configured with shorter timeouts.
30+
31+
See the
32+
['does TCP keep-alive time affect MongoDB deployments?']({{<docsref "/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments">}})
33+
documentation for more information.
34+
{{% /note %}}
35+
2736

2837
## Prerequisites
2938

docs/reference/content/driver/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ title = "MongoDB Driver"
77
pre = "<i class='fa fa-arrows-h'></i>"
88
+++
99

10-
## MongoDB Driver 3.4 Documentation
10+
## MongoDB Driver 3.5 Documentation
1111

1212
The following guide provides information on using the synchronous
13-
MongoDB Java Driver 3.4.
13+
MongoDB Java Driver 3.5.
1414

15-
### What's New in 3.4
15+
### What's New in 3.5
1616

1717
The [What's New]({{< relref "whats-new.md" >}}) guide explains
1818
the major new features of the driver.

docs/reference/content/driver/tutorials/connect-to-mongodb.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ MongoClient constructors, see
1919
[`MongoClient() API documentation`]({{< apiref "com/mongodb/MongoClient.html">}}).
2020
{{% /note %}}
2121

22+
{{% note %}}
23+
The 3.5 release deprecated socket keep-alive settings, also socket keep-alive checks are now on by default.
24+
It is *strongly recommended* that system keep-alive settings should be configured with shorter timeouts.
25+
26+
See the
27+
['does TCP keep-alive time affect MongoDB deployments?']({{<docsref "/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments">}})
28+
documentation for more information.
29+
{{% /note %}}
30+
2231
## Prerequisites
2332

2433
- Running MongoDB deployments to which to connect. For example, to connect to a standalone, you must have a running standalone.

docs/reference/content/whats-new.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,22 @@ title = "What's New"
77
pre = "<i class='fa fa-level-up'></i>"
88
+++
99

10-
# What's New
10+
# What's New 3.5
1111

12-
This release includes full support for the upcoming MongoDB 3.4 server release. Key new features include:
12+
13+
### KeepAlive configuration deprecated
14+
15+
The 3.5 release deprecated socket keep-alive settings, also socket keep-alive checks are now on by default.
16+
It is *strongly recommended* that system keep-alive settings should be configured with shorter timeouts.
17+
18+
See the
19+
['does TCP keep-alive time affect MongoDB deployments?']({{<docsref "/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments">}})
20+
documentation for more information.
21+
22+
23+
## What's New in 3.4
24+
25+
The 3.4 release included full support for the upcoming MongoDB 3.4 server release. Key new features include:
1326

1427
### Support for Decimal128 Format
1528

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void open() throws IOException {
6262
handler.getOpen();
6363
}
6464

65+
@SuppressWarnings("deprecation")
6566
@Override
6667
public void openAsync(final AsyncCompletionHandler<Void> handler) {
6768
isTrue("unopened", channel == null);

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static Builder builder() {
5050
public static class Builder {
5151
private long connectTimeoutMS = 10000;
5252
private long readTimeoutMS;
53-
private boolean keepAlive;
53+
private boolean keepAlive = true;
5454
private int receiveBufferSize;
5555
private int sendBufferSize;
5656

@@ -81,9 +81,13 @@ public Builder readTimeout(final int readTimeout, final TimeUnit timeUnit) {
8181
/**
8282
* Sets keep-alive.
8383
*
84-
* @param keepAlive true if keep-alive should be enabled
84+
* @param keepAlive false if keep-alive should be disabled
8585
* @return this
86+
* @deprecated configuring keep-alive has been deprecated. It now defaults to true and disabling it is not recommended.
87+
* @see <a href="https://docs.mongodb.com/manual/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments">
88+
* Does TCP keep-alive time affect MongoDB Deployments?</a>
8689
*/
90+
@Deprecated
8791
public Builder keepAlive(final boolean keepAlive) {
8892
this.keepAlive = keepAlive;
8993
return this;
@@ -159,10 +163,14 @@ public int getReadTimeout(final TimeUnit timeUnit) {
159163
}
160164

161165
/**
162-
* Gets whether keep-alive is enabled. Defaults to false.
166+
* Gets whether keep-alive is enabled. Defaults to true.
163167
*
164168
* @return true if keep-alive is enabled.
169+
* @deprecated configuring keep-alive has been deprecated. It now defaults to true and disabling it is not recommended.
170+
* @see <a href="https://docs.mongodb.com/manual/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments">
171+
* Does TCP keep-alive time affect MongoDB Deployments?</a>
165172
*/
173+
@Deprecated
166174
public boolean isKeepAlive() {
167175
return keepAlive;
168176
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import static java.util.concurrent.TimeUnit.MILLISECONDS;
3030

3131
final class SocketStreamHelper {
32+
33+
@SuppressWarnings("deprecation")
3234
static void initialize(final Socket socket, final ServerAddress address, final SocketSettings settings, final SslSettings sslSettings)
3335
throws IOException {
3436
socket.setTcpNoDelay(true);

driver-core/src/main/com/mongodb/connection/netty/NettyStream.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public void open() throws IOException {
101101
handler.get();
102102
}
103103

104+
@SuppressWarnings("deprecation")
104105
@Override
105106
public void openAsync(final AsyncCompletionHandler<Void> handler) {
106107
Bootstrap bootstrap = new Bootstrap();

driver-core/src/test/unit/com/mongodb/connection/SocketSettingsSpecification.groovy

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ import spock.lang.Specification
2121

2222
import static java.util.concurrent.TimeUnit.MILLISECONDS
2323

24+
@SuppressWarnings('deprecation')
2425
class SocketSettingsSpecification extends Specification {
26+
2527
def 'should have correct defaults'() {
2628
when:
2729
def settings = SocketSettings.builder().build()
2830

2931
then:
3032
settings.getConnectTimeout(MILLISECONDS) == 10000
3133
settings.getReadTimeout(MILLISECONDS) == 0
32-
!settings.keepAlive
34+
settings.keepAlive
3335
settings.receiveBufferSize == 0
3436
settings.sendBufferSize == 0
3537
}
@@ -39,17 +41,16 @@ class SocketSettingsSpecification extends Specification {
3941
def settings = SocketSettings.builder()
4042
.connectTimeout(5000, MILLISECONDS)
4143
.readTimeout(2000, MILLISECONDS)
42-
.keepAlive(true)
44+
.keepAlive(false)
4345
.sendBufferSize(1000)
4446
.receiveBufferSize(1500)
45-
.keepAlive(true)
4647
.build()
4748

4849

4950
then:
5051
settings.getConnectTimeout(MILLISECONDS) == 5000
5152
settings.getReadTimeout(MILLISECONDS) == 2000
52-
settings.keepAlive
53+
!settings.keepAlive
5354
settings.sendBufferSize == 1000
5455
settings.receiveBufferSize == 1500
5556
}
@@ -65,7 +66,7 @@ class SocketSettingsSpecification extends Specification {
6566
then:
6667
settings.getConnectTimeout(MILLISECONDS) == 5000
6768
settings.getReadTimeout(MILLISECONDS) == 2000
68-
!settings.keepAlive
69+
settings.keepAlive
6970
settings.sendBufferSize == 0
7071
settings.receiveBufferSize == 0
7172
}
@@ -79,15 +80,13 @@ class SocketSettingsSpecification extends Specification {
7980
.keepAlive(true)
8081
.sendBufferSize(1000)
8182
.receiveBufferSize(1500)
82-
.keepAlive(true)
8383
.build() ==
8484
SocketSettings.builder()
8585
.connectTimeout(5000, MILLISECONDS)
8686
.readTimeout(2000, MILLISECONDS)
8787
.keepAlive(true)
8888
.sendBufferSize(1000)
8989
.receiveBufferSize(1500)
90-
.keepAlive(true)
9190
.build()
9291
}
9392

@@ -105,20 +104,18 @@ class SocketSettingsSpecification extends Specification {
105104
.keepAlive(true)
106105
.sendBufferSize(1000)
107106
.receiveBufferSize(1500)
108-
.keepAlive(true)
109107
.build().hashCode() ==
110108
SocketSettings.builder()
111109
.connectTimeout(5000, MILLISECONDS)
112110
.readTimeout(2000, MILLISECONDS)
113111
.keepAlive(true)
114112
.sendBufferSize(1000)
115113
.receiveBufferSize(1500)
116-
.keepAlive(true)
117114
.build().hashCode()
118115
}
119116

120117
def 'different settings should have different hash codes'() {
121118
expect:
122119
SocketSettings.builder().keepAlive(true).build().hashCode() != SocketSettings.builder().keepAlive(false).build().hashCode()
123120
}
124-
}
121+
}

0 commit comments

Comments
 (0)