@@ -44,6 +44,12 @@ type Dialer struct {
44
44
// destination is a host name that has multiple address family
45
45
// DNS records.
46
46
DualStack bool
47
+
48
+ // KeepAlive specifies the keep-alive period for an active
49
+ // network connection.
50
+ // If zero, keep-alives are not enabled. Network protocols
51
+ // that do not support keep-alives ignore this field.
52
+ KeepAlive time.Duration
47
53
}
48
54
49
55
// Return either now+Timeout or Deadline, whichever comes first.
@@ -162,9 +168,19 @@ func (d *Dialer) Dial(network, address string) (Conn, error) {
162
168
return dialMulti (network , address , d .LocalAddr , ras , deadline )
163
169
}
164
170
}
165
- return dial (network , ra .toAddr (), dialer , d .deadline ())
171
+ c , err := dial (network , ra .toAddr (), dialer , d .deadline ())
172
+ if d .KeepAlive > 0 && err == nil {
173
+ if tc , ok := c .(* TCPConn ); ok {
174
+ tc .SetKeepAlive (true )
175
+ tc .SetKeepAlivePeriod (d .KeepAlive )
176
+ testHookSetKeepAlive ()
177
+ }
178
+ }
179
+ return c , err
166
180
}
167
181
182
+ var testHookSetKeepAlive = func () {} // changed by dial_test.go
183
+
168
184
// dialMulti attempts to establish connections to each destination of
169
185
// the list of addresses. It will return the first established
170
186
// connection and close the other connections. Otherwise it returns
0 commit comments