Skip to content

Commit 06b083e

Browse files
author
Divjot Arora
committed
Enable TCP Keepalive by default.
GODRIVER-714 Change-Id: I080f3bbab626161adbd51085ce56cfc3164e6355
1 parent 3288575 commit 06b083e

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

mongo/options/clientoptions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func (c *ClientOptions) SetConnectTimeout(d time.Duration) *ClientOptions {
117117
}
118118

119119
// SetDialer specifies a custom dialer used to dial new connections to a server.
120+
// If a custom dialer is not set, a net.Dialer with a 300 second keepalive time will be used by default.
120121
func (c *ClientOptions) SetDialer(d ContextDialer) *ClientOptions {
121122
c.TopologyOptions = append(
122123
c.TopologyOptions,

x/network/connection/keepalive_300.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (C) MongoDB, Inc. 2017-present.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
//+build !go1.12
8+
9+
package connection
10+
11+
import "time"
12+
13+
const tcpKeepalive = 300 * time.Second
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (C) MongoDB, Inc. 2017-present.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
//+build go1.12
8+
9+
package connection
10+
11+
const tcpKeepalive = 0 // will be set by default on Go 1.12 and higher

x/network/connection/options.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ func newConfig(opts ...Option) (*config, error) {
4444
}
4545

4646
if cfg.dialer == nil {
47-
cfg.dialer = &net.Dialer{Timeout: cfg.connectTimeout}
47+
cfg.dialer = &net.Dialer{
48+
KeepAlive: tcpKeepalive,
49+
Timeout: cfg.connectTimeout,
50+
}
4851
}
4952

5053
return cfg, nil

0 commit comments

Comments
 (0)