-
Notifications
You must be signed in to change notification settings - Fork 17.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: add KeepAliveConfig and implement SetKeepAliveConfig
Fixes #62254 Fixes #48622 Change-Id: Ida598e7fa914c8737fdbc1c813bcd68adb5119c3 Reviewed-on: https://go-review.googlesource.com/c/go/+/542275 Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Damien Neil <[email protected]> Run-TryBot: Andy Pan <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
- Loading branch information
Showing
32 changed files
with
1,131 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
pkg net, method (*TCPConn) SetKeepAliveConfig(KeepAliveConfig) error #62254 | ||
pkg net, type Dialer struct, KeepAliveConfig KeepAliveConfig #62254 | ||
pkg net, type KeepAliveConfig struct #62254 | ||
pkg net, type KeepAliveConfig struct, Count int #62254 | ||
pkg net, type KeepAliveConfig struct, Enable bool #62254 | ||
pkg net, type KeepAliveConfig struct, Idle time.Duration #62254 | ||
pkg net, type KeepAliveConfig struct, Interval time.Duration #62254 | ||
pkg net, type ListenConfig struct, KeepAliveConfig KeepAliveConfig #62254 | ||
pkg syscall (windows-386), const WSAENOPROTOOPT = 10042 #62254 | ||
pkg syscall (windows-386), const WSAENOPROTOOPT Errno #62254 | ||
pkg syscall (windows-amd64), const WSAENOPROTOOPT = 10042 #62254 | ||
pkg syscall (windows-amd64), const WSAENOPROTOOPT Errno #62254 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
The new type [`KeepAliveConfig`](/net#KeepAliveConfig) permits fine-tuning | ||
the keep-alive options for TCP connections, via a new | ||
[`TCPConn.SetKeepAliveConfig`](/net#TCPConn.SetKeepAliveConfig) method and | ||
new KeepAliveConfig fields for [`Dialer`](net#Dialer) and [`ListenConfig`](net#ListenConfig). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The syscall package now defines WSAENOPROTOOPT on Windows. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
See `syscall (windows-386)/62254.md`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build aix || freebsd || linux || netbsd || darwin || dragonfly | ||
|
||
package net | ||
|
||
import "time" | ||
|
||
var testConfigs = []KeepAliveConfig{ | ||
{ | ||
Enable: true, | ||
Idle: 5 * time.Second, | ||
Interval: 3 * time.Second, | ||
Count: 10, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: 0, | ||
Interval: 0, | ||
Count: 0, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: -1, | ||
Interval: -1, | ||
Count: -1, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: -1, | ||
Interval: 3 * time.Second, | ||
Count: 10, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: 5 * time.Second, | ||
Interval: -1, | ||
Count: 10, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: 5 * time.Second, | ||
Interval: 3 * time.Second, | ||
Count: -1, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: -1, | ||
Interval: -1, | ||
Count: 10, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: -1, | ||
Interval: 3 * time.Second, | ||
Count: -1, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: 5 * time.Second, | ||
Interval: -1, | ||
Count: -1, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: 0, | ||
Interval: 3 * time.Second, | ||
Count: 10, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: 5 * time.Second, | ||
Interval: 0, | ||
Count: 10, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: 5 * time.Second, | ||
Interval: 3 * time.Second, | ||
Count: 0, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: 0, | ||
Interval: 0, | ||
Count: 10, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: 0, | ||
Interval: 3 * time.Second, | ||
Count: 0, | ||
}, | ||
{ | ||
Enable: true, | ||
Idle: 5 * time.Second, | ||
Interval: 0, | ||
Count: 0, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2023 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
//go:build darwin | ||
|
||
package net | ||
|
||
import ( | ||
"syscall" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func getCurrentKeepAliveSettings(fd int) (cfg KeepAliveConfig, err error) { | ||
tcpKeepAlive, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_KEEPALIVE) | ||
if err != nil { | ||
return | ||
} | ||
tcpKeepAliveIdle, err := syscall.GetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE) | ||
if err != nil { | ||
return | ||
} | ||
tcpKeepAliveInterval, err := syscall.GetsockoptInt(fd, syscall.IPPROTO_TCP, sysTCP_KEEPINTVL) | ||
if err != nil { | ||
return | ||
} | ||
tcpKeepAliveCount, err := syscall.GetsockoptInt(fd, syscall.IPPROTO_TCP, sysTCP_KEEPCNT) | ||
if err != nil { | ||
return | ||
} | ||
cfg = KeepAliveConfig{ | ||
Enable: tcpKeepAlive != 0, | ||
Idle: time.Duration(tcpKeepAliveIdle) * time.Second, | ||
Interval: time.Duration(tcpKeepAliveInterval) * time.Second, | ||
Count: tcpKeepAliveCount, | ||
} | ||
return | ||
} | ||
|
||
func verifyKeepAliveSettings(t *testing.T, fd int, oldCfg, cfg KeepAliveConfig) { | ||
if cfg.Idle == 0 { | ||
cfg.Idle = defaultTCPKeepAliveIdle | ||
} | ||
if cfg.Interval == 0 { | ||
cfg.Interval = defaultTCPKeepAliveInterval | ||
} | ||
if cfg.Count == 0 { | ||
cfg.Count = defaultTCPKeepAliveCount | ||
} | ||
if cfg.Idle == -1 { | ||
cfg.Idle = oldCfg.Idle | ||
} | ||
if cfg.Interval == -1 { | ||
cfg.Interval = oldCfg.Interval | ||
} | ||
if cfg.Count == -1 { | ||
cfg.Count = oldCfg.Count | ||
} | ||
|
||
tcpKeepAlive, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_KEEPALIVE) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if (tcpKeepAlive != 0) != cfg.Enable { | ||
t.Fatalf("SO_KEEPALIVE: got %t; want %t", tcpKeepAlive != 0, cfg.Enable) | ||
} | ||
|
||
tcpKeepAliveIdle, err := syscall.GetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if time.Duration(tcpKeepAliveIdle)*time.Second != cfg.Idle { | ||
t.Fatalf("TCP_KEEPIDLE: got %ds; want %v", tcpKeepAliveIdle, cfg.Idle) | ||
} | ||
|
||
tcpKeepAliveInterval, err := syscall.GetsockoptInt(fd, syscall.IPPROTO_TCP, sysTCP_KEEPINTVL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if time.Duration(tcpKeepAliveInterval)*time.Second != cfg.Interval { | ||
t.Fatalf("TCP_KEEPINTVL: got %ds; want %v", tcpKeepAliveInterval, cfg.Interval) | ||
} | ||
|
||
tcpKeepAliveCount, err := syscall.GetsockoptInt(fd, syscall.IPPROTO_TCP, sysTCP_KEEPCNT) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if tcpKeepAliveCount != cfg.Count { | ||
t.Fatalf("TCP_KEEPCNT: got %d; want %d", tcpKeepAliveCount, cfg.Count) | ||
} | ||
} |
Oops, something went wrong.