-
Notifications
You must be signed in to change notification settings - Fork 259
Set TCP_USER_TIMEOUT socket option #926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-robot
merged 3 commits into
openshift:master
from
p0lyn0mial:tcp-usr-timeout-dialer
Nov 2, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
| package network | ||
|
|
||
| import ( | ||
| "context" | ||
| "net" | ||
| ) | ||
|
|
||
| type DialContext func(ctx context.Context, network, address string) (net.Conn, error) | ||
|
|
||
| // DefaultDialContext returns a DialContext function from a network dialer with default options sets. | ||
| func DefaultClientDialContext() DialContext { | ||
| return dialerWithDefaultOptions() | ||
| } |
This file contains hidden or 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,91 @@ | ||
| // +build linux | ||
|
|
||
| package network | ||
|
|
||
| import ( | ||
| "context" | ||
| "net" | ||
| "os" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| func dialerWithDefaultOptions() DialContext { | ||
| nd := &net.Dialer{ | ||
| // TCP_USER_TIMEOUT does affect the behaviour of connect() which is controlled by this field so we set it to the same value | ||
| Timeout: 25 * time.Second, | ||
| } | ||
| return wrapDialContext(nd.DialContext) | ||
| } | ||
|
|
||
| func wrapDialContext(dc DialContext) DialContext { | ||
| return func(ctx context.Context, network, address string) (net.Conn, error) { | ||
| conn, err := dc(ctx, network, address) | ||
| if err != nil { | ||
| return conn, err | ||
| } | ||
|
|
||
| if tcpCon, ok := conn.(*net.TCPConn); ok { | ||
| tcpFD, err := tcpCon.File() | ||
| if err != nil { | ||
| return conn, err | ||
| } | ||
| if err := setDefaultSocketOptions(int(tcpFD.Fd())); err != nil { | ||
| return conn, err | ||
| } | ||
| } | ||
| return conn, nil | ||
| } | ||
| } | ||
|
|
||
| // setDefaultSocketOptions sets custom socket options so that we can detect connections to an unhealthy (dead) peer quickly. | ||
| // In particular we set TCP_USER_TIMEOUT that specifies the maximum amount of time that transmitted data may remain | ||
| // unacknowledged before TCP will forcibly close the connection. | ||
| // | ||
| // Note | ||
| // TCP_USER_TIMEOUT can't be too low because a single dropped packet might drop the entire connection. | ||
| // Ideally it should be set to: TCP_KEEPIDLE + TCP_KEEPINTVL * TCP_KEEPCNT | ||
| func setDefaultSocketOptions(fd int) error { | ||
| // specifies the maximum amount of time in milliseconds that transmitted data may remain | ||
| // unacknowledged before TCP will forcibly close the corresponding connection and return ETIMEDOUT to the application | ||
| tcpUserTimeoutInMilliSeconds := int(25 * time.Second / time.Millisecond) | ||
|
|
||
| // specifies the interval at which probes are sent in seconds | ||
| tcpKeepIntvl := int(roundDuration(5*time.Second, time.Second)) | ||
|
|
||
| // specifies the threshold for sending the first KEEP ALIVE probe in seconds | ||
| tcpKeepIdle := int(roundDuration(2*time.Second, time.Second)) | ||
|
|
||
| if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, tcpUserTimeoutInMilliSeconds); err != nil { | ||
| return wrapSyscallError("setsockopt", err) | ||
| } | ||
|
|
||
| if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, tcpKeepIntvl); err != nil { | ||
| return wrapSyscallError("setsockopt", err) | ||
| } | ||
|
|
||
| if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, tcpKeepIdle); err != nil { | ||
| return wrapSyscallError("setsockopt", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // roundDurationUp rounds d to the next multiple of to. | ||
| // | ||
| // note that it was copied from the std library | ||
| func roundDuration(d time.Duration, to time.Duration) time.Duration { | ||
| return (d + to - 1) / to | ||
| } | ||
|
|
||
| // wrapSyscallError takes an error and a syscall name. If the error is | ||
| // a syscall.Errno, it wraps it in a os.SyscallError using the syscall name. | ||
| // | ||
| // note that it was copied from the std library | ||
| func wrapSyscallError(name string, err error) error { | ||
| if _, ok := err.(syscall.Errno); ok { | ||
| err = os.NewSyscallError(name, err) | ||
| } | ||
| return err | ||
| } |
This file contains hidden or 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,19 @@ | ||
| // +build !linux | ||
|
|
||
| package network | ||
|
|
||
| import ( | ||
| "net" | ||
| "time" | ||
|
|
||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| func dialerWithDefaultOptions() DialContext { | ||
| klog.V(2).Info("Creating the default network Dialer (unsupported platform). It may take up to 15 minutes to detect broken connections and establish a new one") | ||
| nd := &net.Dialer{ | ||
| Timeout: 30 * time.Second, | ||
| KeepAlive: 30 * time.Second, | ||
| } | ||
| return nd.DialContext | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keepalive in linux is gone?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no,
KeepAlivesetts bothTCP_KEEPINTVLandTCP_KEEPIDLEto the same value. Since we want distinct values we are now setting them insetDefaultSocketOptionsfunction