fix: set tcp keep alive

This commit is contained in:
debugtalk
2022-10-25 15:13:02 +08:00
parent 2959b94caf
commit 5d4f6610e3
2 changed files with 10 additions and 2 deletions

View File

@@ -93,10 +93,18 @@ func (wd *Driver) httpRequest(method string, rawURL string, rawBody []byte) (raw
}
func convertToHTTPClient(conn net.Conn) *http.Client {
// set tcp keep alive
tcpConn := conn.(*net.TCPConn)
if err := tcpConn.SetKeepAlive(true); err != nil {
log.Error().Err(err).Msg("set tcp keep alive failed")
}
if err := tcpConn.SetKeepAlivePeriod(5 * time.Second); err != nil {
log.Error().Err(err).Msg("set tcp keep alive period failed")
}
return &http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return conn, nil
return tcpConn, nil
},
},
Timeout: 0,