Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
fdfbb406
Commit
fdfbb406
authored
Feb 24, 2014
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net: add Dialer.KeepAlive option
LGTM=rsc R=rsc CC=golang-codereviews
https://golang.org/cl/68380043
parent
e2fe968d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
1 deletion
+50
-1
src/pkg/net/dial.go
src/pkg/net/dial.go
+17
-1
src/pkg/net/dial_test.go
src/pkg/net/dial_test.go
+33
-0
No files found.
src/pkg/net/dial.go
View file @
fdfbb406
...
@@ -44,6 +44,12 @@ type Dialer struct {
...
@@ -44,6 +44,12 @@ type Dialer struct {
// destination is a host name that has multiple address family
// destination is a host name that has multiple address family
// DNS records.
// DNS records.
DualStack
bool
DualStack
bool
// KeepAlive specifies the keep-alive period for an active
// network connection.
// If zero, keep-alives are not enabled. Network protocols
// that do not support keep-alives ignore this field.
KeepAlive
time
.
Duration
}
}
// Return either now+Timeout or Deadline, whichever comes first.
// Return either now+Timeout or Deadline, whichever comes first.
...
@@ -162,9 +168,19 @@ func (d *Dialer) Dial(network, address string) (Conn, error) {
...
@@ -162,9 +168,19 @@ func (d *Dialer) Dial(network, address string) (Conn, error) {
return
dialMulti
(
network
,
address
,
d
.
LocalAddr
,
ras
,
deadline
)
return
dialMulti
(
network
,
address
,
d
.
LocalAddr
,
ras
,
deadline
)
}
}
}
}
return
dial
(
network
,
ra
.
toAddr
(),
dialer
,
d
.
deadline
())
c
,
err
:=
dial
(
network
,
ra
.
toAddr
(),
dialer
,
d
.
deadline
())
if
d
.
KeepAlive
>
0
&&
err
==
nil
{
if
tc
,
ok
:=
c
.
(
*
TCPConn
);
ok
{
tc
.
SetKeepAlive
(
true
)
tc
.
SetKeepAlivePeriod
(
d
.
KeepAlive
)
testHookSetKeepAlive
()
}
}
return
c
,
err
}
}
var
testHookSetKeepAlive
=
func
()
{}
// changed by dial_test.go
// dialMulti attempts to establish connections to each destination of
// dialMulti attempts to establish connections to each destination of
// the list of addresses. It will return the first established
// the list of addresses. It will return the first established
// connection and close the other connections. Otherwise it returns
// connection and close the other connections. Otherwise it returns
...
...
src/pkg/net/dial_test.go
View file @
fdfbb406
...
@@ -555,3 +555,36 @@ func TestDialDualStackLocalhost(t *testing.T) {
...
@@ -555,3 +555,36 @@ func TestDialDualStackLocalhost(t *testing.T) {
}
}
}
}
}
}
func
TestDialerKeepAlive
(
t
*
testing
.
T
)
{
ln
:=
newLocalListener
(
t
)
defer
ln
.
Close
()
defer
func
()
{
testHookSetKeepAlive
=
func
()
{}
}()
go
func
()
{
for
{
c
,
err
:=
ln
.
Accept
()
if
err
!=
nil
{
return
}
c
.
Close
()
}
}()
for
_
,
keepAlive
:=
range
[]
bool
{
false
,
true
}
{
got
:=
false
testHookSetKeepAlive
=
func
()
{
got
=
true
}
var
d
Dialer
if
keepAlive
{
d
.
KeepAlive
=
30
*
time
.
Second
}
c
,
err
:=
d
.
Dial
(
"tcp"
,
ln
.
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
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment