Skip to content

Commit b98cecf

Browse files
corona10bradfitz
authored andcommitted
net: use same TCP Keep Alive interval between dial and accept
Fixes #31510 Change-Id: I601d114b617a055380bf3c805e2d9a9b0795b656 Reviewed-on: https://go-review.googlesource.com/c/go/+/175259 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent d199369 commit b98cecf

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

src/net/dial.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import (
1212
"time"
1313
)
1414

15+
// defaultTCPKeepAlive is a default constant value for TCPKeepAlive times
16+
// See golang.org/issue/31510
17+
const (
18+
defaultTCPKeepAlive = 15 * time.Second
19+
)
20+
1521
// A Dialer contains options for connecting to an address.
1622
//
1723
// The zero value for each field is equivalent to dialing
@@ -425,7 +431,7 @@ func (d *Dialer) DialContext(ctx context.Context, network, address string) (Conn
425431
setKeepAlive(tc.fd, true)
426432
ka := d.KeepAlive
427433
if d.KeepAlive == 0 {
428-
ka = 15 * time.Second
434+
ka = defaultTCPKeepAlive
429435
}
430436
setKeepAlivePeriod(tc.fd, ka)
431437
testHookSetKeepAlive(ka)

src/net/tcpsock_plan9.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"io"
1010
"os"
11-
"time"
1211
)
1312

1413
func (c *TCPConn) readFrom(r io.Reader) (int64, error) {
@@ -50,7 +49,7 @@ func (ln *TCPListener) accept() (*TCPConn, error) {
5049
setKeepAlive(fd, true)
5150
ka := ln.lc.KeepAlive
5251
if ln.lc.KeepAlive == 0 {
53-
ka = 3 * time.Minute
52+
ka = defaultTCPKeepAlive
5453
}
5554
setKeepAlivePeriod(fd, ka)
5655
}

src/net/tcpsock_posix.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"io"
1212
"os"
1313
"syscall"
14-
"time"
1514
)
1615

1716
func sockaddrToTCP(sa syscall.Sockaddr) Addr {
@@ -146,7 +145,7 @@ func (ln *TCPListener) accept() (*TCPConn, error) {
146145
setKeepAlive(fd, true)
147146
ka := ln.lc.KeepAlive
148147
if ln.lc.KeepAlive == 0 {
149-
ka = 3 * time.Minute
148+
ka = defaultTCPKeepAlive
150149
}
151150
setKeepAlivePeriod(fd, ka)
152151
}

0 commit comments

Comments
 (0)