diff options
author | Carlo Alberto Ferraris <cafxx@strayorange.com> | 2018-04-15 11:34:19 +0900 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2018-12-03 16:36:46 +0000 |
commit | 5bd7e9c54f946eec95d32762e7e9e1222504bfc1 (patch) | |
tree | 829af0feb5345416ae5eaa663de8ed6d7742367c /src/net/dial_test.go | |
parent | 1adbb2bb9b256907eaf3f012d7f818765e6e2a2b (diff) | |
download | go-5bd7e9c54f946eec95d32762e7e9e1222504bfc1.tar.gz go-5bd7e9c54f946eec95d32762e7e9e1222504bfc1.zip |
net: enable TCP keepalives by default
This is just the first step in attempting to make all network connection have
timeouts as a "safe default". TCP keepalives only protect against certain
classes of network and host issues (e.g. server/OS crash), but do nothing
against application-level issues (e.g. an application that accepts connections
but then fails to serve requests).
The actual keep-alive duration (15s) is chosen to cause broken connections
to be closed after 2~3 minutes (depending on the OS, see #23549 for details).
We don't make the actual default value part of the public API for a number of
reasons:
- because it's not very useful by itself: as discussed in #23549 the actual
"timeout" after which the connection is torn down is duration*(KEEPCNT+1),
and we use the OS-wide value for KEEPCNT because there's currently no way
to set it from Go.
- because it may change in the future: if users need to rely on a specific
value they should explicitly set this value instead of relying on the default.
Fixes #23459
Change-Id: I348c03be97588d5001e6de0f377e7a93b51957fd
Reviewed-on: https://go-review.googlesource.com/c/107196
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/net/dial_test.go')
-rw-r--r-- | src/net/dial_test.go | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/src/net/dial_test.go b/src/net/dial_test.go index 983338885d..3a2c59a2d1 100644 --- a/src/net/dial_test.go +++ b/src/net/dial_test.go @@ -729,22 +729,29 @@ func TestDialerKeepAlive(t *testing.T) { if err := ls.buildup(handler); err != nil { t.Fatal(err) } - defer func() { testHookSetKeepAlive = func() {} }() + defer func() { testHookSetKeepAlive = func(time.Duration) {} }() - for _, keepAlive := range []bool{false, true} { - got := false - testHookSetKeepAlive = func() { got = true } - var d Dialer - if keepAlive { - d.KeepAlive = 30 * time.Second - } + tests := []struct { + ka time.Duration + expected time.Duration + }{ + {-1, -1}, + {0, 15 * time.Second}, + {5 * time.Second, 5 * time.Second}, + {30 * time.Second, 30 * time.Second}, + } + + for _, test := range tests { + var got time.Duration = -1 + testHookSetKeepAlive = func(d time.Duration) { got = d } + d := Dialer{KeepAlive: test.ka} c, err := d.Dial("tcp", ls.Listener.Addr().String()) if err != nil { t.Fatal(err) } c.Close() - if got != keepAlive { - t.Errorf("Dialer.KeepAlive = %v: SetKeepAlive called = %v, want %v", d.KeepAlive, got, !got) + if got != test.expected { + t.Errorf("Dialer.KeepAlive = %v: SetKeepAlive set to %v, want %v", d.KeepAlive, got, test.expected) } } } |